Spaces:
Paused
Paused
Merge branch 'main' into clément
Browse files
app.py
CHANGED
@@ -1,8 +1,64 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
from mistralai.client import MistralClient, ChatMessage
|
3 |
import os
|
4 |
-
|
|
|
|
|
|
|
|
|
5 |
import plotly.graph_objects as go
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
# Load environment variables
|
8 |
load_dotenv()
|
@@ -72,5 +128,10 @@ with gr.Blocks() as demo:
|
|
72 |
update_map_btn.click(create_world_map, [lat, lon], map)
|
73 |
|
74 |
|
|
|
|
|
|
|
|
|
|
|
75 |
if __name__ == "__main__":
|
76 |
-
|
|
|
|
|
|
|
1 |
import os
|
2 |
+
import sys
|
3 |
+
from datetime import datetime
|
4 |
+
from pathlib import Path
|
5 |
+
|
6 |
+
import gradio as gr
|
7 |
import plotly.graph_objects as go
|
8 |
+
import uvicorn
|
9 |
+
from dotenv import load_dotenv
|
10 |
+
from fastapi import FastAPI
|
11 |
+
from fastapi.staticfiles import StaticFiles
|
12 |
+
from mistralai.client import ChatMessage, MistralClient
|
13 |
+
|
14 |
+
# create a FastAPI app
|
15 |
+
app = FastAPI()
|
16 |
+
|
17 |
+
# create a static directory to store the static files
|
18 |
+
static_dir = Path('./static')
|
19 |
+
static_dir.mkdir(parents=True, exist_ok=True)
|
20 |
+
|
21 |
+
# mount FastAPI StaticFiles server
|
22 |
+
app.mount("/static", StaticFiles(directory=static_dir), name="static")
|
23 |
+
|
24 |
+
# Gradio stuff
|
25 |
+
|
26 |
+
|
27 |
+
# def predict(text_input):
|
28 |
+
# file_name = f"{datetime.utcnow().strftime('%s')}.html"
|
29 |
+
# file_path = static_dir / file_name
|
30 |
+
# print(file_path)
|
31 |
+
# with open(file_path, "w") as f:
|
32 |
+
# f.write(f"""
|
33 |
+
# <script src="https://cdn.tailwindcss.com"></script>
|
34 |
+
# <body class="bg-gray-200 dark:text-white dark:bg-gray-900">
|
35 |
+
# <h1 class="text-3xl font-bold">
|
36 |
+
# Hello <i>{text_input}</i> From Gradio Iframe
|
37 |
+
# </h1>
|
38 |
+
# <h3>Filename: {file_name}</h3>
|
39 |
+
# """)
|
40 |
+
# iframe = f"""<iframe src="/static/{file_name}" width="100%" height="500px"></iframe>"""
|
41 |
+
# link = f'<a href="/static/{file_name}" target="_blank">{file_name}</a>'
|
42 |
+
# return link, iframe
|
43 |
+
|
44 |
+
|
45 |
+
# with gr.Blocks() as block:
|
46 |
+
# gr.Markdown("""
|
47 |
+
# ## Gradio + FastAPI + Static Server
|
48 |
+
# This is a demo of how to use Gradio with FastAPI and a static server.
|
49 |
+
# The Gradio app generates dynamic HTML files and stores them in a static directory. FastAPI serves the static files.
|
50 |
+
# """)
|
51 |
+
# with gr.Row():
|
52 |
+
# with gr.Column():
|
53 |
+
# text_input = gr.Textbox(label="Name")
|
54 |
+
# markdown = gr.Markdown(label="Output Box")
|
55 |
+
# new_btn = gr.Button("New")
|
56 |
+
# with gr.Column():
|
57 |
+
# html = gr.HTML(label="HTML preview", show_label=True)
|
58 |
+
|
59 |
+
# new_btn.click(fn=predict, inputs=[text_input], outputs=[markdown, html])
|
60 |
+
|
61 |
+
|
62 |
|
63 |
# Load environment variables
|
64 |
load_dotenv()
|
|
|
128 |
update_map_btn.click(create_world_map, [lat, lon], map)
|
129 |
|
130 |
|
131 |
+
|
132 |
+
# mount Gradio app to FastAPI app
|
133 |
+
app = gr.mount_gradio_app(app, demo, path="/")
|
134 |
+
|
135 |
+
# serve the app
|
136 |
if __name__ == "__main__":
|
137 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|
test.py
DELETED
@@ -1,137 +0,0 @@
|
|
1 |
-
import os
|
2 |
-
import sys
|
3 |
-
from datetime import datetime
|
4 |
-
from pathlib import Path
|
5 |
-
|
6 |
-
import gradio as gr
|
7 |
-
import plotly.graph_objects as go
|
8 |
-
import uvicorn
|
9 |
-
from dotenv import load_dotenv
|
10 |
-
from fastapi import FastAPI
|
11 |
-
from fastapi.staticfiles import StaticFiles
|
12 |
-
from mistralai.client import ChatMessage, MistralClient
|
13 |
-
|
14 |
-
# create a FastAPI app
|
15 |
-
app = FastAPI()
|
16 |
-
|
17 |
-
# create a static directory to store the static files
|
18 |
-
static_dir = Path('./static')
|
19 |
-
static_dir.mkdir(parents=True, exist_ok=True)
|
20 |
-
|
21 |
-
# mount FastAPI StaticFiles server
|
22 |
-
app.mount("/static", StaticFiles(directory=static_dir), name="static")
|
23 |
-
|
24 |
-
# Gradio stuff
|
25 |
-
|
26 |
-
|
27 |
-
# def predict(text_input):
|
28 |
-
# file_name = f"{datetime.utcnow().strftime('%s')}.html"
|
29 |
-
# file_path = static_dir / file_name
|
30 |
-
# print(file_path)
|
31 |
-
# with open(file_path, "w") as f:
|
32 |
-
# f.write(f"""
|
33 |
-
# <script src="https://cdn.tailwindcss.com"></script>
|
34 |
-
# <body class="bg-gray-200 dark:text-white dark:bg-gray-900">
|
35 |
-
# <h1 class="text-3xl font-bold">
|
36 |
-
# Hello <i>{text_input}</i> From Gradio Iframe
|
37 |
-
# </h1>
|
38 |
-
# <h3>Filename: {file_name}</h3>
|
39 |
-
# """)
|
40 |
-
# iframe = f"""<iframe src="/static/{file_name}" width="100%" height="500px"></iframe>"""
|
41 |
-
# link = f'<a href="/static/{file_name}" target="_blank">{file_name}</a>'
|
42 |
-
# return link, iframe
|
43 |
-
|
44 |
-
|
45 |
-
# with gr.Blocks() as block:
|
46 |
-
# gr.Markdown("""
|
47 |
-
# ## Gradio + FastAPI + Static Server
|
48 |
-
# This is a demo of how to use Gradio with FastAPI and a static server.
|
49 |
-
# The Gradio app generates dynamic HTML files and stores them in a static directory. FastAPI serves the static files.
|
50 |
-
# """)
|
51 |
-
# with gr.Row():
|
52 |
-
# with gr.Column():
|
53 |
-
# text_input = gr.Textbox(label="Name")
|
54 |
-
# markdown = gr.Markdown(label="Output Box")
|
55 |
-
# new_btn = gr.Button("New")
|
56 |
-
# with gr.Column():
|
57 |
-
# html = gr.HTML(label="HTML preview", show_label=True)
|
58 |
-
|
59 |
-
# new_btn.click(fn=predict, inputs=[text_input], outputs=[markdown, html])
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
# Load environment variables
|
64 |
-
load_dotenv()
|
65 |
-
api_key = os.getenv('API_KEY')
|
66 |
-
client = MistralClient(api_key=api_key)
|
67 |
-
model = 'mistral-small'
|
68 |
-
|
69 |
-
|
70 |
-
title = "Gaia Mistral Chat Demo"
|
71 |
-
description = "Example of simple chatbot with Gradio and Mistral AI via its API"
|
72 |
-
placeholder = "Posez moi une question sur l'agriculture"
|
73 |
-
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 ?"]
|
74 |
-
|
75 |
-
|
76 |
-
def chat_with_mistral(user_input):
|
77 |
-
messages = [ChatMessage(role="user", content=user_input)]
|
78 |
-
|
79 |
-
chat_response = client.chat(model=model, messages=messages)
|
80 |
-
return chat_response.choices[0].message.content
|
81 |
-
|
82 |
-
def create_world_map(
|
83 |
-
lat=45.5017,
|
84 |
-
lon=-73.5673,
|
85 |
-
):
|
86 |
-
fig = go.Figure(go.Scattermapbox
|
87 |
-
(
|
88 |
-
lat=[lat],
|
89 |
-
lon=[lon],
|
90 |
-
mode='markers',
|
91 |
-
marker=go.scattermapbox.Marker(size=14),
|
92 |
-
text=['Montreal'],
|
93 |
-
))
|
94 |
-
|
95 |
-
fig.update_layout(
|
96 |
-
mapbox_style="open-street-map",
|
97 |
-
hovermode='closest',
|
98 |
-
mapbox=dict(
|
99 |
-
bearing=0,
|
100 |
-
center=go.layout.mapbox.Center(
|
101 |
-
lat=lat,
|
102 |
-
lon=lon,
|
103 |
-
),
|
104 |
-
pitch=0,
|
105 |
-
zoom=5
|
106 |
-
),
|
107 |
-
)
|
108 |
-
|
109 |
-
return fig
|
110 |
-
|
111 |
-
with gr.Blocks() as demo:
|
112 |
-
with gr.Column():
|
113 |
-
with gr.Row():
|
114 |
-
user_input = gr.Textbox(lines=2, placeholder=placeholder)
|
115 |
-
send_chat_btn = gr.Button(value="Send")
|
116 |
-
lat = gr.Number(value=45.5017, label="Latitude")
|
117 |
-
lon = gr.Number(value=-73.5673, label="Longitude")
|
118 |
-
update_map_btn = gr.Button(value="Update Map")
|
119 |
-
|
120 |
-
chat_output = gr.Textbox(lines=2, placeholder="Réponse")
|
121 |
-
|
122 |
-
# map:
|
123 |
-
map = gr.Plot()
|
124 |
-
demo.load(chat_with_mistral, user_input, chat_output)
|
125 |
-
send_chat_btn.click(chat_with_mistral, user_input, chat_output)
|
126 |
-
# map:
|
127 |
-
demo.load(create_world_map, [lat, lon], map)
|
128 |
-
update_map_btn.click(create_world_map, [lat, lon], map)
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
# mount Gradio app to FastAPI app
|
133 |
-
app = gr.mount_gradio_app(app, demo, path="/")
|
134 |
-
|
135 |
-
# serve the app
|
136 |
-
if __name__ == "__main__":
|
137 |
-
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|