Spaces:
Paused
Paused
API weather en place
Browse files- Weather.json +1 -2
- app.py +42 -37
- user_location.json +1 -1
- weather.py +1 -1
Weather.json
CHANGED
@@ -1,5 +1,4 @@
|
|
1 |
{
|
2 |
"temperature": 20,
|
3 |
-
"
|
4 |
-
"pression": 1000
|
5 |
}
|
|
|
1 |
{
|
2 |
"temperature": 20,
|
3 |
+
"weather": "sunny"
|
|
|
4 |
}
|
app.py
CHANGED
@@ -5,11 +5,10 @@ from pathlib import Path
|
|
5 |
import plotly.graph_objects as go
|
6 |
import uvicorn
|
7 |
from dotenv import load_dotenv
|
8 |
-
from fastapi import FastAPI
|
9 |
from fastapi.staticfiles import StaticFiles
|
10 |
from mistralai.client import ChatMessage, MistralClient
|
11 |
from pydantic import BaseModel
|
12 |
-
from fastapi import Depends
|
13 |
import json
|
14 |
from fastapi.responses import HTMLResponse, RedirectResponse, Response
|
15 |
from datetime import date
|
@@ -99,8 +98,7 @@ class UserLocation(BaseModel):
|
|
99 |
|
100 |
class Weather(BaseModel):
|
101 |
temperature: float
|
102 |
-
|
103 |
-
pression: float
|
104 |
|
105 |
|
106 |
|
@@ -115,25 +113,32 @@ def load_user_profile():
|
|
115 |
with open('user_profile.json', 'r') as f:
|
116 |
user_profile = json.load(f)
|
117 |
return UserProfile(**user_profile)
|
|
|
118 |
@app.put("/user_profile")
|
119 |
def update_user_profile(user_profile: UserProfile):
|
120 |
with open('user_profile.json', 'w') as f:
|
121 |
json.dump(user_profile.dict(), f)
|
122 |
return user_profile
|
123 |
|
|
|
|
|
124 |
@app.get("/weather")
|
125 |
def load_weather():
|
126 |
with open('Weather.json', 'r') as f:
|
127 |
w = json.load(f)
|
128 |
return Weather(**w)
|
129 |
|
|
|
|
|
|
|
|
|
|
|
130 |
|
131 |
-
#
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
# return RedirectResponse(url='/home')
|
137 |
|
138 |
@app.post("/user_location")
|
139 |
async def set_user_location(user_location: UserLocation):
|
@@ -158,13 +163,6 @@ async def read_meteo(location: str, date: str):
|
|
158 |
|
159 |
|
160 |
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
# On récupère les infos sur la ville. On les sauvegarde dans un fichier JSON
|
168 |
@app.get("/", response_class=HTMLResponse)
|
169 |
async def enter_location():
|
170 |
return """
|
@@ -185,26 +183,37 @@ async def enter_location():
|
|
185 |
body: JSON.stringify({city: city}),
|
186 |
})
|
187 |
.then(response => {
|
188 |
-
if (response.
|
189 |
-
window.location.href =
|
190 |
}
|
191 |
});
|
192 |
});
|
193 |
</script>
|
194 |
"""
|
195 |
-
|
196 |
-
|
197 |
# Home page : using the user profile, display the weather and chat with Mistral AI
|
198 |
@app.get("/home", response_class=HTMLResponse)
|
199 |
-
async def home(user_profile: UserProfile = Depends(load_user_profile),
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
208 |
# create the map
|
209 |
fig = create_world_map(lat, lon)
|
210 |
# save the map as a file
|
@@ -212,20 +221,18 @@ async def home(user_profile: UserProfile = Depends(load_user_profile), Weather:
|
|
212 |
fig.write_html(str(map_file))
|
213 |
# display the map
|
214 |
map_html = f'<iframe src="/static/map.html" width="100%" height="500px"></iframe>'
|
215 |
-
|
216 |
|
217 |
return f"""
|
218 |
<center>
|
219 |
-
<h1>
|
220 |
<div style="display: flex;">
|
221 |
<div style="flex: 50%;">
|
222 |
<h2>Informations du jour</h2>
|
223 |
<p>temperature: {temperature}</p>
|
224 |
-
<p>
|
225 |
-
<p>pression: {pression}</p>
|
226 |
<h2>Map</h2>
|
227 |
{map_html}
|
228 |
-
|
229 |
</div>
|
230 |
<div style="flex: 50%;">
|
231 |
<h2>Avez-vous une question ?</h2>
|
@@ -233,5 +240,3 @@ async def home(user_profile: UserProfile = Depends(load_user_profile), Weather:
|
|
233 |
</div>
|
234 |
</div>
|
235 |
"""
|
236 |
-
# <h2>Gradio Dashboard</h2>
|
237 |
-
# {dashboard_html}
|
|
|
5 |
import plotly.graph_objects as go
|
6 |
import uvicorn
|
7 |
from dotenv import load_dotenv
|
8 |
+
from fastapi import FastAPI, Depends
|
9 |
from fastapi.staticfiles import StaticFiles
|
10 |
from mistralai.client import ChatMessage, MistralClient
|
11 |
from pydantic import BaseModel
|
|
|
12 |
import json
|
13 |
from fastapi.responses import HTMLResponse, RedirectResponse, Response
|
14 |
from datetime import date
|
|
|
98 |
|
99 |
class Weather(BaseModel):
|
100 |
temperature: float
|
101 |
+
weather: str
|
|
|
102 |
|
103 |
|
104 |
|
|
|
113 |
with open('user_profile.json', 'r') as f:
|
114 |
user_profile = json.load(f)
|
115 |
return UserProfile(**user_profile)
|
116 |
+
|
117 |
@app.put("/user_profile")
|
118 |
def update_user_profile(user_profile: UserProfile):
|
119 |
with open('user_profile.json', 'w') as f:
|
120 |
json.dump(user_profile.dict(), f)
|
121 |
return user_profile
|
122 |
|
123 |
+
|
124 |
+
|
125 |
@app.get("/weather")
|
126 |
def load_weather():
|
127 |
with open('Weather.json', 'r') as f:
|
128 |
w = json.load(f)
|
129 |
return Weather(**w)
|
130 |
|
131 |
+
# Load user location
|
132 |
+
def load_user_location():
|
133 |
+
with open('user_location.json', 'r') as f:
|
134 |
+
user_location = json.load(f)
|
135 |
+
return UserLocation(**user_location)
|
136 |
|
137 |
+
# Save weather information
|
138 |
+
def save_weather(weather: Weather):
|
139 |
+
|
140 |
+
with open('Weather.json', 'w') as f:
|
141 |
+
json.dump(weather.dict(), f)
|
|
|
142 |
|
143 |
@app.post("/user_location")
|
144 |
async def set_user_location(user_location: UserLocation):
|
|
|
163 |
|
164 |
|
165 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
166 |
@app.get("/", response_class=HTMLResponse)
|
167 |
async def enter_location():
|
168 |
return """
|
|
|
183 |
body: JSON.stringify({city: city}),
|
184 |
})
|
185 |
.then(response => {
|
186 |
+
if (response.ok) {
|
187 |
+
window.location.href = "/home";
|
188 |
}
|
189 |
});
|
190 |
});
|
191 |
</script>
|
192 |
"""
|
|
|
|
|
193 |
# Home page : using the user profile, display the weather and chat with Mistral AI
|
194 |
@app.get("/home", response_class=HTMLResponse)
|
195 |
+
async def home(user_profile: UserProfile = Depends(load_user_profile), weather: Weather = Depends(load_weather)):
|
196 |
+
|
197 |
+
|
198 |
+
with open('user_location.json', 'r') as f:
|
199 |
+
user_location = json.load(f)
|
200 |
+
# Get weather data for the user location
|
201 |
+
weather_data, lat, lon = get_weather(user_location['city'], today)
|
202 |
+
# Convert the keys to datetime objects
|
203 |
+
weather_times = {datetime.strptime(time, '%Y-%m-%d %H:%M:%S'): info for time, info in weather_data.items()}
|
204 |
+
# Find the time closest to the current time
|
205 |
+
current_time = datetime.now()
|
206 |
+
closest_time = min(weather_times.keys(), key=lambda time: abs(time - current_time))
|
207 |
+
# Extract weather information for the closest time
|
208 |
+
weather_info = weather_times[closest_time]
|
209 |
+
# Extract temperature and weather from the weather information
|
210 |
+
temperature = float(weather_info.split(', ')[1].split('°C')[0].split(': ')[1])
|
211 |
+
weather = weather_info.split(', ')[2].split(': ')[1]
|
212 |
+
# Create a Weather object from the weather data
|
213 |
+
weather = Weather(temperature=temperature, weather=weather)
|
214 |
+
temperature = weather.temperature
|
215 |
+
weather = weather.weather
|
216 |
+
|
217 |
# create the map
|
218 |
fig = create_world_map(lat, lon)
|
219 |
# save the map as a file
|
|
|
221 |
fig.write_html(str(map_file))
|
222 |
# display the map
|
223 |
map_html = f'<iframe src="/static/map.html" width="100%" height="500px"></iframe>'
|
224 |
+
|
225 |
|
226 |
return f"""
|
227 |
<center>
|
228 |
+
<h1>Bienvenue !</h1></center>
|
229 |
<div style="display: flex;">
|
230 |
<div style="flex: 50%;">
|
231 |
<h2>Informations du jour</h2>
|
232 |
<p>temperature: {temperature}</p>
|
233 |
+
<p>weather: {weather}</p>
|
|
|
234 |
<h2>Map</h2>
|
235 |
{map_html}
|
|
|
236 |
</div>
|
237 |
<div style="flex: 50%;">
|
238 |
<h2>Avez-vous une question ?</h2>
|
|
|
240 |
</div>
|
241 |
</div>
|
242 |
"""
|
|
|
|
user_location.json
CHANGED
@@ -1 +1 @@
|
|
1 |
-
{"city": "
|
|
|
1 |
+
{"city": "Gif-sur-Yvette"}
|
weather.py
CHANGED
@@ -30,7 +30,7 @@ def get_weather(city, date):
|
|
30 |
|
31 |
if str(datew).split(' ')[0]==str(date) :
|
32 |
d[str(datew)]=f"Location : {city}, Temperature: {round(temperature-273.15,2)}°C, Weather: {weather_description}"
|
33 |
-
return d
|
34 |
else:
|
35 |
print(f"Error: {forecast_response.status_code}")
|
36 |
|
|
|
30 |
|
31 |
if str(datew).split(' ')[0]==str(date) :
|
32 |
d[str(datew)]=f"Location : {city}, Temperature: {round(temperature-273.15,2)}°C, Weather: {weather_description}"
|
33 |
+
return d, latitude, longitude
|
34 |
else:
|
35 |
print(f"Error: {forecast_response.status_code}")
|
36 |
|