decodingdatascience commited on
Commit
cd4535c
1 Parent(s): 4b8bc21

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +99 -0
  2. requirements.txt +6 -0
app.py ADDED
@@ -0,0 +1,99 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
10
+ # Initialize OpenAI client
11
+ openai.api_key = os.getenv("OPENAI_API_KEY1")
12
+
13
+ # Define function to get current weather
14
+ def get_current_weather(location, unit='celsius'):
15
+ weather_api_key = os.getenv("WEATHER_API_KEY")
16
+ base_url = f"http://api.openweathermap.org/data/2.5/weather?q={location}&appid={weather_api_key}&units=metric"
17
+ try:
18
+ response = requests.get(base_url, timeout=10) # Increased timeout for the request
19
+ response.raise_for_status() # Check if the request was successful
20
+ data = response.json()
21
+ weather_description = data['weather'][0]['description']
22
+ return {
23
+ "location": location,
24
+ "temperature": data['main']['temp'],
25
+ "weather": weather_description
26
+ }
27
+ except requests.exceptions.Timeout:
28
+ return {"error": "Request timed out. Please try again later."}
29
+ except requests.exceptions.RequestException as e:
30
+ print(f"Request failed: {e}")
31
+ return {"error": str(e)}
32
+
33
+ # Function definition and initial message handling
34
+ def weather_chat(user_message):
35
+ messages = []
36
+ messages.append({"role": "user", "content": user_message})
37
+ messages.append({"role": "assistant", "content": "You are a weather bot. Answer only in Celsius. If two cities are asked, provide weather for both."})
38
+
39
+ # Sending initial message to OpenAI
40
+ try:
41
+ response = openai.ChatCompletion.create(
42
+ model="gpt-3.5-turbo",
43
+ temperature=0,
44
+ max_tokens=256,
45
+ top_p=1,
46
+ frequency_penalty=0,
47
+ presence_penalty=0,
48
+ messages=messages,
49
+ functions=[
50
+ {
51
+ "name": "get_current_weather",
52
+ "description": "Get the current weather in a given location",
53
+ "parameters": {
54
+ "type": "object",
55
+ "properties": {
56
+ "location": {"type": "string", "description": "The city, e.g. San Francisco"},
57
+ "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]}
58
+ },
59
+ "required": ["location"]
60
+ }
61
+ }
62
+ ]
63
+ )
64
+ except Exception as e:
65
+ print(f"OpenAI API call failed: {e}")
66
+ return "Failed to communicate with the OpenAI API. Please try again later."
67
+
68
+ # Handling function calls and fetching weather data
69
+ try:
70
+ function_call = response['choices'][0]['message']['function_call']
71
+ arguments = eval(function_call['arguments'])
72
+ weather_data = get_current_weather(arguments['location'])
73
+ if 'error' in weather_data:
74
+ return weather_data['error']
75
+ messages.append({"role": "assistant", "content": None, "function_call": {"name": "get_current_weather", "arguments": str(arguments)}})
76
+ messages.append({"role": "function", "name": "get_current_weather", "content": str(weather_data)})
77
+
78
+ # Continue conversation with weather data
79
+ response = openai.ChatCompletion.create(
80
+ model="gpt-3.5-turbo",
81
+ messages=messages
82
+ )
83
+
84
+ return response['choices'][0]['message']['content']
85
+ except Exception as e:
86
+ print(f"Error during processing: {e}")
87
+ return "I'm here to provide weather updates. Please ask me questions related to weather."
88
+
89
+ # Define Gradio interface
90
+ iface = gr.Interface(
91
+ fn=weather_chat,
92
+ inputs=gr.Textbox(label="Weather Queries"),
93
+ outputs=gr.Textbox(label="Weather Updates"),
94
+ title="DDS Weather Bot",
95
+ description="Ask me anything about weather!"
96
+ )
97
+
98
+ # Launch the Gradio interface
99
+ iface.launch(share=True)
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