cspilot commited on
Commit
3da1e38
·
verified ·
1 Parent(s): d8eb5f0

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +12 -19
src/streamlit_app.py CHANGED
@@ -1,52 +1,46 @@
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}",
@@ -58,9 +52,8 @@ if prompt:
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()
 
1
  # ─── 1. Variables de entorno ────────────────────────────────────────────────
2
  import os
 
 
3
  os.environ["XDG_CONFIG_HOME"] = "/tmp"
 
4
  os.environ["STREAMLIT_DISABLE_WATCHDOG_WARN"] = "true"
5
  os.environ["STREAMLIT_MIGRATE_DATA"] = "false"
 
6
  os.environ["STREAMLIT_BROWSER_GATHER_USAGE_STATS"] = "false"
7
 
8
  # ─── 2. Imports ─────────────────────────────────────────────────────────────
9
  import streamlit as st
10
  import requests
 
11
 
12
+ # ─── 3. Config de página ───────────────────────────────────────────────────
13
  st.set_page_config(page_title="🤖 Chat con DeepSeek-R1", page_icon="🤖")
14
 
15
+ # ─── 4. Lógica de la app ───────────────────────────────────────────────────
16
  st.title("🤖 Chat con DeepSeek-R1")
17
 
18
+ # Ideal: pon esto en st.secrets en HF Settings
19
+ API_KEY = "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"
20
 
 
21
  if "messages" not in st.session_state:
22
  st.session_state.messages = []
23
 
24
+ # 4.1 Muestra todo el historial
25
  for msg in st.session_state.messages:
26
  with st.chat_message(msg["role"]):
27
  st.markdown(msg["content"])
28
 
29
+ # 4.2 Input del usuario
30
  prompt = st.chat_input("Escribe tu mensaje…")
31
 
32
  if prompt:
33
+ # Añades el mensaje y lo muestras
34
  st.session_state.messages.append({"role": "user", "content": prompt})
35
  with st.chat_message("user"):
36
  st.markdown(prompt)
37
 
38
+ # Mensaje de “pensando”
39
  with st.chat_message("assistant"):
40
  st.write("Pensando…")
41
 
42
  # Llamada a la API
43
+ resp = requests.post(
44
  "https://openrouter.ai/api/v1/chat/completions",
45
  headers={
46
  "Authorization": f"Bearer {API_KEY}",
 
52
  },
53
  )
54
 
55
+ # Procesas la respuesta y la guardas
56
+ reply = resp.json()["choices"][0]["message"]["content"]
57
  st.session_state.messages.append({"role": "assistant", "content": reply})
58
 
59
+ # ¡No hace falta raise ni rerun!