ArceusInception commited on
Commit
8af5b38
·
verified ·
1 Parent(s): 9d3c1c5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -5
app.py CHANGED
@@ -9,8 +9,12 @@ def preprocess_city_name_and_generate_insight(city_name):
9
  return city_name, "Hugging Face token is not set in the environment variables."
10
  client = InferenceClient(model="gpt2", token=token)
11
  prompt = f"Generate a short, interesting fact about {city_name} in less than 10 words:"
12
- result = client(inputs=prompt)
13
- insight = result['generated_text'].strip()
 
 
 
 
14
  return city_name, insight
15
 
16
  def get_weather(city_name):
@@ -18,7 +22,7 @@ def get_weather(city_name):
18
  API_Key = os.getenv("OPENWEATHER_API_KEY")
19
  if not API_Key:
20
  return "API Key is not set. Please set the OPENWEATHER_API_KEY environment variable."
21
-
22
  url = f'https://api.openweathermap.org/data/2.5/weather?q={corrected_city_name}&appid={API_Key}&units=metric'
23
  response = requests.get(url)
24
 
@@ -40,8 +44,8 @@ iface = gr.Interface(
40
  fn=get_weather,
41
  inputs=gr.Textbox(label="Enter City Name", placeholder="Type here..."),
42
  outputs=gr.Textbox(label="Weather Update"),
43
- title="Weather App with AI City Insights",
44
- description="Enter a city name to get a lively description of the current weather, temperature, and humidity, plus an interesting fact about the city, all generated by AI."
45
  )
46
 
47
  # Launching the interface
 
9
  return city_name, "Hugging Face token is not set in the environment variables."
10
  client = InferenceClient(model="gpt2", token=token)
11
  prompt = f"Generate a short, interesting fact about {city_name} in less than 10 words:"
12
+ try:
13
+ result = client.call(inputs=prompt) # Correct method to make an inference call
14
+ # Assuming the GPT-2 model completes the sentence in a meaningful way
15
+ insight = result[0]['generated_text'].strip()
16
+ except Exception as e:
17
+ insight = "Could not generate insight due to an error."
18
  return city_name, insight
19
 
20
  def get_weather(city_name):
 
22
  API_Key = os.getenv("OPENWEATHER_API_KEY")
23
  if not API_Key:
24
  return "API Key is not set. Please set the OPENWEATHER_API_KEY environment variable."
25
+
26
  url = f'https://api.openweathermap.org/data/2.5/weather?q={corrected_city_name}&appid={API_Key}&units=metric'
27
  response = requests.get(url)
28
 
 
44
  fn=get_weather,
45
  inputs=gr.Textbox(label="Enter City Name", placeholder="Type here..."),
46
  outputs=gr.Textbox(label="Weather Update"),
47
+ title="WeatherAssistantApp",
48
+ description="Enter a city name to get a lively description of the current weather, temperature, and humidity."
49
  )
50
 
51
  # Launching the interface