Update app.py
Browse files
app.py
CHANGED
@@ -2,6 +2,7 @@ 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')
|
@@ -13,18 +14,22 @@ def setup_client():
|
|
13 |
api_key=api_key,
|
14 |
)
|
15 |
|
16 |
-
def
|
17 |
-
"""Main chat function"""
|
18 |
if not message:
|
19 |
return history, ""
|
20 |
-
|
|
|
21 |
print(f"User message: {message}")
|
22 |
|
|
|
|
|
|
|
23 |
try:
|
24 |
client = setup_client()
|
25 |
|
26 |
-
# Create the completion
|
27 |
-
|
28 |
model="x-ai/grok-4",
|
29 |
messages=[
|
30 |
{
|
@@ -33,22 +38,30 @@ def chat_with_grok(message, history):
|
|
33 |
}
|
34 |
],
|
35 |
max_tokens=1000,
|
36 |
-
temperature=0.7
|
|
|
37 |
)
|
38 |
|
39 |
-
|
40 |
-
|
41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
|
43 |
-
#
|
44 |
-
history
|
45 |
-
|
46 |
-
|
47 |
|
48 |
except Exception as e:
|
49 |
error_msg = f"Error: {str(e)}"
|
50 |
-
|
51 |
-
|
|
|
52 |
|
53 |
def clear_chat():
|
54 |
"""Clear the chat history"""
|
@@ -59,7 +72,7 @@ with gr.Blocks(title="Grok 4 Chat Interface by Xhaheen ", theme=gr.themes.Soft()
|
|
59 |
gr.HTML("""
|
60 |
<div style="text-align: center; padding: 20px;">
|
61 |
<h1>🤖 Grok 4 Chat Interface</h1>
|
62 |
-
<p>Chat with xAI's Grok 4 model via OpenRouter</p>
|
63 |
</div>
|
64 |
""")
|
65 |
|
@@ -86,8 +99,10 @@ with gr.Blocks(title="Grok 4 Chat Interface by Xhaheen ", theme=gr.themes.Soft()
|
|
86 |
# Event handlers
|
87 |
def submit_message(message, history):
|
88 |
if message:
|
89 |
-
|
90 |
-
|
|
|
|
|
91 |
|
92 |
# Send message on button click
|
93 |
send_btn.click(
|
|
|
2 |
import openai
|
3 |
from openai import OpenAI
|
4 |
import os
|
5 |
+
import time
|
6 |
|
7 |
def setup_client():
|
8 |
api_key = os.environ.get('OPENROUTER_API_KEY')
|
|
|
14 |
api_key=api_key,
|
15 |
)
|
16 |
|
17 |
+
def chat_with_grok_streaming(message, history):
|
18 |
+
"""Main chat function with streaming"""
|
19 |
if not message:
|
20 |
return history, ""
|
21 |
+
|
22 |
+
# Add logging
|
23 |
print(f"User message: {message}")
|
24 |
|
25 |
+
# Add user message immediately
|
26 |
+
history.append((message, ""))
|
27 |
+
|
28 |
try:
|
29 |
client = setup_client()
|
30 |
|
31 |
+
# Create the streaming completion
|
32 |
+
stream = client.chat.completions.create(
|
33 |
model="x-ai/grok-4",
|
34 |
messages=[
|
35 |
{
|
|
|
38 |
}
|
39 |
],
|
40 |
max_tokens=1000,
|
41 |
+
temperature=0.7,
|
42 |
+
stream=True # Enable streaming
|
43 |
)
|
44 |
|
45 |
+
# Stream the response
|
46 |
+
response = ""
|
47 |
+
for chunk in stream:
|
48 |
+
if chunk.choices[0].delta.content is not None:
|
49 |
+
response += chunk.choices[0].delta.content
|
50 |
+
# Update the last message in history with the streaming response
|
51 |
+
history[-1] = (message, response)
|
52 |
+
yield history, ""
|
53 |
+
time.sleep(0.05) # Small delay to make streaming visible
|
54 |
|
55 |
+
# Final update and logging
|
56 |
+
history[-1] = (message, response)
|
57 |
+
print(f"AI response: {response}")
|
58 |
+
yield history, ""
|
59 |
|
60 |
except Exception as e:
|
61 |
error_msg = f"Error: {str(e)}"
|
62 |
+
print(f"Error occurred: {str(e)}")
|
63 |
+
history[-1] = (message, error_msg)
|
64 |
+
yield history, ""
|
65 |
|
66 |
def clear_chat():
|
67 |
"""Clear the chat history"""
|
|
|
72 |
gr.HTML("""
|
73 |
<div style="text-align: center; padding: 20px;">
|
74 |
<h1>🤖 Grok 4 Chat Interface</h1>
|
75 |
+
<p>Chat with xAI's Grok 4 model via OpenRouter (Streaming)</p>
|
76 |
</div>
|
77 |
""")
|
78 |
|
|
|
99 |
# Event handlers
|
100 |
def submit_message(message, history):
|
101 |
if message:
|
102 |
+
# Use yield from for streaming
|
103 |
+
yield from chat_with_grok_streaming(message, history)
|
104 |
+
else:
|
105 |
+
yield history, message
|
106 |
|
107 |
# Send message on button click
|
108 |
send_btn.click(
|