Rizonando commited on
Commit
d15f95f
路
verified 路
1 Parent(s): 3505078

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -17
app.py CHANGED
@@ -1,38 +1,55 @@
1
- # SMART - Interfaz tipo ChatGPT usando Gradio y modelo p煤blico estable
2
 
3
  import gradio as gr
4
  import requests
5
 
6
- # Usaremos un modelo p煤blico accesible sin restricciones complejas
7
- API_URL = "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.1"
8
- HEADERS = {"Authorization": "Bearer hf_xxxxxxxxxxxxxxxxxxxxxx"}
9
 
 
10
 
11
- # Funci贸n para enviar mensaje a la API de Hugging Face
12
  def chat_with_smart(message, history):
 
 
 
13
  history = history or []
14
- prompt = "".join([f"Usuario: {msg}\nAsistente: {res}\n" for msg, res in history])
15
  prompt += f"Usuario: {message}\nAsistente:"
16
 
 
 
 
 
 
 
17
  try:
18
- response = requests.post(API_URL, headers=HEADERS, json={"inputs": prompt})
19
  response.raise_for_status()
20
- output = response.json()
 
 
 
21
 
22
- if isinstance(output, dict) and "error" in output:
23
- return history, f"[ERROR]: {output['error']}"
 
 
24
 
25
- text = output[0].get("generated_text", "[Respuesta vac铆a]")
26
- reply = text[len(prompt):].strip()
27
- history.append((message, reply))
28
- return history, reply
29
 
 
 
 
 
30
  except Exception as e:
31
- return history, f"[ERROR de conexi贸n]: {str(e)}"
32
 
33
  # Interfaz visual tipo ChatGPT
 
34
  def build_interface():
35
- with gr.Blocks(theme=gr.themes.Base(), title="SMART") as demo:
36
  gr.Markdown("# 馃 SMART - Tu Asistente Inteligente")
37
 
38
  with gr.Row():
@@ -44,7 +61,7 @@ def build_interface():
44
 
45
  with gr.Column(scale=4):
46
  chatbot = gr.Chatbot()
47
- msg = gr.Textbox(placeholder="Escribe un mensaje...", label="Mensaje")
48
  send = gr.Button("Enviar")
49
 
50
  state = gr.State([])
@@ -55,6 +72,9 @@ def build_interface():
55
  return demo
56
 
57
  # Ejecutar la app
 
58
  if __name__ == "__main__":
59
  build_interface().launch()
 
 
60
 
 
1
+ # SMART - Interfaz tipo ChatGPT usando Gradio y modelo estable sin requerimientos especiales
2
 
3
  import gradio as gr
4
  import requests
5
 
6
+ # Modelo accesible sin restricciones de acceso
7
+ API_URL = "https://api-inference.huggingface.co/models/google/flan-t5-large"
8
+ HEADERS = {"Authorization": "Bearer hf_tu_token_aqui"} # Reemplaza con tu token v谩lido
9
 
10
+ # Funci贸n para interactuar con el modelo
11
 
 
12
  def chat_with_smart(message, history):
13
+ if not message:
14
+ return history, "[ERROR]: El mensaje est谩 vac铆o."
15
+
16
  history = history or []
17
+ prompt = "".join([f"Usuario: {m}\nAsistente: {r}\n" for m, r in history])
18
  prompt += f"Usuario: {message}\nAsistente:"
19
 
20
+ payload = {
21
+ "inputs": prompt,
22
+ "parameters": {"max_new_tokens": 128},
23
+ "options": {"wait_for_model": True}
24
+ }
25
+
26
  try:
27
+ response = requests.post(API_URL, headers=HEADERS, json=payload, timeout=30)
28
  response.raise_for_status()
29
+ result = response.json()
30
+
31
+ if isinstance(result, dict) and "error" in result:
32
+ return history, f"[ERROR de modelo]: {result['error']}"
33
 
34
+ try:
35
+ respuesta = result[0]["generated_text"].strip()
36
+ except (IndexError, KeyError, TypeError):
37
+ respuesta = "[Sin contenido generado]"
38
 
39
+ history.append((message, respuesta))
40
+ return history, respuesta
 
 
41
 
42
+ except requests.exceptions.HTTPError as e:
43
+ return history, f"[ERROR HTTP]: {e.response.status_code} - {e.response.reason}"
44
+ except requests.exceptions.RequestException as e:
45
+ return history, f"[ERROR de red]: {str(e)}"
46
  except Exception as e:
47
+ return history, f"[ERROR inesperado]: {str(e)}"
48
 
49
  # Interfaz visual tipo ChatGPT
50
+
51
  def build_interface():
52
+ with gr.Blocks(title="SMART", theme=gr.themes.Soft()) as demo:
53
  gr.Markdown("# 馃 SMART - Tu Asistente Inteligente")
54
 
55
  with gr.Row():
 
61
 
62
  with gr.Column(scale=4):
63
  chatbot = gr.Chatbot()
64
+ msg = gr.Textbox(placeholder="Escribe un mensaje...", label="Mensaje", lines=2)
65
  send = gr.Button("Enviar")
66
 
67
  state = gr.State([])
 
72
  return demo
73
 
74
  # Ejecutar la app
75
+
76
  if __name__ == "__main__":
77
  build_interface().launch()
78
+
79
+
80