Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,37 +1,35 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
| 3 |
import requests
|
| 4 |
-
from langchain_groq import ChatGroq
|
| 5 |
-
from langchain_core.prompts import ChatPromptTemplate
|
| 6 |
-
from langchain_core.output_parsers import StrOutputParser
|
| 7 |
|
| 8 |
def preprocess_city_name_and_generate_insight(city_name):
|
| 9 |
-
|
| 10 |
-
if not
|
| 11 |
return city_name, "GROQ API key is not set in the environment variables."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
try:
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
("system", system_message), ("human", human_message)
|
| 21 |
-
]
|
| 22 |
-
)
|
| 23 |
-
chain = prompt | chat | StrOutputParser()
|
| 24 |
-
result = chain.invoke({"text": city_name})
|
| 25 |
-
|
| 26 |
-
insight = result.strip() if result else "No insight generated."
|
| 27 |
except Exception as e:
|
| 28 |
print(f"Error generating insight: {str(e)}")
|
| 29 |
insight = "Could not generate insight due to an error."
|
| 30 |
|
| 31 |
return city_name, insight
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
| 35 |
def get_weather(city_name):
|
| 36 |
corrected_city_name, city_insight = preprocess_city_name_and_generate_insight(city_name)
|
| 37 |
API_Key = os.getenv("OPENWEATHER_API_KEY")
|
|
@@ -64,4 +62,4 @@ iface = gr.Interface(
|
|
| 64 |
)
|
| 65 |
|
| 66 |
# Launching the interface
|
| 67 |
-
iface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
| 3 |
import requests
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
def preprocess_city_name_and_generate_insight(city_name):
|
| 6 |
+
api_key = os.getenv("GROQ_API_KEY")
|
| 7 |
+
if not api_key:
|
| 8 |
return city_name, "GROQ API key is not set in the environment variables."
|
| 9 |
+
|
| 10 |
+
url = "https://api.example.com/generate_insight" # Hypothetical URL
|
| 11 |
+
headers = {
|
| 12 |
+
"Authorization": f"Bearer {api_key}",
|
| 13 |
+
"Content-Type": "application/json"
|
| 14 |
+
}
|
| 15 |
+
data = {
|
| 16 |
+
"model": "llama3-70b-8192",
|
| 17 |
+
"prompt": f"Generate a short, interesting fact about {city_name} in less than 10 words."
|
| 18 |
+
}
|
| 19 |
|
| 20 |
try:
|
| 21 |
+
response = requests.post(url, json=data, headers=headers)
|
| 22 |
+
if response.status_code == 200:
|
| 23 |
+
result = response.json()
|
| 24 |
+
insight = result.get('text', "No insight generated.").strip()
|
| 25 |
+
else:
|
| 26 |
+
insight = "Failed to generate insight due to API error."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
except Exception as e:
|
| 28 |
print(f"Error generating insight: {str(e)}")
|
| 29 |
insight = "Could not generate insight due to an error."
|
| 30 |
|
| 31 |
return city_name, insight
|
| 32 |
|
|
|
|
|
|
|
| 33 |
def get_weather(city_name):
|
| 34 |
corrected_city_name, city_insight = preprocess_city_name_and_generate_insight(city_name)
|
| 35 |
API_Key = os.getenv("OPENWEATHER_API_KEY")
|
|
|
|
| 62 |
)
|
| 63 |
|
| 64 |
# Launching the interface
|
| 65 |
+
iface.launch()
|