Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,63 +2,46 @@ import gradio as gr
|
|
| 2 |
from openai import OpenAI
|
| 3 |
import os
|
| 4 |
|
| 5 |
-
#
|
| 6 |
client = OpenAI(
|
| 7 |
-
api_key=os.environ.get("
|
| 8 |
base_url="https://api.together.xyz/v1",
|
| 9 |
)
|
| 10 |
|
| 11 |
-
def
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
| 19 |
stream = client.chat.completions.create(
|
| 20 |
model="meta-llama/Llama-3.3-70B-Instruct-Turbo",
|
| 21 |
-
messages=
|
| 22 |
stream=True
|
| 23 |
)
|
| 24 |
|
| 25 |
-
|
| 26 |
for chunk in stream:
|
| 27 |
-
if chunk.choices[0].delta.content:
|
| 28 |
-
|
| 29 |
-
yield
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
system_prompt = gr.Textbox(value="Tu es un assistant utile.", label="Prompt système")
|
| 44 |
-
|
| 45 |
-
with gr.Row():
|
| 46 |
-
with gr.Column(scale=4):
|
| 47 |
-
chatbot = gr.Chatbot(height=400, type='messages', allow_tags=False)
|
| 48 |
-
msg = gr.Textbox(label="Message")
|
| 49 |
-
with gr.Column(scale=1):
|
| 50 |
-
history_dataset = gr.Dataset(
|
| 51 |
-
components=[msg],
|
| 52 |
-
samples=[],
|
| 53 |
-
label="Historique (Cliquez pour recharger)"
|
| 54 |
-
)
|
| 55 |
-
|
| 56 |
-
def update_history(history):
|
| 57 |
-
user_messages = [[msg_item["content"]] for msg_item in history if msg_item["role"] == "user"]
|
| 58 |
-
return gr.Dataset(samples=user_messages)
|
| 59 |
-
|
| 60 |
-
msg.submit(respond, [msg, chatbot, system_prompt], [msg, chatbot]).then(
|
| 61 |
-
update_history, [chatbot], history_dataset
|
| 62 |
-
)
|
| 63 |
|
| 64 |
-
|
|
|
|
|
|
| 2 |
from openai import OpenAI
|
| 3 |
import os
|
| 4 |
|
| 5 |
+
# Configuration du client
|
| 6 |
client = OpenAI(
|
| 7 |
+
api_key=os.environ.get("TOGETHER_API_KEY"), # Utilisez une variable d'environnement pour la sécurité
|
| 8 |
base_url="https://api.together.xyz/v1",
|
| 9 |
)
|
| 10 |
|
| 11 |
+
def predict(message, history, system_prompt):
|
| 12 |
+
# Transformation de l'historique Gradio vers le format attendu par l'API
|
| 13 |
+
messages = [{"role": "system", "content": system_prompt}]
|
| 14 |
+
|
| 15 |
+
for human, assistant in history:
|
| 16 |
+
messages.append({"role": "user", "content": human})
|
| 17 |
+
messages.append({"role": "assistant", "content": assistant})
|
| 18 |
+
|
| 19 |
+
messages.append({"role": "user", "content": message})
|
| 20 |
+
|
| 21 |
+
# Appel au streaming
|
| 22 |
stream = client.chat.completions.create(
|
| 23 |
model="meta-llama/Llama-3.3-70B-Instruct-Turbo",
|
| 24 |
+
messages=messages,
|
| 25 |
stream=True
|
| 26 |
)
|
| 27 |
|
| 28 |
+
partial_message = ""
|
| 29 |
for chunk in stream:
|
| 30 |
+
if chunk.choices[0].delta.content is not None:
|
| 31 |
+
partial_message += chunk.choices[0].delta.content
|
| 32 |
+
yield partial_message
|
| 33 |
+
|
| 34 |
+
# Interface Gradio optimisée
|
| 35 |
+
demo = gr.ChatInterface(
|
| 36 |
+
fn=predict,
|
| 37 |
+
additional_inputs=[
|
| 38 |
+
gr.Textbox(value="Tu es un assistant utile.", label="Prompt système")
|
| 39 |
+
],
|
| 40 |
+
title="Mon Chatbot Llama 3.3",
|
| 41 |
+
description="Interface de chat avec streaming en temps réel.",
|
| 42 |
+
examples=["Explique-moi la physique quantique", "Écris un poème sur l'IA"],
|
| 43 |
+
theme="soft"
|
| 44 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
+
if __name__ == "__main__":
|
| 47 |
+
demo.launch()
|