Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,24 +1,58 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
import os
|
|
|
|
|
|
|
| 3 |
import requests
|
| 4 |
-
from
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
if not api_key:
|
| 9 |
-
return city_name, "GROQ API key is not set in the environment variables."
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
return city_name, insight
|
| 23 |
|
| 24 |
def get_weather(city_name):
|
|
@@ -26,10 +60,10 @@ def get_weather(city_name):
|
|
| 26 |
API_Key = os.getenv("OPENWEATHER_API_KEY")
|
| 27 |
if not API_Key:
|
| 28 |
return "API Key is not set. Please set the OPENWEATHER_API_KEY environment variable."
|
| 29 |
-
|
| 30 |
url = f'https://api.openweathermap.org/data/2.5/weather?q={corrected_city_name}&appid={API_Key}&units=metric'
|
| 31 |
response = requests.get(url)
|
| 32 |
-
|
| 33 |
if response.status_code == 200:
|
| 34 |
data = response.json()
|
| 35 |
weather = data['weather'][0]['description']
|
|
@@ -43,7 +77,7 @@ def get_weather(city_name):
|
|
| 43 |
else:
|
| 44 |
return "Failed to retrieve data. Please check the city name and try again."
|
| 45 |
|
| 46 |
-
#
|
| 47 |
iface = gr.Interface(
|
| 48 |
fn=get_weather,
|
| 49 |
inputs=gr.Textbox(label="Enter City Name", placeholder="Type here..."),
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
import json
|
| 3 |
+
import gradio as gr
|
| 4 |
import requests
|
| 5 |
+
from dotenv import load_dotenv
|
| 6 |
+
from groq import Groq # Correct import after installing groq module
|
| 7 |
|
| 8 |
+
# Load environment variables
|
| 9 |
+
load_dotenv()
|
|
|
|
|
|
|
| 10 |
|
| 11 |
+
# Groq client initialization
|
| 12 |
+
client = Groq(api_key=os.getenv("GROQ_API_KEY"))
|
| 13 |
|
| 14 |
+
def preprocess_city_name_and_generate_insight(city_name):
|
| 15 |
+
tools = [
|
| 16 |
+
{
|
| 17 |
+
"type": "function",
|
| 18 |
+
"function": {
|
| 19 |
+
"name": "generate_city_insight",
|
| 20 |
+
"description": "Generate a short, interesting fact about a specified city",
|
| 21 |
+
"parameters": {
|
| 22 |
+
"type": "object",
|
| 23 |
+
"properties": {
|
| 24 |
+
"city_name": {
|
| 25 |
+
"type": "string",
|
| 26 |
+
"description": "The name of the city to generate insights for",
|
| 27 |
+
},
|
| 28 |
+
},
|
| 29 |
+
"required": ["city_name"],
|
| 30 |
+
},
|
| 31 |
+
},
|
| 32 |
+
}
|
| 33 |
+
]
|
| 34 |
|
| 35 |
+
# Simulate a user message asking for insights about a city
|
| 36 |
+
response = client.chat.completions.create(
|
| 37 |
+
model="mixtral-8x7b-32768",
|
| 38 |
+
messages=[
|
| 39 |
+
{
|
| 40 |
+
"role": "user",
|
| 41 |
+
"content": f"What interesting fact can you tell me about {city_name}?",
|
| 42 |
+
}
|
| 43 |
+
],
|
| 44 |
+
temperature=0,
|
| 45 |
+
max_tokens=300,
|
| 46 |
+
tools=tools,
|
| 47 |
+
tool_choice="auto"
|
| 48 |
+
)
|
| 49 |
|
| 50 |
+
# Extract and handle the response
|
| 51 |
+
groq_response = response.choices[0].message
|
| 52 |
+
args = json.loads(groq_response.tool_calls[0].function.arguments)
|
| 53 |
+
|
| 54 |
+
# Assuming generate_city_insight function is defined somewhere that returns an insight
|
| 55 |
+
insight = generate_city_insight(**args)
|
| 56 |
return city_name, insight
|
| 57 |
|
| 58 |
def get_weather(city_name):
|
|
|
|
| 60 |
API_Key = os.getenv("OPENWEATHER_API_KEY")
|
| 61 |
if not API_Key:
|
| 62 |
return "API Key is not set. Please set the OPENWEATHER_API_KEY environment variable."
|
| 63 |
+
|
| 64 |
url = f'https://api.openweathermap.org/data/2.5/weather?q={corrected_city_name}&appid={API_Key}&units=metric'
|
| 65 |
response = requests.get(url)
|
| 66 |
+
|
| 67 |
if response.status_code == 200:
|
| 68 |
data = response.json()
|
| 69 |
weather = data['weather'][0]['description']
|
|
|
|
| 77 |
else:
|
| 78 |
return "Failed to retrieve data. Please check the city name and try again."
|
| 79 |
|
| 80 |
+
# Gradio interface definition
|
| 81 |
iface = gr.Interface(
|
| 82 |
fn=get_weather,
|
| 83 |
inputs=gr.Textbox(label="Enter City Name", placeholder="Type here..."),
|