Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import requests
|
| 3 |
+
import gradio as gr
|
| 4 |
+
|
| 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
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
response = requests.post(url, headers=headers, json=data)
|
| 26 |
+
|
| 27 |
+
if response.status_code == 200:
|
| 28 |
+
return response.json()['choices'][0]['message']['content']
|
| 29 |
+
else:
|
| 30 |
+
return f"Error: {response.text}"
|
| 31 |
+
|
| 32 |
+
# Create Gradio interface
|
| 33 |
+
iface = gr.Interface(fn=chat_with_bot, inputs="text", outputs="text", title="xAI Chatbot", description="Chat with Grok!")
|
| 34 |
+
|
| 35 |
+
# Launch the interface
|
| 36 |
+
if __name__ == "__main__":
|
| 37 |
+
iface.launch()
|