cspilot commited on
Commit
d8eb5f0
·
verified ·
1 Parent(s): 9c9f42f

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +43 -22
src/streamlit_app.py CHANGED
@@ -1,45 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
  import requests
3
  from streamlit.runtime.scriptrunner import RerunException
4
 
5
- import os
6
- os.environ["STREAMLIT_DISABLE_WATCHDOG_WARN"] = "true"
7
- os.environ["STREAMLIT_MIGRATE_DATA"] = "false"
8
- os.environ["XDG_CONFIG_HOME"] = "/tmp"
9
 
 
10
  st.title("🤖 Chat con DeepSeek-R1")
11
 
 
12
  API_KEY = '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'
13
 
 
14
  if "messages" not in st.session_state:
15
  st.session_state.messages = []
16
 
 
17
  for msg in st.session_state.messages:
18
  with st.chat_message(msg["role"]):
19
  st.markdown(msg["content"])
20
 
21
- prompt = st.chat_input("Escribe tu mensaje...")
 
22
 
23
  if prompt:
 
24
  st.session_state.messages.append({"role": "user", "content": prompt})
25
  with st.chat_message("user"):
26
  st.markdown(prompt)
27
 
 
28
  with st.chat_message("assistant"):
29
- st.write("Pensando...")
30
-
31
- response = requests.post(
32
- "https://openrouter.ai/api/v1/chat/completions",
33
- headers={
34
- "Authorization": f"Bearer {API_KEY}",
35
- "Content-Type": "application/json"
36
- },
37
- json={
38
- "model": "deepseek/deepseek-r1:free",
39
- "messages": st.session_state.messages
40
- }
41
- )
42
-
43
- reply = response.json()["choices"][0]["message"]["content"]
44
- st.session_state.messages.append({"role": "assistant", "content": reply})
45
- raise RerunException()
 
 
 
 
 
1
+ # ─── 1. Variables de entorno ────────────────────────────────────────────────
2
+ import os
3
+
4
+ # Evita que Streamlit intente escribir en /.streamlit
5
+ os.environ["XDG_CONFIG_HOME"] = "/tmp"
6
+ # Desactiva watchdog warnings y migraciones que escriben en disco
7
+ os.environ["STREAMLIT_DISABLE_WATCHDOG_WARN"] = "true"
8
+ os.environ["STREAMLIT_MIGRATE_DATA"] = "false"
9
+ # Desactiva recolección de estadísticas de uso
10
+ os.environ["STREAMLIT_BROWSER_GATHER_USAGE_STATS"] = "false"
11
+
12
+ # ─── 2. Imports ─────────────────────────────────────────────────────────────
13
  import streamlit as st
14
  import requests
15
  from streamlit.runtime.scriptrunner import RerunException
16
 
17
+ # ─── 3. Configuración de página ─────────────────────────────────────────────
18
+ st.set_page_config(page_title="🤖 Chat con DeepSeek-R1", page_icon="🤖")
 
 
19
 
20
+ # ─── 4. Tu lógica ───────────────────────────────────────────────────────────
21
  st.title("🤖 Chat con DeepSeek-R1")
22
 
23
+ # Ideal: mueve esto a st.secrets en tu Space en HF
24
  API_KEY = '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'
25
 
26
+ # Inicializa el historial de mensajes
27
  if "messages" not in st.session_state:
28
  st.session_state.messages = []
29
 
30
+ # Muestra el chat
31
  for msg in st.session_state.messages:
32
  with st.chat_message(msg["role"]):
33
  st.markdown(msg["content"])
34
 
35
+ # Input del usuario
36
+ prompt = st.chat_input("Escribe tu mensaje…")
37
 
38
  if prompt:
39
+ # Agrega mensaje del usuario
40
  st.session_state.messages.append({"role": "user", "content": prompt})
41
  with st.chat_message("user"):
42
  st.markdown(prompt)
43
 
44
+ # Mensaje intermedio
45
  with st.chat_message("assistant"):
46
+ st.write("Pensando…")
47
+
48
+ # Llamada a la API
49
+ response = requests.post(
50
+ "https://openrouter.ai/api/v1/chat/completions",
51
+ headers={
52
+ "Authorization": f"Bearer {API_KEY}",
53
+ "Content-Type": "application/json",
54
+ },
55
+ json={
56
+ "model": "deepseek/deepseek-r1:free",
57
+ "messages": st.session_state.messages,
58
+ },
59
+ )
60
+
61
+ # Procesa y almacena la respuesta
62
+ reply = response.json()["choices"][0]["message"]["content"]
63
+ st.session_state.messages.append({"role": "assistant", "content": reply})
64
+
65
+ # Fuerza el rerun para actualizar la UI
66
+ raise RerunException()