ivanoctaviogaitansantos commited on
Commit
7d0525e
verified
1 Parent(s): 912a3a6

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +75 -0
app.py ADDED
@@ -0,0 +1,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+ import json
4
+ import os # Para acceder a variables de entorno
5
+
6
+ # Cargar clave API desde Secret configurado en Hugging Face Spaces
7
+ API_KEY = os.getenv("SAMBANOVA_API_KEY")
8
+ API_URL = "https://api.sambanova.ai/v1/chat/completions"
9
+
10
+ headers = {
11
+ "Authorization": f"Bearer {API_KEY}",
12
+ "Content-Type": "application/json",
13
+ }
14
+
15
+ def query_deepseek_stream(user_message, chat_history):
16
+ messages = [{"role": "system", "content": "You are a helpful assistant"}]
17
+ for user_msg, ai_msg in chat_history:
18
+ messages.append({"role": "user", "content": user_msg})
19
+ messages.append({"role": "assistant", "content": ai_msg})
20
+ messages.append({"role": "user", "content": user_message})
21
+
22
+ json_data = {
23
+ "model": "DeepSeek-V3.1",
24
+ "messages": messages,
25
+ "stream": True,
26
+ }
27
+
28
+ try:
29
+ response = requests.post(API_URL, headers=headers, json=json_data, stream=True)
30
+ response.raise_for_status()
31
+
32
+ collected_text = ""
33
+ updated_history = chat_history.copy()
34
+
35
+ updated_history.append((user_message, ""))
36
+
37
+ for line in response.iter_lines(decode_unicode=True):
38
+ if line:
39
+ if line.startswith("data: "):
40
+ json_str = line[len("data: "):]
41
+ if json_str == "[DONE]":
42
+ break
43
+ try:
44
+ data = json.loads(json_str)
45
+ delta = data.get("choices", [{}])[0].get("delta", {})
46
+ text_fragment = delta.get("content", "")
47
+
48
+ collected_text += text_fragment
49
+
50
+ if updated_history:
51
+ updated_history[-1] = (updated_history[-1][0], collected_text)
52
+
53
+ yield updated_history, updated_history
54
+ except json.JSONDecodeError:
55
+ continue
56
+ except requests.exceptions.RequestException as err:
57
+ error_msg = f"Error: {err}"
58
+ updated_history = chat_history.copy()
59
+ if not updated_history or updated_history[-1][0] != user_message:
60
+ updated_history.append((user_message, error_msg))
61
+ else:
62
+ updated_history[-1] = (updated_history[-1][0], error_msg)
63
+ yield updated_history, updated_history
64
+
65
+
66
+ with gr.Blocks() as demo:
67
+ chat_history = gr.State([])
68
+ chatbot = gr.Chatbot()
69
+ msg = gr.Textbox(label="Escribe tu mensaje")
70
+ btn = gr.Button("Enviar")
71
+
72
+ btn.click(query_deepseek_stream, inputs=[msg, chat_history], outputs=[chatbot, chat_history])
73
+ msg.submit(query_deepseek_stream, inputs=[msg, chat_history], outputs=[chatbot, chat_history])
74
+
75
+ demo.launch()