Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,10 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
|
| 5 |
def respond(
|
|
@@ -12,26 +17,25 @@ def respond(
|
|
| 12 |
hf_token: gr.OAuthToken,
|
| 13 |
):
|
| 14 |
"""
|
| 15 |
-
|
|
|
|
| 16 |
"""
|
| 17 |
-
|
| 18 |
|
| 19 |
messages = [{"role": "system", "content": system_message}]
|
| 20 |
-
|
| 21 |
messages.extend(history)
|
| 22 |
-
|
| 23 |
messages.append({"role": "user", "content": message})
|
| 24 |
|
| 25 |
response = ""
|
| 26 |
|
| 27 |
-
for
|
| 28 |
messages,
|
| 29 |
max_tokens=max_tokens,
|
| 30 |
stream=True,
|
| 31 |
temperature=temperature,
|
| 32 |
top_p=top_p,
|
| 33 |
):
|
| 34 |
-
choices =
|
| 35 |
token = ""
|
| 36 |
if len(choices) and choices[0].delta.content:
|
| 37 |
token = choices[0].delta.content
|
|
@@ -39,10 +43,12 @@ def respond(
|
|
| 39 |
response += token
|
| 40 |
yield response
|
| 41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
"""
|
| 44 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 45 |
-
"""
|
| 46 |
chatbot = gr.ChatInterface(
|
| 47 |
respond,
|
| 48 |
type="messages",
|
|
@@ -50,13 +56,7 @@ chatbot = gr.ChatInterface(
|
|
| 50 |
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 51 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 52 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 53 |
-
gr.Slider(
|
| 54 |
-
minimum=0.1,
|
| 55 |
-
maximum=1.0,
|
| 56 |
-
value=0.95,
|
| 57 |
-
step=0.05,
|
| 58 |
-
label="Top-p (nucleus sampling)",
|
| 59 |
-
),
|
| 60 |
],
|
| 61 |
)
|
| 62 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
+
from gradio_client import Client
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
# Se connecter au Space qui expose le MCP
|
| 7 |
+
mcp_client = Client("HackathonCRA/mcp")
|
| 8 |
|
| 9 |
|
| 10 |
def respond(
|
|
|
|
| 17 |
hf_token: gr.OAuthToken,
|
| 18 |
):
|
| 19 |
"""
|
| 20 |
+
Chat client qui envoie d'abord au modèle HF, et qui peut ensuite
|
| 21 |
+
appeler le MCP via gradio_client si besoin.
|
| 22 |
"""
|
| 23 |
+
llm = InferenceClient(token=hf_token.token, model="openai/gpt-oss-20b")
|
| 24 |
|
| 25 |
messages = [{"role": "system", "content": system_message}]
|
|
|
|
| 26 |
messages.extend(history)
|
|
|
|
| 27 |
messages.append({"role": "user", "content": message})
|
| 28 |
|
| 29 |
response = ""
|
| 30 |
|
| 31 |
+
for msg in llm.chat_completion(
|
| 32 |
messages,
|
| 33 |
max_tokens=max_tokens,
|
| 34 |
stream=True,
|
| 35 |
temperature=temperature,
|
| 36 |
top_p=top_p,
|
| 37 |
):
|
| 38 |
+
choices = msg.choices
|
| 39 |
token = ""
|
| 40 |
if len(choices) and choices[0].delta.content:
|
| 41 |
token = choices[0].delta.content
|
|
|
|
| 43 |
response += token
|
| 44 |
yield response
|
| 45 |
|
| 46 |
+
# 👉 Exemple : appel au MCP (ici on appelle une fn "search_docs" du serveur)
|
| 47 |
+
# tu adaptes en fonction de ce que ton serveur expose comme endpoints
|
| 48 |
+
result = mcp_client.predict("ma requête", api_name="/search_docs")
|
| 49 |
+
yield f"\n\n[MCP result] {result}"
|
| 50 |
+
|
| 51 |
|
|
|
|
|
|
|
|
|
|
| 52 |
chatbot = gr.ChatInterface(
|
| 53 |
respond,
|
| 54 |
type="messages",
|
|
|
|
| 56 |
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 57 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 58 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 59 |
+
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
],
|
| 61 |
)
|
| 62 |
|