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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -6
app.py CHANGED
@@ -1,13 +1,25 @@
1
  import gradio as gr
2
  import requests
3
  import os
 
 
 
 
 
 
 
 
 
 
 
4
 
5
  def get_weather(city_name):
 
6
  API_Key = os.getenv("OPENWEATHER_API_KEY")
7
  if not API_Key:
8
  return "API Key is not set. Please set the OPENWEATHER_API_KEY environment variable."
9
 
10
- url = f'https://api.openweathermap.org/data/2.5/weather?q={city_name}&appid={API_Key}&units=metric'
11
  response = requests.get(url)
12
 
13
  if response.status_code == 200:
@@ -15,11 +27,10 @@ def get_weather(city_name):
15
  weather = data['weather'][0]['description']
16
  temp = data['main']['temp']
17
  humidity = data['main']['humidity']
18
-
19
  weather_emoji = "☀️" if "clear" in weather else "☁️" if "cloud" in weather else "🌧️" if "rain" in weather else "❄️" if "snow" in weather else "🌫️"
20
- return (f"In {city_name}, the weather is currently {weather} {weather_emoji}. "
21
  f"The temperature is a comfy {temp}°C 🌡️. "
22
- f"Also, humidity levels are at {humidity}%, so keep hydrated! 💧")
23
  else:
24
  return "Failed to retrieve data. Please check the city name and try again."
25
 
@@ -28,8 +39,8 @@ iface = gr.Interface(
28
  fn=get_weather,
29
  inputs=gr.Textbox(label="Enter City Name", placeholder="Type here..."),
30
  outputs=gr.Textbox(label="Weather Update"),
31
- title="WeatherAssistantApp",
32
- description="Enter a city name to get a lively description of the current weather, temperature, and humidity."
33
  )
34
 
35
  # Launching the interface
 
1
  import gradio as gr
2
  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."
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
 
25
  if response.status_code == 200:
 
27
  weather = data['weather'][0]['description']
28
  temp = data['main']['temp']
29
  humidity = data['main']['humidity']
 
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
 
 
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