Spaces:
Paused
Paused
File size: 4,170 Bytes
8f27bd5 c593df6 aeaf613 c593df6 c858ae5 c593df6 8f27bd5 aeaf613 8f27bd5 aeaf613 f8f1f43 8f27bd5 aeaf613 8f27bd5 e107fb2 aeaf613 8e131a9 aeaf613 754727f aeaf613 8e131a9 aeaf613 754727f e107fb2 c593df6 c858ae5 c593df6 e107fb2 c593df6 |
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 |
import os
import sys
from datetime import datetime
from pathlib import Path
import gradio as gr
import plotly.graph_objects as go
import uvicorn
from dotenv import load_dotenv
from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles
from mistralai.client import ChatMessage, MistralClient
# create a FastAPI app
app = FastAPI()
# 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")
# Gradio stuff
# def predict(text_input):
# file_name = f"{datetime.utcnow().strftime('%s')}.html"
# file_path = static_dir / file_name
# print(file_path)
# with open(file_path, "w") as f:
# f.write(f"""
# <script src="https://cdn.tailwindcss.com"></script>
# <body class="bg-gray-200 dark:text-white dark:bg-gray-900">
# <h1 class="text-3xl font-bold">
# Hello <i>{text_input}</i> From Gradio Iframe
# </h1>
# <h3>Filename: {file_name}</h3>
# """)
# iframe = f"""<iframe src="/static/{file_name}" width="100%" height="500px"></iframe>"""
# link = f'<a href="/static/{file_name}" target="_blank">{file_name}</a>'
# return link, iframe
# with gr.Blocks() as block:
# gr.Markdown("""
# ## Gradio + FastAPI + Static Server
# This is a demo of how to use Gradio with FastAPI and a static server.
# The Gradio app generates dynamic HTML files and stores them in a static directory. FastAPI serves the static files.
# """)
# with gr.Row():
# with gr.Column():
# text_input = gr.Textbox(label="Name")
# markdown = gr.Markdown(label="Output Box")
# new_btn = gr.Button("New")
# with gr.Column():
# html = gr.HTML(label="HTML preview", show_label=True)
# new_btn.click(fn=predict, inputs=[text_input], outputs=[markdown, html])
# Load environment variables
load_dotenv()
api_key = os.getenv('API_KEY')
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 chat_with_mistral(user_input):
messages = [ChatMessage(role="user", content=user_input)]
chat_response = client.chat(model=model, messages=messages)
return chat_response.choices[0].message.content
def create_world_map(
lat=45.5017,
lon=-73.5673,
):
fig = go.Figure(go.Scattermapbox
(
lat=[lat],
lon=[lon],
mode='markers',
marker=go.scattermapbox.Marker(size=14),
text=['Montreal'],
))
fig.update_layout(
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
with gr.Blocks() as demo:
with gr.Column():
with gr.Row():
user_input = gr.Textbox(lines=2, placeholder=placeholder)
send_chat_btn = gr.Button(value="Send")
lat = gr.Number(value=45.5017, label="Latitude")
lon = gr.Number(value=-73.5673, label="Longitude")
update_map_btn = gr.Button(value="Update Map")
chat_output = gr.Textbox(lines=2, placeholder="Réponse")
# map:
map = gr.Plot()
demo.load(chat_with_mistral, user_input, chat_output)
send_chat_btn.click(chat_with_mistral, user_input, chat_output)
# map:
demo.load(create_world_map, [lat, lon], map)
update_map_btn.click(create_world_map, [lat, lon], map)
# mount Gradio app to FastAPI app
app = gr.mount_gradio_app(app, demo, path="/")
# serve the app
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=7860)
|