typesdigital commited on
Commit
b826358
·
verified ·
1 Parent(s): ed0f938

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -89
app.py CHANGED
@@ -4,78 +4,21 @@ import gradio as gr
4
 
5
  # Set your API keys
6
  os.environ['XAI_API_KEY'] = 'xai-Gxw7oW4yR6Q6oTd0v9lDRLotXZQYJNz9YKlH7R6eMyTmqIV9h6uustEGZAaJEvGmewlwbUnM1jTX4chj' # Replace with your actual xAI API key
7
- os.environ['SPORTS_API_KEY'] = '21253d4ad8mshc858b5f3ba04919p1b5a03jsna381170f15fe' # 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
  }
@@ -83,32 +26,7 @@ def chat_with_bot(user_input):
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
 
@@ -117,7 +35,7 @@ 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
 
 
4
 
5
  # Set your API keys
6
  os.environ['XAI_API_KEY'] = 'xai-Gxw7oW4yR6Q6oTd0v9lDRLotXZQYJNz9YKlH7R6eMyTmqIV9h6uustEGZAaJEvGmewlwbUnM1jTX4chj' # Replace with your actual xAI API key
7
+ os.environ['SERPER_API_KEY'] = '206256c6acfbcd5a46195f3312aaa7e8ed38ae5f' # Replace with your Serper AI key
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
 
9
  def chat_with_bot(user_input):
 
 
 
10
  url = "https://api.x.ai/v1/chat/completions"
11
  headers = {
12
  "Content-Type": "application/json",
13
  "Authorization": f"Bearer {os.environ['XAI_API_KEY']}"
14
  }
15
 
 
 
 
16
  data = {
17
+ "messages": [
18
+ {"role": "system", "content": "You are a helpful assistant."},
19
+ {"role": "user", "content": user_input}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
  ],
21
+ "model": "grok-beta",
22
  "stream": False,
23
  "temperature": 0.7
24
  }
 
26
  response = requests.post(url, headers=headers, json=data)
27
 
28
  if response.status_code == 200:
29
+ return response.json()['choices'][0]['message']['content']
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
  else:
31
  return f"Error: {response.text}"
32
 
 
35
  fn=chat_with_bot,
36
  inputs="text",
37
  outputs="text",
38
+ title="xAI Chatbot",
39
  description="Chat with Grok! Type your message below.",
40
  )
41