Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -5,18 +5,25 @@ import gradio as gr
|
|
5 |
# Set your API key
|
6 |
os.environ['XAI_API_KEY'] = 'xai-Gxw7oW4yR6Q6oTd0v9lDRLotXZQYJNz9YKlH7R6eMyTmqIV9h6uustEGZAaJEvGmewlwbUnM1jTX4chj' # Replace with your actual API key
|
7 |
|
|
|
|
|
|
|
8 |
def chat_with_bot(user_input):
|
|
|
|
|
|
|
9 |
url = "https://api.x.ai/v1/chat/completions"
|
10 |
headers = {
|
11 |
"Content-Type": "application/json",
|
12 |
"Authorization": f"Bearer {os.environ['XAI_API_KEY']}"
|
13 |
}
|
14 |
|
|
|
|
|
|
|
|
|
15 |
data = {
|
16 |
-
"messages":
|
17 |
-
{"role": "system", "content": "You are a helpful assistant."},
|
18 |
-
{"role": "user", "content": user_input}
|
19 |
-
],
|
20 |
"model": "grok-beta",
|
21 |
"stream": False,
|
22 |
"temperature": 0.7
|
@@ -25,12 +32,26 @@ def chat_with_bot(user_input):
|
|
25 |
response = requests.post(url, headers=headers, json=data)
|
26 |
|
27 |
if response.status_code == 200:
|
28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
else:
|
30 |
return f"Error: {response.text}"
|
31 |
|
32 |
# Create Gradio interface
|
33 |
-
iface = gr.Interface(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
|
35 |
# Launch the interface
|
36 |
if __name__ == "__main__":
|
|
|
5 |
# Set your API key
|
6 |
os.environ['XAI_API_KEY'] = 'xai-Gxw7oW4yR6Q6oTd0v9lDRLotXZQYJNz9YKlH7R6eMyTmqIV9h6uustEGZAaJEvGmewlwbUnM1jTX4chj' # Replace with your actual API key
|
7 |
|
8 |
+
# Initialize conversation history
|
9 |
+
conversation_history = []
|
10 |
+
|
11 |
def chat_with_bot(user_input):
|
12 |
+
# Append user input to conversation history
|
13 |
+
conversation_history.append({"role": "user", "content": user_input})
|
14 |
+
|
15 |
url = "https://api.x.ai/v1/chat/completions"
|
16 |
headers = {
|
17 |
"Content-Type": "application/json",
|
18 |
"Authorization": f"Bearer {os.environ['XAI_API_KEY']}"
|
19 |
}
|
20 |
|
21 |
+
# Prepare messages for the API call
|
22 |
+
messages = [{"role": "system", "content": "You are a helpful assistant."}] + \
|
23 |
+
[{"role": msg["role"], "content": msg["content"]} for msg in conversation_history]
|
24 |
+
|
25 |
data = {
|
26 |
+
"messages": messages,
|
|
|
|
|
|
|
27 |
"model": "grok-beta",
|
28 |
"stream": False,
|
29 |
"temperature": 0.7
|
|
|
32 |
response = requests.post(url, headers=headers, json=data)
|
33 |
|
34 |
if response.status_code == 200:
|
35 |
+
bot_response = response.json()['choices'][0]['message']['content']
|
36 |
+
# Append bot response to conversation history
|
37 |
+
conversation_history.append({"role": "assistant", "content": bot_response})
|
38 |
+
|
39 |
+
# Format the chat history for display
|
40 |
+
chat_display = "\n".join([f"{msg['role'].capitalize()}: {msg['content']}" for msg in conversation_history])
|
41 |
+
|
42 |
+
return chat_display
|
43 |
else:
|
44 |
return f"Error: {response.text}"
|
45 |
|
46 |
# Create Gradio interface
|
47 |
+
iface = gr.Interface(
|
48 |
+
fn=chat_with_bot,
|
49 |
+
inputs="text",
|
50 |
+
outputs="text",
|
51 |
+
title="xAI Chatbot",
|
52 |
+
description="Chat with Grok! Type your message below.",
|
53 |
+
theme="default",
|
54 |
+
)
|
55 |
|
56 |
# Launch the interface
|
57 |
if __name__ == "__main__":
|