Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,20 +1,21 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
import os
|
| 4 |
-
from huggingface_hub import
|
| 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 |
-
#
|
| 12 |
-
client =
|
| 13 |
prompt = f"Generate a short, interesting fact about {city_name} in less than 10 words:"
|
| 14 |
-
|
| 15 |
try:
|
| 16 |
-
|
| 17 |
-
|
|
|
|
| 18 |
except Exception as e:
|
| 19 |
print(f"Error generating insight: {str(e)}")
|
| 20 |
insight = "Could not generate insight due to an error."
|
|
@@ -22,6 +23,7 @@ def preprocess_city_name_and_generate_insight(city_name):
|
|
| 22 |
return city_name, insight
|
| 23 |
|
| 24 |
|
|
|
|
| 25 |
def get_weather(city_name):
|
| 26 |
corrected_city_name, city_insight = preprocess_city_name_and_generate_insight(city_name)
|
| 27 |
API_Key = os.getenv("OPENWEATHER_API_KEY")
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 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 |
+
|
| 11 |
+
# Correctly initializing InferenceClient with the token
|
| 12 |
+
client = InferenceClient("gpt2", token=token)
|
| 13 |
prompt = f"Generate a short, interesting fact about {city_name} in less than 10 words:"
|
| 14 |
+
|
| 15 |
try:
|
| 16 |
+
# Assuming the GPT-2 model expects a key named 'inputs' for its input
|
| 17 |
+
result = client(predict={"inputs": prompt})
|
| 18 |
+
insight = result['generated_text'].strip() if result else "No insight generated."
|
| 19 |
except Exception as e:
|
| 20 |
print(f"Error generating insight: {str(e)}")
|
| 21 |
insight = "Could not generate insight due to an error."
|
|
|
|
| 23 |
return city_name, insight
|
| 24 |
|
| 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")
|