| import gradio as gr |
| import openai |
| from openai import OpenAI |
| import os |
| import time |
|
|
| def setup_client(): |
| api_key = os.environ.get('OPENROUTER_API_KEY') |
| if not api_key: |
| raise ValueError("OPENROUTER_API_KEY environment variable not set") |
| |
| return OpenAI( |
| base_url="https://openrouter.ai/api/v1", |
| api_key=api_key, |
| ) |
|
|
| def chat_with_grok_streaming(message, history): |
| """Main chat function with streaming""" |
| if not message: |
| return history, "" |
| |
| |
| print(f"User message: {message}") |
| |
| |
| history.append((message, "")) |
| |
| try: |
| client = setup_client() |
| |
| |
| stream = client.chat.completions.create( |
| model="x-ai/grok-4", |
| messages=[ |
| { |
| "role": "user", |
| "content": message |
| } |
| ], |
| max_tokens=1000, |
| temperature=0.7, |
| stream=True |
| ) |
| |
| |
| response = "" |
| for chunk in stream: |
| if chunk.choices[0].delta.content is not None: |
| response += chunk.choices[0].delta.content |
| |
| history[-1] = (message, response) |
| yield history, "" |
| time.sleep(0.05) |
| |
| |
| history[-1] = (message, response) |
| print(f"AI response: {response}") |
| yield history, "" |
| |
| except Exception as e: |
| error_msg = f"Error: {str(e)}" |
| print(f"Error occurred: {str(e)}") |
| history[-1] = (message, error_msg) |
| yield history, "" |
|
|
| def clear_chat(): |
| """Clear the chat history""" |
| return [], "" |
|
|
| |
| with gr.Blocks(title="Grok 4 Chat Interface by Xhaheen ", theme=gr.themes.Soft()) as demo: |
| gr.HTML(""" |
| <div style="text-align: center; padding: 20px;"> |
| <h1>π€ Grok 4 Chat Interface</h1> |
| <p>Chat with xAI's Grok 4 model via OpenRouter (Streaming)</p> |
| </div> |
| """) |
| |
| with gr.Row(): |
| with gr.Column(scale=1): |
| clear_btn = gr.Button("ποΈ Clear Chat", variant="secondary", size="large") |
| |
| with gr.Column(scale=3): |
| chatbot = gr.Chatbot( |
| label="Chat with Grok 4", |
| height=500, |
| show_copy_button=True |
| ) |
| |
| msg_input = gr.Textbox( |
| label="Message", |
| placeholder="Type your message here...", |
| lines=2, |
| max_lines=5 |
| ) |
| |
| send_btn = gr.Button("Send π", variant="primary", size="large") |
| |
| |
| def submit_message(message, history): |
| if message: |
| |
| yield from chat_with_grok_streaming(message, history) |
| else: |
| yield history, message |
| |
| |
| send_btn.click( |
| submit_message, |
| inputs=[msg_input, chatbot], |
| outputs=[chatbot, msg_input] |
| ) |
| |
| |
| msg_input.submit( |
| submit_message, |
| inputs=[msg_input, chatbot], |
| outputs=[chatbot, msg_input] |
| ) |
| |
| |
| clear_btn.click( |
| clear_chat, |
| outputs=[chatbot, msg_input] |
| ) |
|
|
| |
| if __name__ == "__main__": |
| demo.launch() |