Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,110 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import openai
|
3 |
+
from openai import OpenAI
|
4 |
+
import os
|
5 |
+
|
6 |
+
def setup_client():
|
7 |
+
api_key = os.environ.get('OPENROUTER_API_KEY')
|
8 |
+
if not api_key:
|
9 |
+
raise ValueError("OPENROUTER_API_KEY environment variable not set")
|
10 |
+
|
11 |
+
return OpenAI(
|
12 |
+
base_url="https://openrouter.ai/api/v1",
|
13 |
+
api_key=api_key,
|
14 |
+
)
|
15 |
+
|
16 |
+
def chat_with_grok(message, history):
|
17 |
+
"""Main chat function"""
|
18 |
+
if not message:
|
19 |
+
return history, ""
|
20 |
+
|
21 |
+
try:
|
22 |
+
client = setup_client()
|
23 |
+
|
24 |
+
# Create the completion
|
25 |
+
completion = client.chat.completions.create(
|
26 |
+
model="x-ai/grok-4",
|
27 |
+
messages=[
|
28 |
+
{
|
29 |
+
"role": "user",
|
30 |
+
"content": message
|
31 |
+
}
|
32 |
+
],
|
33 |
+
max_tokens=1000,
|
34 |
+
temperature=0.7
|
35 |
+
)
|
36 |
+
|
37 |
+
response = completion.choices[0].message.content
|
38 |
+
|
39 |
+
# Add to history
|
40 |
+
history.append((message, response))
|
41 |
+
|
42 |
+
return history, ""
|
43 |
+
|
44 |
+
except Exception as e:
|
45 |
+
error_msg = f"Error: {str(e)}"
|
46 |
+
history.append((message, error_msg))
|
47 |
+
return history, ""
|
48 |
+
|
49 |
+
def clear_chat():
|
50 |
+
"""Clear the chat history"""
|
51 |
+
return [], ""
|
52 |
+
|
53 |
+
# Create the Gradio interface
|
54 |
+
with gr.Blocks(title="Grok 4 Chat Interface by Xhaheen ", theme=gr.themes.Soft()) as demo:
|
55 |
+
gr.HTML("""
|
56 |
+
<div style="text-align: center; padding: 20px;">
|
57 |
+
<h1>π€ Grok 4 Chat Interface</h1>
|
58 |
+
<p>Chat with xAI's Grok 4 model via OpenRouter</p>
|
59 |
+
</div>
|
60 |
+
""")
|
61 |
+
|
62 |
+
with gr.Row():
|
63 |
+
with gr.Column(scale=1):
|
64 |
+
clear_btn = gr.Button("ποΈ Clear Chat", variant="secondary", size="large")
|
65 |
+
|
66 |
+
with gr.Column(scale=3):
|
67 |
+
chatbot = gr.Chatbot(
|
68 |
+
label="Chat with Grok 4",
|
69 |
+
height=500,
|
70 |
+
show_copy_button=True
|
71 |
+
)
|
72 |
+
|
73 |
+
msg_input = gr.Textbox(
|
74 |
+
label="Message",
|
75 |
+
placeholder="Type your message here...",
|
76 |
+
lines=2,
|
77 |
+
max_lines=5
|
78 |
+
)
|
79 |
+
|
80 |
+
send_btn = gr.Button("Send π", variant="primary", size="large")
|
81 |
+
|
82 |
+
# Event handlers
|
83 |
+
def submit_message(message, history):
|
84 |
+
if message:
|
85 |
+
return chat_with_grok(message, history)
|
86 |
+
return history, message
|
87 |
+
|
88 |
+
# Send message on button click
|
89 |
+
send_btn.click(
|
90 |
+
submit_message,
|
91 |
+
inputs=[msg_input, chatbot],
|
92 |
+
outputs=[chatbot, msg_input]
|
93 |
+
)
|
94 |
+
|
95 |
+
# Send message on Enter key
|
96 |
+
msg_input.submit(
|
97 |
+
submit_message,
|
98 |
+
inputs=[msg_input, chatbot],
|
99 |
+
outputs=[chatbot, msg_input]
|
100 |
+
)
|
101 |
+
|
102 |
+
# Clear chat
|
103 |
+
clear_btn.click(
|
104 |
+
clear_chat,
|
105 |
+
outputs=[chatbot, msg_input]
|
106 |
+
)
|
107 |
+
|
108 |
+
# Launch the interface - Remove share=True for Hugging Face
|
109 |
+
if __name__ == "__main__":
|
110 |
+
demo.launch()
|