Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,29 +1,37 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import requests
|
| 3 |
-
from huggingface_hub import InferenceClient
|
| 4 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
def preprocess_city_name_and_generate_insight(city_name):
|
| 7 |
-
|
| 8 |
-
if not
|
| 9 |
-
return city_name, "
|
| 10 |
-
|
| 11 |
-
try:
|
| 12 |
-
|
| 13 |
-
client = InferenceClient(token=token)
|
| 14 |
|
| 15 |
-
|
|
|
|
| 16 |
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
insight = result
|
| 20 |
except Exception as e:
|
| 21 |
print(f"Error generating insight: {str(e)}")
|
| 22 |
insight = "Could not generate insight due to an error."
|
| 23 |
-
|
| 24 |
return city_name, insight
|
| 25 |
|
| 26 |
|
|
|
|
| 27 |
def get_weather(city_name):
|
| 28 |
corrected_city_name, city_insight = preprocess_city_name_and_generate_insight(city_name)
|
| 29 |
API_Key = os.getenv("OPENWEATHER_API_KEY")
|
|
|
|
| 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 |
+
groq_api_key = os.getenv("GROQ_API_KEY")
|
| 10 |
+
if not groq_api_key:
|
| 11 |
+
return city_name, "GROQ API key is not set in the environment variables."
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
+
try:
|
| 14 |
+
chat = ChatGroq(api_key=groq_api_key, model_name="llama3-70b-8192")
|
| 15 |
|
| 16 |
+
system_message = f"Generate a short, interesting fact about {city_name} in less than 10 words."
|
| 17 |
+
human_message = "{text}"
|
| 18 |
+
prompt = ChatPromptTemplate.from_messages(
|
| 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")
|