fiel1986 commited on
Commit
8937b67
·
verified ·
1 Parent(s): 472a6f4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -38
app.py CHANGED
@@ -2,23 +2,21 @@ import os
2
  import gradio as gr
3
  import torch
4
  import spaces
5
- from gradio import ChatMessage # Importar el objeto correcto para Gradio 6
6
 
7
  # Configuración del token
8
  HF_TOKEN = os.getenv("HF_TOKEN")
9
  print(f"Token del entorno encontrado: {'Sí' if HF_TOKEN else 'No'}")
10
 
 
 
 
11
  def chat_response(message, history, system_message, max_tokens, temperature, top_p, user_token):
12
  """
13
  Genera la respuesta del chat compatible con Gradio 6.0.
14
- history ahora es una lista de diccionarios o ChatMessage objects: [{'role': 'user', 'content': '...'}, ...]
15
  """
16
- # Determinar el token a usar
17
  token = user_token if user_token else HF_TOKEN
18
 
19
  if not token:
20
- # Si no hay token, agregamos un mensaje de error al historial
21
- # Formato correcto: {'role': 'assistant', 'content': '...'}
22
  history.append({"role": "assistant", "content": "⚠️ **Error de Configuración**: No se encontró un token de Hugging Face. \n\nConfigura 'HF_TOKEN' en el Space o escribe tu token abajo."})
23
  yield history
24
  return
@@ -30,26 +28,24 @@ def chat_response(message, history, system_message, max_tokens, temperature, top
30
  yield history
31
  return
32
 
33
- client = InferenceClient(token=token, model="deepseek-ai/DeepSeek-Coder-V2-Lite-Instruct")
 
34
 
35
- # Construir mensajes para la API
36
- # history ya viene en el formato correcto para la API en Gradio 6
37
  messages = [{"role": "system", "content": system_message}]
38
 
39
- # Agregar el historial (ya es una lista de dicts con role/content)
40
  for msg in history:
41
- # Asegurarse de que el mensaje tenga role y content
42
  if isinstance(msg, dict) and 'role' in msg and 'content' in msg:
43
  messages.append(msg)
44
- elif hasattr(msg, 'role') and hasattr(msg, 'content'): # Si es objeto ChatMessage
45
  messages.append({"role": msg.role, "content": msg.content})
46
 
47
- # Agregar el mensaje actual del usuario
48
  messages.append({"role": "user", "content": message})
49
 
50
  response_text = ""
51
 
52
  try:
 
53
  for chunk in client.chat_completion(
54
  messages,
55
  max_tokens=int(max_tokens),
@@ -60,30 +56,15 @@ def chat_response(message, history, system_message, max_tokens, temperature, top
60
  if chunk.choices and hasattr(chunk.choices[0].delta, 'content') and chunk.choices[0].delta.content:
61
  response_text += chunk.choices[0].delta.content
62
 
63
- # En Gradio 6, para hacer streaming, debemos actualizar el último mensaje del historial.
64
- # El historial 'history' contiene los mensajes previos. El último mensaje de usuario está al final.
65
- # Pero necesitamos agregar el mensaje del asistente que se va construyendo.
66
-
67
- # Estrategia:
68
- # 1. Copiar el historial actual (que tiene el mensaje del usuario al final).
69
- # 2. Si el último elemento es del usuario, lo mantenemos.
70
- # 3. Añadimos el mensaje del asistente en construcción como un nuevo elemento.
71
-
72
  current_history = list(history)
73
-
74
- # Verificar si el último mensaje es del usuario (lo cual debería serlo al iniciar la respuesta)
75
  if current_history and current_history[-1].get('role') == 'user':
76
- # Añadimos el mensaje del asistente en construcción
77
  current_history.append({"role": "assistant", "content": response_text})
78
  else:
79
- # Si por alguna razón el último no es usuario, actualizamos el último asistente
80
- # (Esto es un caso borde, pero lo manejamos)
81
  for i in range(len(current_history) - 1, -1, -1):
82
  if current_history[i].get('role') == 'assistant':
83
  current_history[i]['content'] = response_text
84
  break
85
  else:
86
- # Si no hay asistente, añadimos
87
  current_history.append({"role": "assistant", "content": response_text})
88
 
89
  yield current_history
@@ -95,15 +76,15 @@ def chat_response(message, history, system_message, max_tokens, temperature, top
95
  error_text = "⚠️ **Token Inválido**: El token de Hugging Face no es válido."
96
  elif "429" in error_msg:
97
  error_text = "⚠️ **Demasiadas peticiones**: Intenta de nuevo en unos segundos."
 
 
98
  else:
99
  error_text = f"⚠️ **Error**: {error_msg[:200]}"
100
 
101
  current_history = list(history)
102
- # Actualizar o añadir mensaje de error
103
  if current_history and current_history[-1].get('role') == 'user':
104
  current_history.append({"role": "assistant", "content": error_text})
105
  else:
106
- # Si no, actualizamos el último asistente existente
107
  for i in range(len(current_history) - 1, -1, -1):
108
  if current_history[i].get('role') == 'assistant':
109
  current_history[i]['content'] = error_text
@@ -119,10 +100,10 @@ def greet_gpu(n):
119
  zero = torch.Tensor([0]).to(device)
120
  return f"Hello {int(zero + n)} Tensor (GPU: {device})"
121
 
122
- # --- INTERFAZ DE GRADIO 6.0 ---
123
  with gr.Blocks() as demo:
124
- gr.Markdown("# Esme - Chatbot AI")
125
- gr.Markdown("🔐 **Opcional**: Escribe tu token de Hugging Face si no quieres usar el del Space.")
126
 
127
  with gr.Row():
128
  token_input = gr.Textbox(
@@ -133,7 +114,6 @@ with gr.Blocks() as demo:
133
  )
134
  clear_btn = gr.Button("🗑️ Limpiar Chat", scale=1)
135
 
136
- # Chatbot en Gradio 6 maneja automáticamente el formato de diccionarios
137
  chatbot = gr.Chatbot(
138
  label="Chat con Esme",
139
  height=600,
@@ -143,7 +123,7 @@ with gr.Blocks() as demo:
143
  with gr.Row():
144
  with gr.Column(scale=3):
145
  system_input = gr.Textbox(
146
- value="Tu nombre es Esme, eres amable y amigable",
147
  label="System Message"
148
  )
149
  max_tokens_input = gr.Slider(1, 4096, value=512, step=1, label="Max Tokens")
@@ -155,7 +135,6 @@ with gr.Blocks() as demo:
155
 
156
  chat_input = gr.Textbox(label="Mensaje", placeholder="Escribe aquí...")
157
 
158
- # Evento de envío
159
  chat_input.submit(
160
  fn=chat_response,
161
  inputs=[
@@ -170,9 +149,7 @@ with gr.Blocks() as demo:
170
  outputs=[chatbot]
171
  )
172
 
173
- # Limpiar chat
174
- clear_btn.click(lambda: [], None, chatbot) # Devolver lista vacía para limpiar
175
 
176
  if __name__ == "__main__":
177
- # Tema en launch()
178
  demo.queue(max_size=50).launch(theme=gr.themes.Soft())
 
2
  import gradio as gr
3
  import torch
4
  import spaces
 
5
 
6
  # Configuración del token
7
  HF_TOKEN = os.getenv("HF_TOKEN")
8
  print(f"Token del entorno encontrado: {'Sí' if HF_TOKEN else 'No'}")
9
 
10
+ # CAMBIO: Usamos un modelo disponible en la API gratuita
11
+ MODEL_ID = "Qwen/Qwen2.5-Coder-7B-Instruct"
12
+
13
  def chat_response(message, history, system_message, max_tokens, temperature, top_p, user_token):
14
  """
15
  Genera la respuesta del chat compatible con Gradio 6.0.
 
16
  """
 
17
  token = user_token if user_token else HF_TOKEN
18
 
19
  if not token:
 
 
20
  history.append({"role": "assistant", "content": "⚠️ **Error de Configuración**: No se encontró un token de Hugging Face. \n\nConfigura 'HF_TOKEN' en el Space o escribe tu token abajo."})
21
  yield history
22
  return
 
28
  yield history
29
  return
30
 
31
+ # Inicializar cliente con el NUEVO modelo
32
+ client = InferenceClient(token=token, model=MODEL_ID)
33
 
34
+ # Construir mensajes
 
35
  messages = [{"role": "system", "content": system_message}]
36
 
 
37
  for msg in history:
 
38
  if isinstance(msg, dict) and 'role' in msg and 'content' in msg:
39
  messages.append(msg)
40
+ elif hasattr(msg, 'role') and hasattr(msg, 'content'):
41
  messages.append({"role": msg.role, "content": msg.content})
42
 
 
43
  messages.append({"role": "user", "content": message})
44
 
45
  response_text = ""
46
 
47
  try:
48
+ # Llamada a la API
49
  for chunk in client.chat_completion(
50
  messages,
51
  max_tokens=int(max_tokens),
 
56
  if chunk.choices and hasattr(chunk.choices[0].delta, 'content') and chunk.choices[0].delta.content:
57
  response_text += chunk.choices[0].delta.content
58
 
 
 
 
 
 
 
 
 
 
59
  current_history = list(history)
 
 
60
  if current_history and current_history[-1].get('role') == 'user':
 
61
  current_history.append({"role": "assistant", "content": response_text})
62
  else:
 
 
63
  for i in range(len(current_history) - 1, -1, -1):
64
  if current_history[i].get('role') == 'assistant':
65
  current_history[i]['content'] = response_text
66
  break
67
  else:
 
68
  current_history.append({"role": "assistant", "content": response_text})
69
 
70
  yield current_history
 
76
  error_text = "⚠️ **Token Inválido**: El token de Hugging Face no es válido."
77
  elif "429" in error_msg:
78
  error_text = "⚠️ **Demasiadas peticiones**: Intenta de nuevo en unos segundos."
79
+ elif "not supported" in error_msg.lower():
80
+ error_text = f"⚠️ **Modelo no soportado**: El modelo '{MODEL_ID}' no está disponible en la API gratuita actual. Intenta con otro modelo."
81
  else:
82
  error_text = f"⚠️ **Error**: {error_msg[:200]}"
83
 
84
  current_history = list(history)
 
85
  if current_history and current_history[-1].get('role') == 'user':
86
  current_history.append({"role": "assistant", "content": error_text})
87
  else:
 
88
  for i in range(len(current_history) - 1, -1, -1):
89
  if current_history[i].get('role') == 'assistant':
90
  current_history[i]['content'] = error_text
 
100
  zero = torch.Tensor([0]).to(device)
101
  return f"Hello {int(zero + n)} Tensor (GPU: {device})"
102
 
103
+ # --- INTERFAZ ---
104
  with gr.Blocks() as demo:
105
+ gr.Markdown("# Esme ")
106
+ gr.Markdown(f" Modelo actual: **{MODEL_ID}** | 🔐 Token opcional abajo")
107
 
108
  with gr.Row():
109
  token_input = gr.Textbox(
 
114
  )
115
  clear_btn = gr.Button("🗑️ Limpiar Chat", scale=1)
116
 
 
117
  chatbot = gr.Chatbot(
118
  label="Chat con Esme",
119
  height=600,
 
123
  with gr.Row():
124
  with gr.Column(scale=3):
125
  system_input = gr.Textbox(
126
+ value="Eres un asistente de programación experto y amable llamado Esme.",
127
  label="System Message"
128
  )
129
  max_tokens_input = gr.Slider(1, 4096, value=512, step=1, label="Max Tokens")
 
135
 
136
  chat_input = gr.Textbox(label="Mensaje", placeholder="Escribe aquí...")
137
 
 
138
  chat_input.submit(
139
  fn=chat_response,
140
  inputs=[
 
149
  outputs=[chatbot]
150
  )
151
 
152
+ clear_btn.click(lambda: [], None, chatbot)
 
153
 
154
  if __name__ == "__main__":
 
155
  demo.queue(max_size=50).launch(theme=gr.themes.Soft())