Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,54 +2,55 @@ import gradio as gr
|
|
| 2 |
from openai import OpenAI
|
| 3 |
import os
|
| 4 |
|
|
|
|
| 5 |
client = OpenAI(
|
| 6 |
api_key=os.environ.get("KEY_SECRET"),
|
| 7 |
base_url="https://api.together.xyz/v1",
|
| 8 |
)
|
| 9 |
|
| 10 |
def respond(message, history, system_prompt):
|
| 11 |
-
|
|
|
|
| 12 |
|
| 13 |
-
# Construction
|
| 14 |
messages = [{"role": "system", "content": system_prompt}]
|
| 15 |
for h in history:
|
| 16 |
messages.append({"role": "user", "content": h[0]})
|
| 17 |
if h[1]: messages.append({"role": "assistant", "content": h[1]})
|
| 18 |
messages.append({"role": "user", "content": message})
|
| 19 |
|
| 20 |
-
# Appel
|
| 21 |
stream = client.chat.completions.create(
|
| 22 |
model="meta-llama/Llama-3.3-70B-Instruct-Turbo",
|
| 23 |
messages=messages,
|
| 24 |
stream=True
|
| 25 |
)
|
| 26 |
|
|
|
|
| 27 |
history.append([message, ""])
|
|
|
|
| 28 |
partial_text = ""
|
| 29 |
for chunk in stream:
|
| 30 |
if chunk.choices[0].delta.content:
|
| 31 |
partial_text += chunk.choices[0].delta.content
|
|
|
|
| 32 |
history[-1][1] = partial_text
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
history_text = "\n\n".join([f"• {h[0]}" for h in history])
|
| 36 |
-
yield "", history, history_text
|
| 37 |
|
| 38 |
with gr.Blocks() as demo:
|
| 39 |
-
gr.Markdown("# Chatbot Llama 3.3")
|
| 40 |
|
| 41 |
with gr.Row():
|
| 42 |
with gr.Column(scale=1):
|
| 43 |
system_prompt = gr.Textbox(value="Tu es un assistant utile.", label="Prompt système")
|
| 44 |
-
gr.Markdown("### Vos questions précédentes")
|
| 45 |
-
# Utilisation d'un Markdown simple à la place du Dataset capricieux
|
| 46 |
-
history_display = gr.Markdown("Aucune question pour le moment.")
|
| 47 |
|
| 48 |
with gr.Column(scale=3):
|
|
|
|
| 49 |
chatbot = gr.Chatbot(height=500)
|
| 50 |
msg = gr.Textbox(label="Entrez votre message")
|
| 51 |
|
| 52 |
-
# Logique d'envoi
|
| 53 |
-
msg.submit(respond, [msg, chatbot, system_prompt], [msg, chatbot
|
| 54 |
|
| 55 |
demo.launch()
|
|
|
|
| 2 |
from openai import OpenAI
|
| 3 |
import os
|
| 4 |
|
| 5 |
+
# Configuration client
|
| 6 |
client = OpenAI(
|
| 7 |
api_key=os.environ.get("KEY_SECRET"),
|
| 8 |
base_url="https://api.together.xyz/v1",
|
| 9 |
)
|
| 10 |
|
| 11 |
def respond(message, history, system_prompt):
|
| 12 |
+
# 1. Nettoyage de l'historique pour Gradio (doit être une liste de listes de strings)
|
| 13 |
+
history = history if history is not None else []
|
| 14 |
|
| 15 |
+
# 2. Construction des messages pour l'API Together (format standard)
|
| 16 |
messages = [{"role": "system", "content": system_prompt}]
|
| 17 |
for h in history:
|
| 18 |
messages.append({"role": "user", "content": h[0]})
|
| 19 |
if h[1]: messages.append({"role": "assistant", "content": h[1]})
|
| 20 |
messages.append({"role": "user", "content": message})
|
| 21 |
|
| 22 |
+
# 3. Appel API avec streaming
|
| 23 |
stream = client.chat.completions.create(
|
| 24 |
model="meta-llama/Llama-3.3-70B-Instruct-Turbo",
|
| 25 |
messages=messages,
|
| 26 |
stream=True
|
| 27 |
)
|
| 28 |
|
| 29 |
+
# 4. Préparation de la réponse dans l'historique
|
| 30 |
history.append([message, ""])
|
| 31 |
+
|
| 32 |
partial_text = ""
|
| 33 |
for chunk in stream:
|
| 34 |
if chunk.choices[0].delta.content:
|
| 35 |
partial_text += chunk.choices[0].delta.content
|
| 36 |
+
# Mise à jour de la dernière réponse
|
| 37 |
history[-1][1] = partial_text
|
| 38 |
+
# Yield simple : Gradio Chatbot attend (historique_mis_a_jour)
|
| 39 |
+
yield "", history
|
|
|
|
|
|
|
| 40 |
|
| 41 |
with gr.Blocks() as demo:
|
| 42 |
+
gr.Markdown("# Chatbot Llama 3.3 (Mentorat)")
|
| 43 |
|
| 44 |
with gr.Row():
|
| 45 |
with gr.Column(scale=1):
|
| 46 |
system_prompt = gr.Textbox(value="Tu es un assistant utile.", label="Prompt système")
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
with gr.Column(scale=3):
|
| 49 |
+
# Le Chatbot gère l'affichage de l'historique automatiquement
|
| 50 |
chatbot = gr.Chatbot(height=500)
|
| 51 |
msg = gr.Textbox(label="Entrez votre message")
|
| 52 |
|
| 53 |
+
# Logique d'envoi simplifiée
|
| 54 |
+
msg.submit(respond, [msg, chatbot, system_prompt], [msg, chatbot])
|
| 55 |
|
| 56 |
demo.launch()
|