Spaces:
Sleeping
Sleeping
File size: 14,435 Bytes
da4b152 2eec4f9 da4b152 2eec4f9 da4b152 2eec4f9 da4b152 75790fa da4b152 1fb9ebe da4b152 1fb9ebe da4b152 1fb9ebe da4b152 eff2ee3 1fb9ebe da4b152 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 |
import json
import os
from datetime import date, datetime
from pathlib import Path
import plotly.graph_objects as go
from dotenv import load_dotenv
from fastapi import Depends, FastAPI, Request
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import HTMLResponse, Response
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from mistralai.client import ChatMessage, MistralClient
from pydantic import BaseModel
from weather import get_weather
# code that gives the date of today
today = date.today()
today = today.strftime("%Y-%m-%d")
# Hugging face space secret retrieval: FIXME
def create_env_file():
import os
secrets = ['MISTRAL_API_KEY', 'AGRO_API_KEY', 'OPENCAGE_API_KEY']
for secret in secrets:
secret_value = os.environ[secret]
if secret_value is None:
print(f"Please set the environment variable {secret}")
else:
with open('.env', 'a') as f:
f.write(f"{secret}={secret_value}\n")
# Hugging face space secret retrieval: FIXME
production = False
if production:
create_env_file()
# Load environment variables
load_dotenv()
#api_key='Yb2kAF0DR4Mva5AEmoYFV3kYRAKdXB7i'
#client = MistralClient(api_key=api_key)
model = 'mistral-small'
title = "Gaia Mistral Chat Demo"
description = "Example of simple chatbot with Gradio and Mistral AI via its API"
placeholder = "Posez moi une question sur l'agriculture"
examples = ["Comment fait on pour produire du maïs ?",
"Rédige moi une lettre pour faire un stage dans une exploitation agricole", "Comment reprendre une exploitation agricole ?"]
def create_prompt_system():
prompt = "Ton rôle: Assistant agricole\n\n"
prompt += "Ton objectif: Aider les agriculteurs dans leur recherche d'information. Tu peux répondre à des questions sur l'agriculture, donner des conseils, ou aider à rédiger des documents administratifs.\n\n"
prompt += "Ton public: Agriculteurs, étudiants en agriculture, personnes en reconversion professionnelle, ou toute personne ayant des questions sur l'agriculture.\n\n"
prompt += "Ton style: Tu es professionnel, bienveillant, et tu as une connaissance approfondie de l'agriculture. Tu réponds uniquement en français et de manière semi-concise. Tu es capable de répondre à des questions techniques, mais tu sais aussi t'adapter à des personnes qui ne connaissent pas bien le domaine.\n\n"
prompt += "Tu disposes du contexte suivant pour répondre aux questions:\n\n"
# load all the json files in the root
for file in Path('.').glob('*.json'):
with open(file, 'r') as f:
prompt += f"Contexte: {file.stem}\n\n"
# convert the json to a string using the json module
file_content = json.load(f)
prompt += json.dumps(file_content, indent=4)
prompt += "\n\n"
prompt += "Tu es prêt à répondre aux questions ?\n\n"
return prompt
def chat_with_mistral(user_input):
#messages = [ChatMessage(role="user", content=user_input)]
#chat_response = client.chat(model=model, messages=messages)
return 'Donne moi une clé API valide et je te réponds volontiers ;-)' #chat_response.choices[0].message.content
# create a FastAPI app
app = FastAPI()
# Add CORS middleware
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # Allows all origins
allow_credentials=True,
allow_methods=["*"], # Allows all methods
allow_headers=["*"], # Allows all headers
)
# create a static directory to store the static files
static_dir = Path('./static')
static_dir.mkdir(parents=True, exist_ok=True)
# mount FastAPI StaticFiles server
app.mount("/static", StaticFiles(directory=static_dir), name="static")
# templating
templates = Jinja2Templates(directory="static")
with open('data/departements.geojson', 'r') as f:
departements = json.load(f)
with open('data/regions.geojson', 'r') as f:
regions = json.load(f)
with open('videos.json', 'r') as f:
videos = json.load(f)
def create_world_map(lat, lon, dpmts=False, rgns= False):
fig = go.Figure()
for video_id, video_info in videos.items():
name = video_info['name']
url = video_info['url']
location = video_info['location']
video_lat = video_info['lat']
video_lon = video_info['lon']
fig.add_trace(go.Scattermapbox(
mode='markers',
lon=[video_lon],
lat=[video_lat],
marker=go.scattermapbox.Marker(
size=14,
color='blue'
),
text=name,
hoverinfo='text',
showlegend=False,
visible=True,
))
if dpmts :
for feature_departement in departements['features']:
if feature_departement['geometry']['type'] == 'Polygon':
coords = feature_departement['geometry']['coordinates'][0]
lons, lats = zip(*coords)
lons = list(lons)
lats = list(lats)
fig.add_trace(go.Scattermapbox(
mode='lines',
lon=lons + [lons[0]],
lat=lats + [lats[0]],
marker=go.scattermapbox.Marker(
size=14
),
text=feature_departement['properties']['nom'],
hoverinfo='text',
line=dict(
color='blue',
width=1
),
showlegend=False,
visible=True,
))
if rgns:
for feature_region in regions['features']:
if feature_region['geometry']['type'] == 'Polygon':
coords = feature_region['geometry']['coordinates'][0]
lons, lats = zip(*coords)
lons = list(lons)
lats = list(lats)
fig.add_trace(go.Scattermapbox(
mode='lines',
lon=lons + [lons[0]],
lat=lats + [lats[0]],
hoverinfo='text',
line=dict(
color='red', # Set the line color to red
width=1, # Set the width of the line
),
showlegend=False,
visible=True,
))
fig.add_trace(go.Scattermapbox(
lat=[lat],
lon=[lon],
mode='markers',
marker=go.scattermapbox.Marker(size=14, color='red'),
text=['Location'],
showlegend=False,
hoverinfo='none'
))
fig.update_layout(
autosize=True,
plot_bgcolor='rgba(0,0,0,0)',
paper_bgcolor='rgba(0,0,0,0)',
mapbox_style="open-street-map",
hovermode='closest',
mapbox=dict(
bearing=0,
center=go.layout.mapbox.Center(
lat=lat,
lon=lon,
),
pitch=0,
zoom=5
),
)
return fig
# Profile stuff
class UserProfile(BaseModel):
name: str
age: int
location: str
lat: float
lon: float
class UserLocation(BaseModel):
city: str
class Weather(BaseModel):
temperature: float
weather: str
@app.post("/user_profile")
def save_user_profile(user_profile: UserProfile):
with open('user_profile.json', 'w') as f:
json.dump(user_profile.dict(), f)
return user_profile.dict()
@app.get("/user_profile")
def load_user_profile():
with open('user_profile.json', 'r') as f:
user_profile = json.load(f)
return UserProfile(**user_profile)
@app.put("/user_profile")
def update_user_profile(user_profile: UserProfile):
with open('user_profile.json', 'w') as f:
json.dump(user_profile.dict(), f)
return user_profile
@app.get("/weather")
def load_weather():
with open('Weather.json', 'r') as f:
w = json.load(f)
return Weather(**w)
# Load user location
def load_user_location():
with open('user_location.json', 'r') as f:
user_location = json.load(f)
return UserLocation(**user_location)
# Save weather information
def save_weather(weather: Weather):
with open('Weather.json', 'w') as f:
json.dump(weather.dict(), f)
@app.post("/user_location")
async def set_user_location(user_location: UserLocation):
# Save the user location as a JSON file
with open('user_location.json', 'w') as f:
json.dump(user_location.dict(), f)
response = Response(headers={"Location": "/home"})
response.status_code = 303
return response
# load user profile on startup
user_profile = load_user_profile()
weather = load_weather()
@app.get("/", response_class=HTMLResponse)
async def enter_location():
return """
<html>
<head>
<style>
body {
background-image: url('https://lh3.googleusercontent.com/JKE8WODi6oggtccvEyMnYswDLqSVjDv4FqIGec2qF1doGXf3HTJ5MnMHqG-thNklmxKO6aGf23XiZAFwbaSxt4sTyWc-IT-zyH6aQA=rj-w0-h1600-l80');
background-size: cover;
color: white; /* make all the text white */
font-size: 20px; /* increase the font size */
font-family: sans-serif; /* change the font to Arial */
}
img {
display: block;
margin-left: auto;
margin-right: auto;
width: 50%;
}
input[type="text"], input[type="submit"] {
width: 100%;
padding: 10px 20px;
margin: 10px 0;
box-sizing: border-box;
border: none;
border-radius: 4px;
background-color: #f8f8f8;
font-size: 16px;
}
</style>
</head>
<body>
<h2>Bienvenue sur votre AI-dashboard connecté</h2>
<p>Connectez-vous et accédez à des informations personnalisées !</p>
<h2> Fonctionnalités</h2>
<ul>
<li> Parlez avec votre assistant Mistral</li>
<li> Obtenez des renseignements météo</li>
<li> Partagez des informations avec les agriculteurs de votre région </li>
<li> Générer une newsletter personnalisée grâce à Mistral</li>
</ul>
<form id="locationForm">
<label for="city">Votre ville :</label><br>
<input type="text" id="city" name="city"><br>
<input type="submit" value="Submit">
</form>
<img src="https://next.ink/wp-content/uploads/2024/02/announcing-mistral.png" alt="Mistral Logo" style="display: block; margin-left: auto; margin-right: auto; width: 50%;">
<div>
<p>© 2024 AgriHackteurs</p>
</div>
</body>
<script>
document.getElementById('locationForm').addEventListener('submit', function(event) {
event.preventDefault();
var city = document.getElementById('city').value;
fetch('/user_location', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({city: city}),
})
.then(response => {
if (response.ok) {
window.location.href = "/home";
}
});
});
</script>
"""
# Home page : using the user profile, display the weather and chat with Mistral AI
@app.get("/home", response_class=HTMLResponse)
async def home(
request: Request,
user_profile: UserProfile = Depends(load_user_profile),
weather: Weather = Depends(load_weather),
):
with open('user_location.json', 'r') as f:
user_location = json.load(f)
# Get weather data for the user location
weather_data, lat, lon = get_weather(user_location['city'], today)
# Convert the keys to datetime objects
weather_times = {datetime.strptime(
time, '%Y-%m-%d %H:%M:%S'): info for time, info in weather_data.items()}
# Find the time closest to the current time
current_time = datetime.now()
#closest_time = min(weather_times.keys(),
# key=lambda time: abs(time - current_time))
# Extract weather information for the closest time
#weather_info = weather_times[closest_time]
# Extract temperature and weather from the weather information
#temperature = float(weather_info.split(
# ', ')[1].split('°C')[0].split(': ')[1])
#weather = weather_info.split(', ')[2].split(': ')[1]
# Create a Weather object from the weather data
temperature = float(20)
weather = 'Sunny'
weather = Weather(temperature=temperature, weather=weather)
temperature = weather.temperature
weather = weather.weather
# create the map
fig = create_world_map(lat, lon, dpmts=True, rgns=True)
# save the map as a file
map_file = static_dir / "map.html"
fig.write_html(str(map_file), config={'displayModeBar': False})
# display the map
map_html = f'<iframe src="/static/map.html" width="100%" height="100%" ></iframe>'
# initialize the chatbot with the system prompt
system_prompt = create_prompt_system()
chat_with_mistral(system_prompt)
return templates.TemplateResponse("layout.html", {"request": request, "user_profile": user_profile, "weather": weather, "map_html": map_html})
class ChatInput(BaseModel):
user_input: str
@app.post("/chat")
async def chat(chat_input: ChatInput):
print(chat_input.user_input)
return chat_with_mistral(chat_input.user_input)
# summarize all the information from the json files
@app.get("/report")
async def report():
# load all the json files in the root
report = ""
for file in Path('.').glob('*.json'):
with open(file, 'r') as f:
report += f"Contexte: {file.stem}\n\n"
# convert the json to a string using the json module
file_content = json.load(f)
report += json.dumps(file_content, indent=4)
report += "\n\n"
report += "Synthétise les informations pour l'utilisateur : \n\n"
# ask mistral to summarize the report
chat_response = client.chat(model=model, messages=[
ChatMessage(role="user", content=report)])
return chat_response.choices[0].message.content
|