ArceusInception commited on
Commit
00b3535
·
verified ·
1 Parent(s): 3865fc3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -7
app.py CHANGED
@@ -1,20 +1,21 @@
1
  import gradio as gr
2
  import requests
3
  import os
4
- from huggingface_hub import InferenceApi # Correct import for inference
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
- # Using the InferenceApi class correctly
12
- client = InferenceApi("gpt2", token)
13
  prompt = f"Generate a short, interesting fact about {city_name} in less than 10 words:"
14
-
15
  try:
16
- result = client(predict=prompt) # Using predict or the correct method
17
- insight = result[0]['generated_text'].strip() if result else "No insight generated."
 
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")