Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,18 +2,19 @@ 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 chat(
|
| 11 |
-
api_messages = [
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
|
| 18 |
stream = client.chat.completions.create(
|
| 19 |
model="meta-llama/Llama-3.3-70B-Instruct-Turbo",
|
|
@@ -28,31 +29,33 @@ def chat(message, history, system_prompt):
|
|
| 28 |
yield partial_text
|
| 29 |
|
| 30 |
def respond(message, history, system_prompt):
|
| 31 |
-
history.append(
|
|
|
|
| 32 |
yield "", history
|
| 33 |
-
|
| 34 |
-
for partial_response in chat(
|
| 35 |
-
history[-1][
|
| 36 |
yield "", history
|
| 37 |
|
| 38 |
with gr.Blocks() as demo:
|
| 39 |
gr.Markdown("## Chatbot Llama 3.3 (Hugging Face Space)")
|
| 40 |
-
|
| 41 |
system_prompt = gr.Textbox(value="Tu es un assistant utile.", label="Prompt système")
|
| 42 |
-
|
| 43 |
with gr.Row():
|
| 44 |
with gr.Column(scale=4):
|
| 45 |
-
chatbot = gr.Chatbot(height=400)
|
| 46 |
msg = gr.Textbox(label="Message")
|
| 47 |
with gr.Column(scale=1):
|
| 48 |
history_dataset = gr.Dataset(
|
| 49 |
-
components=[msg],
|
| 50 |
-
samples=[],
|
| 51 |
label="Historique (Cliquez pour recharger)"
|
| 52 |
)
|
| 53 |
|
| 54 |
def update_history(history):
|
| 55 |
-
|
|
|
|
| 56 |
|
| 57 |
msg.submit(respond, [msg, chatbot, system_prompt], [msg, chatbot]).then(
|
| 58 |
update_history, [chatbot], history_dataset
|
|
|
|
| 2 |
from openai import OpenAI
|
| 3 |
import os
|
| 4 |
|
| 5 |
+
# Le client récupère automatiquement la clé depuis les "Secrets" de Hugging Face
|
| 6 |
client = OpenAI(
|
| 7 |
api_key=os.environ.get("KEY_SECRET"),
|
| 8 |
base_url="https://api.together.xyz/v1",
|
| 9 |
)
|
| 10 |
|
| 11 |
+
def chat(full_history_dicts, system_prompt):
|
| 12 |
+
api_messages = []
|
| 13 |
+
if system_prompt:
|
| 14 |
+
api_messages.append({"role": "system", "content": system_prompt})
|
| 15 |
+
|
| 16 |
+
for msg_item in full_history_dicts[:-1]:
|
| 17 |
+
api_messages.append({"role": msg_item["role"], "content": msg_item["content"]})
|
| 18 |
|
| 19 |
stream = client.chat.completions.create(
|
| 20 |
model="meta-llama/Llama-3.3-70B-Instruct-Turbo",
|
|
|
|
| 29 |
yield partial_text
|
| 30 |
|
| 31 |
def respond(message, history, system_prompt):
|
| 32 |
+
history.append({"role": "user", "content": message})
|
| 33 |
+
history.append({"role": "assistant", "content": ""})
|
| 34 |
yield "", history
|
| 35 |
+
|
| 36 |
+
for partial_response in chat(history, system_prompt):
|
| 37 |
+
history[-1]["content"] = partial_response
|
| 38 |
yield "", history
|
| 39 |
|
| 40 |
with gr.Blocks() as demo:
|
| 41 |
gr.Markdown("## Chatbot Llama 3.3 (Hugging Face Space)")
|
| 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
|