Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
|
| 3 |
+
# Usamos el cliente de Hugging Face para conectar un modelo libre e ilimitado
|
| 4 |
+
# En este caso, usamos un modelo potente y rápido llamado 'Meta-Llama-3-8B-Instruct'
|
| 5 |
+
import os
|
| 6 |
+
from huggingface_hub import InferenceClient
|
| 7 |
+
|
| 8 |
+
client = InferenceClient("meta-llama/Meta-Llama-3-8B-Instruct")
|
| 9 |
+
|
| 10 |
+
def responder(message, history):
|
| 11 |
+
# Formateamos el historial para que la IA entienda el contexto
|
| 12 |
+
messages = []
|
| 13 |
+
for user_msg, ai_msg in history:
|
| 14 |
+
messages.append({"role": "user", "content": user_msg})
|
| 15 |
+
messages.append({"role": "assistant", "content": ai_msg})
|
| 16 |
+
|
| 17 |
+
messages.append({"role": "user", "content": message})
|
| 18 |
+
|
| 19 |
+
# Llamamos a la IA
|
| 20 |
+
response = ""
|
| 21 |
+
for message in client.chat_completion(
|
| 22 |
+
messages,
|
| 23 |
+
max_tokens=512,
|
| 24 |
+
stream=True,
|
| 25 |
+
):
|
| 26 |
+
token = message.choices[0].delta.content
|
| 27 |
+
if token:
|
| 28 |
+
response += token
|
| 29 |
+
yield response
|
| 30 |
+
|
| 31 |
+
# Creamos la interfaz de chat ilimitada
|
| 32 |
+
demo = gr.ChatInterface(
|
| 33 |
+
fn=responder,
|
| 34 |
+
title="🚀 Mi Chat de IA Ilimitado",
|
| 35 |
+
description="Este chat corre en un servidor gratuito de Hugging Face y no tiene límites de uso.",
|
| 36 |
+
textbox=gr.Textbox(placeholder="Escribe lo que quieras...")
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
if __name__ == "__main__":
|
| 40 |
+
demo.queue().launch()
|