Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,21 +1,21 @@
|
|
| 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 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
except Exception as e:
|
| 20 |
print(f"Error generating insight: {str(e)}")
|
| 21 |
insight = "Could not generate insight due to an error."
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
|
|
|
| 3 |
from huggingface_hub import InferenceClient
|
| 4 |
+
import os
|
| 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 |
try:
|
| 12 |
+
client = InferenceClient(model_id="gpt2", api_token=token)
|
| 13 |
+
# Construct the prompt for GPT-2
|
| 14 |
+
prompt = f"Generate a short, interesting fact about {city_name} in less than 10 words."
|
| 15 |
+
# Make the prediction request
|
| 16 |
+
result = client.predict(inputs=prompt)
|
| 17 |
+
# Process the result
|
| 18 |
+
insight = result[0]['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."
|