File size: 1,134 Bytes
410848e
 
 
 
 
 
 
 
 
 
 
 
 
18a06ac
410848e
18a06ac
 
 
 
410848e
 
 
 
18a06ac
410848e
18a06ac
410848e
18a06ac
410848e
 
 
 
18a06ac
410848e
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import os
import requests
import gradio as gr

# Set your API key
os.environ['XAI_API_KEY'] = 'xai-Gxw7oW4yR6Q6oTd0v9lDRLotXZQYJNz9YKlH7R6eMyTmqIV9h6uustEGZAaJEvGmewlwbUnM1jTX4chj'  # Replace with your actual API key

def chat_with_bot(user_input):
    url = "https://api.x.ai/v1/chat/completions"
    headers = {
        "Content-Type": "application/json",
        "Authorization": f"Bearer {os.environ['XAI_API_KEY']}"
    }
   
    data = {
        "messages": [
            {"role": "system", "content": "You are a helpful assistant."},
            {"role": "user", "content": user_input}
        ],
        "model": "grok-beta",
        "stream": False,
        "temperature": 0.7
    }
   
    response = requests.post(url, headers=headers, json=data)
   
    if response.status_code == 200:
        return response.json()['choices'][0]['message']['content']
    else:
        return f"Error: {response.text}"

# Create Gradio interface
iface = gr.Interface(fn=chat_with_bot, inputs="text", outputs="text", title="xAI Chatbot", description="Chat with Grok!")

# Launch the interface
if __name__ == "__main__":
    iface.launch()