ArceusInception commited on
Commit
41b14c2
·
verified ·
1 Parent(s): 16ce4ec

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -13
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
- token = os.getenv("HF_TOKEN")
8
- if not token:
9
- return city_name, "Hugging Face token is not set in the environment variables."
10
-
11
- try:
12
-
13
- client = InferenceClient(token=token)
14
 
15
- prompt = f"Generate a short, interesting fact about {city_name} in less than 10 words."
 
16
 
17
- result = client(inputs=prompt, model_id="gpt2")
 
 
 
 
 
 
 
 
18
 
19
- insight = result['generated_text'].strip() if 'generated_text' in result else "No insight generated."
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")