Rizonando commited on
Commit
304c2e7
路
verified 路
1 Parent(s): a7b081f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -29
app.py CHANGED
@@ -1,40 +1,57 @@
 
 
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
3
 
4
- # Reemplaza ESTE TEXTO con tu verdadero token secreto
5
- import os
6
- API_TOKEN = os.getenv("HF_TOKEN")
7
 
8
- # Inicializa el cliente con el modelo y el token
9
- client = InferenceClient(
10
- model="HuggingFaceH4/zephyr-7b-beta",
11
- token=API_TOKEN
12
- )
13
 
14
- def responder_chat(mensaje, system_message, max_tokens, temperature, top_p):
15
  try:
16
- prompt = f"{system_message}\nUsuario: {mensaje}\nAsistente:"
17
- response = client.text_generation(
18
- prompt,
19
- max_new_tokens=max_tokens,
20
- temperature=temperature,
21
- top_p=top_p,
22
- return_full_text=False
23
- )
24
- return response.generated_text
 
 
25
  except Exception as e:
26
- return f"Lo siento, hubo un error procesando tu mensaje: {str(e)}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
 
28
- with gr.Blocks() as demo:
29
- system_message = gr.Textbox(value="Eres un asistente amigable y 煤til.", label="Mensaje del sistema")
30
- chat_input = gr.Textbox(placeholder="Escribe aqu铆 tu mensaje...", label="Mensaje")
31
- max_tokens = gr.Slider(minimum=10, maximum=512, value=150, step=10, label="Max tokens")
32
- temperature = gr.Slider(minimum=0.1, maximum=1.0, value=0.7, step=0.1, label="Temperature")
33
- top_p = gr.Slider(minimum=0.1, maximum=1.0, value=0.9, step=0.05, label="Top-p")
34
- chat_output = gr.Textbox(label="Respuesta del asistente")
35
 
36
- chat_input.submit(responder_chat, inputs=[chat_input, system_message, max_tokens, temperature, top_p], outputs=chat_output)
 
37
 
38
- demo.launch()
39
 
 
 
 
40
 
 
1
+ # SMART - Interfaz tipo ChatGPT usando Gradio
2
+
3
  import gradio as gr
4
+ import requests
5
 
6
+ API_URL = "https://api-inference.huggingface.co/models/HuggingFaceH4/zephyr-7b-beta"
7
+ HEADERS = {"Authorization": f"Bearer TU_TOKEN_AQUI"} # <- Pega tu token aqu铆
 
8
 
9
+ # Funci贸n para enviar mensaje a la API de Hugging Face
10
+ def chat_with_smart(message, history):
11
+ history = history or []
12
+ full_prompt = "\n".join([f"Usuario: {msg}\nAsistente: {resp}" for msg, resp in history])
13
+ full_prompt += f"\nUsuario: {message}\nAsistente:"
14
 
 
15
  try:
16
+ response = requests.post(API_URL, headers=HEADERS, json={"inputs": full_prompt})
17
+ response.raise_for_status()
18
+ output = response.json()
19
+
20
+ if isinstance(output, dict) and "error" in output:
21
+ return history, f"[ERROR]: {output['error']}"
22
+
23
+ generated = output[0]["generated_text"][len(full_prompt):].strip()
24
+ history.append((message, generated))
25
+ return history, generated
26
+
27
  except Exception as e:
28
+ return history, f"[ERROR de conexi贸n]: {str(e)}"
29
+
30
+ # Interfaz visual tipo ChatGPT
31
+ def build_interface():
32
+ with gr.Blocks(theme=gr.themes.Base(), title="SMART") as demo:
33
+ gr.Markdown("# 馃 SMART - Tu Asistente Inteligente")
34
+
35
+ with gr.Row():
36
+ with gr.Column(scale=1):
37
+ gr.Markdown("### 馃搨 Biblioteca")
38
+ search = gr.Textbox(placeholder="馃攳 Buscar chats...", show_label=False)
39
+ new_chat_btn = gr.Button("+ Nuevo Chat")
40
+ chat_list = gr.Dataset(components=[gr.Textbox()], label="Historial", samples=[])
41
+
42
+ with gr.Column(scale=4):
43
+ chatbot = gr.Chatbot()
44
+ msg = gr.Textbox(placeholder="Escribe un mensaje...", label="Mensaje")
45
+ send = gr.Button("Enviar")
46
 
47
+ state = gr.State([]) # historial
 
 
 
 
 
 
48
 
49
+ send.click(fn=chat_with_smart, inputs=[msg, state], outputs=[state, chatbot])
50
+ new_chat_btn.click(lambda: ([], None), outputs=[state, chatbot])
51
 
52
+ return demo
53
 
54
+ # Ejecutar la app
55
+ if __name__ == "__main__":
56
+ build_interface().launch()
57