Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import requests
|
| 3 |
+
import gradio as gr
|
| 4 |
+
|
| 5 |
+
# Set your API keys
|
| 6 |
+
os.environ['XAI_API_KEY'] = 'your_xai_api_key_here' # Replace with your actual xAI API key
|
| 7 |
+
os.environ['SPORTS_API_KEY'] = 'your_sports_api_key_here' # Replace with your sports data API key
|
| 8 |
+
|
| 9 |
+
# Initialize conversation history
|
| 10 |
+
conversation_history = []
|
| 11 |
+
|
| 12 |
+
def fetch_sports_scores(sport):
|
| 13 |
+
"""Fetches real-time sports scores for the specified sport."""
|
| 14 |
+
url = f"https://api.sportsdata.io/v4/{sport}/scores"
|
| 15 |
+
headers = {
|
| 16 |
+
"User-Agent": "YourAppName",
|
| 17 |
+
"Authorization": f"Bearer {os.environ['SPORTS_API_KEY']}"
|
| 18 |
+
}
|
| 19 |
+
|
| 20 |
+
response = requests.get(url, headers=headers)
|
| 21 |
+
|
| 22 |
+
if response.status_code == 200:
|
| 23 |
+
return response.json() # Return JSON data
|
| 24 |
+
else:
|
| 25 |
+
return f"Error fetching sports data: {response.text}"
|
| 26 |
+
|
| 27 |
+
def browse_web_page(url):
|
| 28 |
+
"""Simulated function to browse a web page."""
|
| 29 |
+
return f"Browsing to {url}..."
|
| 30 |
+
|
| 31 |
+
def chat_with_bot(user_input):
|
| 32 |
+
# Append user input to conversation history
|
| 33 |
+
conversation_history.append({"role": "user", "content": user_input})
|
| 34 |
+
|
| 35 |
+
url = "https://api.x.ai/v1/chat/completions"
|
| 36 |
+
headers = {
|
| 37 |
+
"Content-Type": "application/json",
|
| 38 |
+
"Authorization": f"Bearer {os.environ['XAI_API_KEY']}"
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
messages = [{"role": "system", "content": "You are a helpful assistant."}] + \
|
| 42 |
+
[{"role": msg["role"], "content": msg["content"]} for msg in conversation_history]
|
| 43 |
+
|
| 44 |
+
data = {
|
| 45 |
+
"messages": messages,
|
| 46 |
+
"model": "grok-beta",
|
| 47 |
+
"functions": [
|
| 48 |
+
{
|
| 49 |
+
"name": "fetch_sports_scores",
|
| 50 |
+
"description": "Fetch real-time scores for a specific sport.",
|
| 51 |
+
"parameters": {
|
| 52 |
+
"type": "object",
|
| 53 |
+
"properties": {
|
| 54 |
+
"sport": {
|
| 55 |
+
"type": "string",
|
| 56 |
+
"description": "The sport you want scores for.",
|
| 57 |
+
"example_value": "football",
|
| 58 |
+
},
|
| 59 |
+
},
|
| 60 |
+
"required": ["sport"],
|
| 61 |
+
},
|
| 62 |
+
},
|
| 63 |
+
{
|
| 64 |
+
"name": "browse_web_page",
|
| 65 |
+
"description": "Browse a web page.",
|
| 66 |
+
"parameters": {
|
| 67 |
+
"type": "object",
|
| 68 |
+
"properties": {
|
| 69 |
+
"url": {
|
| 70 |
+
"type": "string",
|
| 71 |
+
"description": "The URL of the web page to browse.",
|
| 72 |
+
"example_value": "https://example.com",
|
| 73 |
+
},
|
| 74 |
+
},
|
| 75 |
+
"required": ["url"],
|
| 76 |
+
},
|
| 77 |
+
},
|
| 78 |
+
],
|
| 79 |
+
"stream": False,
|
| 80 |
+
"temperature": 0.7
|
| 81 |
+
}
|
| 82 |
+
|
| 83 |
+
response = requests.post(url, headers=headers, json=data)
|
| 84 |
+
|
| 85 |
+
if response.status_code == 200:
|
| 86 |
+
bot_response = response.json()['choices'][0]['message']
|
| 87 |
+
|
| 88 |
+
# Check if there's a function call request
|
| 89 |
+
if 'function_call' in bot_response:
|
| 90 |
+
function_name = bot_response['function_call']['name']
|
| 91 |
+
args = bot_response['function_call']['arguments']
|
| 92 |
+
|
| 93 |
+
if function_name == 'fetch_sports_scores':
|
| 94 |
+
sport = args.get('sport')
|
| 95 |
+
result = fetch_sports_scores(sport)
|
| 96 |
+
bot_response_content = str(result)
|
| 97 |
+
elif function_name == 'browse_web_page':
|
| 98 |
+
url = args.get('url')
|
| 99 |
+
bot_response_content = browse_web_page(url)
|
| 100 |
+
else:
|
| 101 |
+
bot_response_content = bot_response['content']
|
| 102 |
+
else:
|
| 103 |
+
bot_response_content = bot_response['content']
|
| 104 |
+
|
| 105 |
+
# Append bot response to conversation history
|
| 106 |
+
conversation_history.append({"role": "assistant", "content": bot_response_content})
|
| 107 |
+
|
| 108 |
+
# Format the chat history for display
|
| 109 |
+
chat_display = "\n".join([f"{msg['role'].capitalize()}: {msg['content']}" for msg in conversation_history])
|
| 110 |
+
|
| 111 |
+
return chat_display
|
| 112 |
+
else:
|
| 113 |
+
return f"Error: {response.text}"
|
| 114 |
+
|
| 115 |
+
# Create Gradio interface
|
| 116 |
+
iface = gr.Interface(
|
| 117 |
+
fn=chat_with_bot,
|
| 118 |
+
inputs="text",
|
| 119 |
+
outputs="text",
|
| 120 |
+
title="xAI Chatbot with Function Calling",
|
| 121 |
+
description="Chat with Grok! Type your message below.",
|
| 122 |
+
)
|
| 123 |
+
|
| 124 |
+
# Launch the interface
|
| 125 |
+
if __name__ == "__main__":
|
| 126 |
+
iface.launch()
|