Eddyhzd commited on
Commit
6284dca
·
1 Parent(s): e360f54
Files changed (1) hide show
  1. app.py +63 -33
app.py CHANGED
@@ -1,37 +1,67 @@
1
- import asyncio
2
  import gradio as gr
3
- from mcp import ClientSession
4
-
5
- # Exemple de config MCP
6
- MCP_SERVERS = {
7
- "gradio": {
8
- "url": "https://hackathoncra-gradio-mcp.hf.space/gradio_api/mcp/"
9
- }
10
- }
11
-
12
- async def query_mcp(prompt: str):
13
- # Crée une session MCP (ici en HTTP)
14
- async with ClientSession("https://hackathoncra-gradio-mcp.hf.space/gradio_api/mcp/") as session:
15
- # Exécute une requête MCP (par exemple un appel "text-generation")
16
- print(session)
17
- response = await session.call(
18
- method="text-generation", # dépend des méthodes exposées par ton serveur
19
- params={"prompt": prompt}
20
- )
21
- return response.get("result", "Pas de résultat MCP")
22
-
23
- # Fonction Gradio qui appelle MCP
24
- def chat_with_mcp(prompt):
25
- return asyncio.run(query_mcp(prompt))
26
-
27
- # Interface Gradio
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
- btn.click(chat_with_mcp, inputs=inp, outputs=out)
 
 
 
35
 
36
- if __name__ == "__main__":
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)