Update app.py
Browse files
app.py
CHANGED
|
@@ -1,28 +1,30 @@
|
|
| 1 |
-
# SMART - Interfaz tipo ChatGPT usando Gradio
|
| 2 |
|
| 3 |
import gradio as gr
|
| 4 |
import requests
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
|
|
|
| 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 |
-
|
| 13 |
-
|
| 14 |
|
| 15 |
try:
|
| 16 |
-
response = requests.post(API_URL, headers=HEADERS, json={"inputs":
|
| 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 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
| 26 |
|
| 27 |
except Exception as e:
|
| 28 |
return history, f"[ERROR de conexi贸n]: {str(e)}"
|
|
@@ -44,7 +46,7 @@ def build_interface():
|
|
| 44 |
msg = gr.Textbox(placeholder="Escribe un mensaje...", label="Mensaje")
|
| 45 |
send = gr.Button("Enviar")
|
| 46 |
|
| 47 |
-
state = gr.State([])
|
| 48 |
|
| 49 |
send.click(fn=chat_with_smart, inputs=[msg, state], outputs=[state, chatbot])
|
| 50 |
new_chat_btn.click(lambda: ([], None), outputs=[state, chatbot])
|
|
|
|
| 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_tu_token_aqui"} # <-- Reemplaza con tu token real
|
| 9 |
|
| 10 |
# Funci贸n para enviar mensaje a la API de Hugging Face
|
| 11 |
def chat_with_smart(message, history):
|
| 12 |
history = history or []
|
| 13 |
+
prompt = "".join([f"Usuario: {msg}\nAsistente: {res}\n" for msg, res in history])
|
| 14 |
+
prompt += f"Usuario: {message}\nAsistente:"
|
| 15 |
|
| 16 |
try:
|
| 17 |
+
response = requests.post(API_URL, headers=HEADERS, json={"inputs": prompt})
|
| 18 |
response.raise_for_status()
|
| 19 |
output = response.json()
|
| 20 |
|
| 21 |
if isinstance(output, dict) and "error" in output:
|
| 22 |
return history, f"[ERROR]: {output['error']}"
|
| 23 |
|
| 24 |
+
text = output[0].get("generated_text", "[Respuesta vac铆a]")
|
| 25 |
+
reply = text[len(prompt):].strip()
|
| 26 |
+
history.append((message, reply))
|
| 27 |
+
return history, reply
|
| 28 |
|
| 29 |
except Exception as e:
|
| 30 |
return history, f"[ERROR de conexi贸n]: {str(e)}"
|
|
|
|
| 46 |
msg = gr.Textbox(placeholder="Escribe un mensaje...", label="Mensaje")
|
| 47 |
send = gr.Button("Enviar")
|
| 48 |
|
| 49 |
+
state = gr.State([])
|
| 50 |
|
| 51 |
send.click(fn=chat_with_smart, inputs=[msg, state], outputs=[state, chatbot])
|
| 52 |
new_chat_btn.click(lambda: ([], None), outputs=[state, chatbot])
|