ArceusInception commited on
Commit
bd817ed
·
verified ·
1 Parent(s): 989864f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -12
app.py CHANGED
@@ -3,18 +3,18 @@ import requests
3
  import os
4
  from huggingface_hub import InferenceClient
5
 
6
- def preprocess_city_name(city_name):
7
  token = os.getenv("HF_TOKEN")
8
  if not token:
9
- return "Hugging Face token is not set in the environment variables."
10
  client = InferenceClient(model="gpt2", token=token)
11
- result = client(inputs=city_name)
12
- # Assuming the result contains a more standardized city name
13
- corrected_city_name = result['generated_text'].strip()
14
- return corrected_city_name
15
 
16
  def get_weather(city_name):
17
- corrected_city_name = preprocess_city_name(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."
@@ -30,18 +30,19 @@ def get_weather(city_name):
30
  weather_emoji = "☀️" if "clear" in weather else "☁️" if "cloud" in weather else "🌧️" if "rain" in weather else "❄️" if "snow" in weather else "🌫️"
31
  return (f"In {corrected_city_name}, the weather is currently {weather} {weather_emoji}. "
32
  f"The temperature is a comfy {temp}°C 🌡️. "
33
- f"And the humidity levels are at {humidity}%, so keep hydrated! 💧")
 
34
  else:
35
  return "Failed to retrieve data. Please check the city name and try again."
36
 
37
- # Defining Gradio interface
38
  iface = gr.Interface(
39
  fn=get_weather,
40
  inputs=gr.Textbox(label="Enter City Name", placeholder="Type here..."),
41
  outputs=gr.Textbox(label="Weather Update"),
42
- title="Weather App with AI City Name Correction",
43
- description="Enter a city name to get a lively description of the current weather, temperature, and humidity. The city name input is preprocessed by GPT-2 to correct any errors or standardize naming."
44
  )
45
 
46
- # Launching the interface
47
  iface.launch()
 
3
  import os
4
  from huggingface_hub import InferenceClient
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
  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):
17
+ corrected_city_name, city_insight = preprocess_city_name_and_generate_insight(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."
 
30
  weather_emoji = "☀️" if "clear" in weather else "☁️" if "cloud" in weather else "🌧️" if "rain" in weather else "❄️" if "snow" in weather else "🌫️"
31
  return (f"In {corrected_city_name}, the weather is currently {weather} {weather_emoji}. "
32
  f"The temperature is a comfy {temp}°C 🌡️. "
33
+ f"And the humidity levels are at {humidity}% 💧. "
34
+ f"Did you know? {city_insight}")
35
  else:
36
  return "Failed to retrieve data. Please check the city name and try again."
37
 
38
+ # Define Gradio interface
39
  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="WeatherAssistantApp",
44
+ description="Enter a city name to get a lively description of the current weather, temperature, and humidity."
45
  )
46
 
47
+ # Launch the interface
48
  iface.launch()