import os
import requests
import gradio as gr

# Set your API keys
os.environ['XAI_API_KEY'] = 'xai-Gxw7oW4yR6Q6oTd0v9lDRLotXZQYJNz9YKlH7R6eMyTmqIV9h6uustEGZAaJEvGmewlwbUnM1jTX4chj'  # Replace with your actual xAI API key
os.environ['SERPER_API_KEY'] = '206256c6acfbcd5a46195f3312aaa7e8ed38ae5f'  # Replace with your Serper AI 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! Type your message below.",
)

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