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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -21
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
- 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")
@@ -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()