Memoona648 commited on
Commit
dea9f96
·
verified ·
1 Parent(s): 033f641

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -21
app.py CHANGED
@@ -3,18 +3,16 @@ from huggingface_hub import InferenceClient
3
  import plotly.express as px
4
  from datetime import datetime
5
  import os
6
- import json
7
 
8
  # ------------------- CONFIG -------------------
9
- API_KEY = os.environ["HF_API_KEY"] # Hugging Face secret from Spaces
10
  MODEL_ID = "duongtruongbinh/smolagents-weather-agent"
11
-
12
  client = InferenceClient(API_KEY)
 
13
 
14
  # ------------------- STREAMLIT PAGE -------------------
15
  st.set_page_config(page_title="Weather Forecast App", page_icon="⛅", layout="wide")
16
 
17
- # Dark/Light mode
18
  mode = st.sidebar.radio("Choose Theme", ["Light", "Dark"])
19
  if mode == "Dark":
20
  st.markdown('<style>body {background-color: #0e1117; color: white;}</style>', unsafe_allow_html=True)
@@ -27,19 +25,10 @@ city = st.text_input("🌍 Enter city name", "London")
27
  if city:
28
  with st.spinner("Fetching weather data..."):
29
  try:
30
- # Call Hugging Face text2text API
31
- response_text = client.text2text(MODEL_ID, inputs=f"Weather in {city}")["generated_text"]
32
-
33
- # Attempt to parse JSON from model output
34
- try:
35
- response = json.loads(response_text)
36
- except:
37
- st.error("Unable to parse weather data. Model output may not be JSON.")
38
- st.write("Raw output:", response_text)
39
- response = None
40
-
41
- if response:
42
- # Current weather metrics
43
  st.subheader(f"Current Weather in {city.title()}")
44
  col1, col2, col3, col4 = st.columns(4)
45
  with col1:
@@ -52,12 +41,11 @@ if city:
52
  st.image(response['icon_url'])
53
 
54
  st.write(f"**Condition:** {response['condition']}")
55
-
56
- # 5-day forecast chart
57
  forecast = response['forecast']
58
  dates = [datetime.fromisoformat(f['datetime']).strftime("%d %b") for f in forecast]
59
  temps = [f['temp'] for f in forecast]
60
-
61
  fig = px.line(
62
  x=dates,
63
  y=temps,
@@ -70,6 +58,6 @@ if city:
70
  font_color='white' if mode=='Dark' else 'black'
71
  )
72
  st.plotly_chart(fig, use_container_width=True)
73
-
74
  except Exception as e:
75
  st.error(f"API Error: {e}")
 
3
  import plotly.express as px
4
  from datetime import datetime
5
  import os
 
6
 
7
  # ------------------- CONFIG -------------------
8
+ API_KEY = os.environ["HF_API_KEY"]
9
  MODEL_ID = "duongtruongbinh/smolagents-weather-agent"
 
10
  client = InferenceClient(API_KEY)
11
+ model = client.model(MODEL_ID) # Get callable model
12
 
13
  # ------------------- STREAMLIT PAGE -------------------
14
  st.set_page_config(page_title="Weather Forecast App", page_icon="⛅", layout="wide")
15
 
 
16
  mode = st.sidebar.radio("Choose Theme", ["Light", "Dark"])
17
  if mode == "Dark":
18
  st.markdown('<style>body {background-color: #0e1117; color: white;}</style>', unsafe_allow_html=True)
 
25
  if city:
26
  with st.spinner("Fetching weather data..."):
27
  try:
28
+ response = model({"location": city}) # Correct call
29
+ if "error" in response:
30
+ st.error("City not found or API issue. Try again!")
31
+ else:
 
 
 
 
 
 
 
 
 
32
  st.subheader(f"Current Weather in {city.title()}")
33
  col1, col2, col3, col4 = st.columns(4)
34
  with col1:
 
41
  st.image(response['icon_url'])
42
 
43
  st.write(f"**Condition:** {response['condition']}")
44
+
 
45
  forecast = response['forecast']
46
  dates = [datetime.fromisoformat(f['datetime']).strftime("%d %b") for f in forecast]
47
  temps = [f['temp'] for f in forecast]
48
+
49
  fig = px.line(
50
  x=dates,
51
  y=temps,
 
58
  font_color='white' if mode=='Dark' else 'black'
59
  )
60
  st.plotly_chart(fig, use_container_width=True)
61
+
62
  except Exception as e:
63
  st.error(f"API Error: {e}")