Foccuus commited on
Commit
71cf852
·
verified ·
1 Parent(s): 2156fa0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -49
app.py CHANGED
@@ -2,63 +2,46 @@ import gradio as gr
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",
21
- messages=api_messages,
22
  stream=True
23
  )
24
 
25
- partial_text = ""
26
  for chunk in stream:
27
- if chunk.choices[0].delta.content:
28
- partial_text += chunk.choices[0].delta.content
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
62
- )
63
 
64
- demo.launch()
 
 
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()