Spaces:
Runtime error
Runtime error
Eddyhzd
commited on
Commit
·
b25820d
1
Parent(s):
e6c446b
Fix: Simple chatBot mistral
Browse files
app.py
CHANGED
|
@@ -1,16 +1,38 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
client = MCPClient("csv_analyzer")
|
| 5 |
-
client.connect()
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
#
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from openai import OpenAI
|
| 3 |
+
import os
|
| 4 |
|
| 5 |
+
cle_api = os.environ.get("CLE_API_MISTRAL")
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
# Initialisation du client Mistral (API compatible OpenAI)
|
| 8 |
+
client = OpenAI(api_key=cle_api, base_url="https://api.mistral.ai/v1")
|
| 9 |
|
| 10 |
+
# Chatbot : simple écho Fonction chatbot reliée à Mistral
|
| 11 |
+
def chatbot(message, history):
|
| 12 |
+
# Préparer l’historique dans le format de Mistral
|
| 13 |
+
messages = []
|
| 14 |
+
for user_msg, bot_msg in history:
|
| 15 |
+
messages.append({"role": "user", "content": user_msg})
|
| 16 |
+
messages.append({"role": "assistant", "content": bot_msg})
|
| 17 |
+
|
| 18 |
+
messages.append({"role": "user", "content": message})
|
| 19 |
|
| 20 |
+
# Appel API Mistral
|
| 21 |
+
response = client.chat.completions.create(
|
| 22 |
+
model="mistral-small-latest",
|
| 23 |
+
messages=messages
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
bot_reply = response.choices[0].message.content.strip()
|
| 27 |
+
history.append(("Vous: " + message, "Bot: " + bot_reply))
|
| 28 |
+
return history, history
|
| 29 |
+
|
| 30 |
+
with gr.Blocks() as demo:
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
chatbot_ui = gr.Chatbot(label="ChatBot")
|
| 34 |
+
msg = gr.Textbox(placeholder="Écrivez un message...")
|
| 35 |
+
|
| 36 |
+
msg.submit(chatbot, [msg, chatbot_ui], [chatbot_ui, chatbot_ui])
|
| 37 |
+
|
| 38 |
+
demo.launch()
|