Spaces:
Runtime error
Runtime error
Eddyhzd
commited on
Commit
·
6284dca
1
Parent(s):
e360f54
test
Browse files
app.py
CHANGED
|
@@ -1,37 +1,67 @@
|
|
| 1 |
-
import asyncio
|
| 2 |
import gradio as gr
|
| 3 |
-
from
|
| 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 |
with gr.Blocks() as demo:
|
| 29 |
-
gr.Markdown("# 🚀 Client MCP avec Gradio")
|
| 30 |
-
inp = gr.Textbox(label="Votre prompt")
|
| 31 |
-
out = gr.Textbox(label="Réponse du MCP")
|
| 32 |
-
btn = gr.Button("Envoyer")
|
| 33 |
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
-
|
| 37 |
-
demo.launch()
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from openai import OpenAI
|
| 3 |
+
import os
|
| 4 |
+
import json
|
| 5 |
+
import requests
|
| 6 |
+
|
| 7 |
+
cle_api = os.environ.get("CLE_API_MISTRAL")
|
| 8 |
+
MCP_URL = "https://hackathoncra-gradio-mcp.hf.space/gradio_api/mcp/"
|
| 9 |
+
|
| 10 |
+
# Initialisation du client Mistral (API compatible OpenAI)
|
| 11 |
+
clientLLM = OpenAI(api_key=cle_api, base_url="https://api.mistral.ai/v1")
|
| 12 |
+
|
| 13 |
+
def call_mcp(payload: dict):
|
| 14 |
+
"""Appel simple au serveur MCP Gradio"""
|
| 15 |
+
headers = {"Content-Type": "application/json"}
|
| 16 |
+
response = requests.post(MCP_URL, data=json.dumps(payload), headers=headers)
|
| 17 |
+
response.raise_for_status()
|
| 18 |
+
return response.json()
|
| 19 |
+
|
| 20 |
+
# Chatbot avec Mistral + MCP
|
| 21 |
+
def chatbot(message, history):
|
| 22 |
+
# Préparer l’historique pour Mistral
|
| 23 |
+
messages = []
|
| 24 |
+
for user_msg, bot_msg in history:
|
| 25 |
+
messages.append({"role": "user", "content": user_msg})
|
| 26 |
+
messages.append({"role": "assistant", "content": bot_msg})
|
| 27 |
+
|
| 28 |
+
messages.append({"role": "user", "content": message})
|
| 29 |
+
|
| 30 |
+
# Appel API Mistral
|
| 31 |
+
response = clientLLM.chat.completions.create(
|
| 32 |
+
model="mistral-small-latest",
|
| 33 |
+
messages=messages
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
bot_reply = response.choices[0].message.content.strip()
|
| 37 |
+
|
| 38 |
+
# Vérifier si la réponse contient un JSON MCP
|
| 39 |
+
try:
|
| 40 |
+
mcp_payload = json.loads(bot_reply)
|
| 41 |
+
mcp_result = call_mcp(mcp_payload)
|
| 42 |
+
bot_reply = f"Réponse via MCP:\n{json.dumps(mcp_result, indent=2)}"
|
| 43 |
+
except json.JSONDecodeError:
|
| 44 |
+
# Pas de JSON MCP, réponse normale
|
| 45 |
+
pass
|
| 46 |
+
|
| 47 |
+
history.append(("Vous: " + message, "Bot: " + bot_reply))
|
| 48 |
+
return history, history
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
def call_mcp(payload: dict):
|
| 52 |
+
"""
|
| 53 |
+
Fonction générique pour interroger le serveur MCP hébergé sur Gradio.
|
| 54 |
+
"""
|
| 55 |
+
headers = {"Content-Type": "application/json"}
|
| 56 |
+
response = requests.post(MCP_URL, data=json.dumps(payload), headers=headers)
|
| 57 |
+
response.raise_for_status()
|
| 58 |
+
return response.json()
|
| 59 |
+
|
| 60 |
with gr.Blocks() as demo:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
+
chatbot_ui = gr.Chatbot(label="ChatBot")
|
| 63 |
+
msg = gr.Textbox(placeholder="Écrivez un message...")
|
| 64 |
+
|
| 65 |
+
msg.submit(chatbot, [msg, chatbot_ui], [chatbot_ui, chatbot_ui])
|
| 66 |
|
| 67 |
+
demo.launch(debug=True)
|
|
|