Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,51 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from huggingface_hub import InferenceClient
|
| 3 |
-
from gradio_client import Client
|
| 4 |
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
|
| 10 |
def respond(
|
|
@@ -14,57 +55,32 @@ def respond(
|
|
| 14 |
max_tokens,
|
| 15 |
temperature,
|
| 16 |
top_p,
|
| 17 |
-
|
| 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
|
| 42 |
-
|
| 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",
|
| 55 |
additional_inputs=[
|
| 56 |
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 57 |
-
gr.Slider(minimum=1, maximum=
|
| 58 |
-
gr.Slider(minimum=0.
|
| 59 |
-
gr.Slider(minimum=0.
|
|
|
|
| 60 |
],
|
| 61 |
)
|
| 62 |
|
| 63 |
with gr.Blocks() as demo:
|
| 64 |
-
with gr.Sidebar():
|
| 65 |
-
gr.LoginButton()
|
| 66 |
chatbot.render()
|
| 67 |
|
| 68 |
-
|
| 69 |
if __name__ == "__main__":
|
| 70 |
demo.launch()
|
|
|
|
| 1 |
+
import os
|
| 2 |
import gradio as gr
|
|
|
|
|
|
|
| 3 |
|
| 4 |
+
# --- OpenAI (SDK officiel) ---
|
| 5 |
+
from openai import OpenAI
|
| 6 |
+
openai_client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
|
| 7 |
|
| 8 |
+
# --- Mistral (SDK officiel) ---
|
| 9 |
+
from mistralai import Mistral
|
| 10 |
+
mistral_client = Mistral(api_key=os.getenv("MISTRAL_API_KEY"))
|
| 11 |
+
|
| 12 |
+
# Choisis à l’exécution quel LLM utiliser
|
| 13 |
+
DEFAULT_PROVIDER = "openai" # ou "mistral"
|
| 14 |
+
|
| 15 |
+
def llm_chat(messages, provider=DEFAULT_PROVIDER, max_tokens, temperature, top_p):
|
| 16 |
+
if provider == "openai":
|
| 17 |
+
# Chat Completions (ex: gpt-4o-mini / gpt-4.1 / … selon ton compte)
|
| 18 |
+
stream = openai_client.chat.completions.create(
|
| 19 |
+
model="gpt-4o-mini",
|
| 20 |
+
messages=messages,
|
| 21 |
+
temperature=temperature,
|
| 22 |
+
top_p=top_p,
|
| 23 |
+
max_tokens=max_tokens,
|
| 24 |
+
stream=True,
|
| 25 |
+
)
|
| 26 |
+
for chunk in stream:
|
| 27 |
+
delta = chunk.choices[0].delta
|
| 28 |
+
if delta and delta.content:
|
| 29 |
+
yield delta.content
|
| 30 |
+
|
| 31 |
+
elif provider == "mistral":
|
| 32 |
+
# Models: "mistral-large-latest", "open-mistral-nemo", etc.
|
| 33 |
+
stream = mistral_client.chat.stream(
|
| 34 |
+
model="mistral-large-latest",
|
| 35 |
+
messages=messages,
|
| 36 |
+
temperature=temperature,
|
| 37 |
+
top_p=top_p,
|
| 38 |
+
max_tokens=max_tokens,
|
| 39 |
+
)
|
| 40 |
+
for event in stream:
|
| 41 |
+
if event.type == "chat.completion.chunk":
|
| 42 |
+
piece = event.data.delta or ""
|
| 43 |
+
if piece:
|
| 44 |
+
yield piece
|
| 45 |
+
stream.close()
|
| 46 |
+
|
| 47 |
+
else:
|
| 48 |
+
yield "[Erreur] Provider inconnu."
|
| 49 |
|
| 50 |
|
| 51 |
def respond(
|
|
|
|
| 55 |
max_tokens,
|
| 56 |
temperature,
|
| 57 |
top_p,
|
| 58 |
+
provider # "openai" ou "mistral"
|
| 59 |
):
|
| 60 |
+
# Construit le format messages attendu par les SDKs
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
messages = [{"role": "system", "content": system_message}]
|
| 62 |
messages.extend(history)
|
| 63 |
messages.append({"role": "user", "content": message})
|
| 64 |
|
| 65 |
response = ""
|
| 66 |
+
for token in llm_chat(messages, provider, max_tokens, temperature, top_p):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
response += token
|
| 68 |
yield response
|
| 69 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
chatbot = gr.ChatInterface(
|
| 71 |
respond,
|
| 72 |
type="messages",
|
| 73 |
additional_inputs=[
|
| 74 |
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 75 |
+
gr.Slider(minimum=1, maximum=4096, value=512, step=1, label="Max new tokens"),
|
| 76 |
+
gr.Slider(minimum=0.0, maximum=2.0, value=0.7, step=0.1, label="Temperature"),
|
| 77 |
+
gr.Slider(minimum=0.0, maximum=1.0, value=0.95, step=0.05, label="Top-p"),
|
| 78 |
+
gr.Dropdown(choices=["openai", "mistral"], value=DEFAULT_PROVIDER, label="Provider"),
|
| 79 |
],
|
| 80 |
)
|
| 81 |
|
| 82 |
with gr.Blocks() as demo:
|
|
|
|
|
|
|
| 83 |
chatbot.render()
|
| 84 |
|
|
|
|
| 85 |
if __name__ == "__main__":
|
| 86 |
demo.launch()
|