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()