Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +75 -0
- requirements.txt +6 -0
app.py
ADDED
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import requests
|
3 |
+
import gradio as gr
|
4 |
+
import openai
|
5 |
+
from dotenv import load_dotenv
|
6 |
+
|
7 |
+
# Load environment variables
|
8 |
+
load_dotenv()
|
9 |
+
openai.api_key = os.getenv("OPENAI_API_KEY")
|
10 |
+
|
11 |
+
# Define function to get current weather
|
12 |
+
def get_current_weather(location):
|
13 |
+
weather_api_key = os.getenv("WEATHER_API_KEY")
|
14 |
+
base_url = f"http://api.openweathermap.org/data/2.5/weather?q={location}&appid={weather_api_key}&units=metric"
|
15 |
+
response = requests.get(base_url, timeout=10)
|
16 |
+
response.raise_for_status()
|
17 |
+
data = response.json()
|
18 |
+
return {
|
19 |
+
"location": location,
|
20 |
+
"temperature": data['main']['temp'],
|
21 |
+
"weather": data['weather'][0]['description']
|
22 |
+
}
|
23 |
+
|
24 |
+
# Define chat function
|
25 |
+
def weather_chat(user_message):
|
26 |
+
messages = [
|
27 |
+
{"role": "user", "content": user_message},
|
28 |
+
{"role": "assistant", "content": "You are a weather bot. Answer only in Celsius. If two cities are asked, provide weather for both."}
|
29 |
+
]
|
30 |
+
|
31 |
+
response = openai.ChatCompletion.create(
|
32 |
+
model="gpt-3.5-turbo",
|
33 |
+
temperature=0,
|
34 |
+
max_tokens=256,
|
35 |
+
messages=messages,
|
36 |
+
functions=[
|
37 |
+
{
|
38 |
+
"name": "get_current_weather",
|
39 |
+
"description": "Get the current weather in a given location",
|
40 |
+
"parameters": {
|
41 |
+
"type": "object",
|
42 |
+
"properties": {
|
43 |
+
"location": {"type": "string", "description": "The city, e.g. San Francisco"}
|
44 |
+
},
|
45 |
+
"required": ["location"]
|
46 |
+
}
|
47 |
+
}
|
48 |
+
]
|
49 |
+
)
|
50 |
+
|
51 |
+
function_call = response['choices'][0]['message']['function_call']
|
52 |
+
arguments = eval(function_call['arguments'])
|
53 |
+
weather_data = get_current_weather(arguments['location'])
|
54 |
+
|
55 |
+
messages.append({"role": "assistant", "content": None, "function_call": function_call})
|
56 |
+
messages.append({"role": "function", "name": "get_current_weather", "content": str(weather_data)})
|
57 |
+
|
58 |
+
response = openai.ChatCompletion.create(
|
59 |
+
model="gpt-3.5-turbo",
|
60 |
+
messages=messages
|
61 |
+
)
|
62 |
+
|
63 |
+
return response['choices'][0]['message']['content']
|
64 |
+
|
65 |
+
# Define Gradio interface
|
66 |
+
iface = gr.Interface(
|
67 |
+
fn=weather_chat,
|
68 |
+
inputs=gr.Textbox(label="Weather Queries"),
|
69 |
+
outputs=gr.Textbox(label="Weather Updates"),
|
70 |
+
title="DDS Weather Bot",
|
71 |
+
description="Ask me anything about weather!"
|
72 |
+
)
|
73 |
+
|
74 |
+
# Launch the Gradio interface
|
75 |
+
iface.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
requests==2.28.1
|
2 |
+
gradio
|
3 |
+
openai==0.28
|
4 |
+
python-dotenv==1.0.0
|
5 |
+
httpx==0.21.1
|
6 |
+
httpcore==0.14.7
|