iaia / app.py
88ggg's picture
Update app.py
fced669 verified
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
from threading import Thread
import gradio as gr
# Configuración del modelo
MODEL_NAME = "microsoft/phi-2" # Modelo ligero y eficiente
# Cargar modelo y tokenizer
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME, trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained(
MODEL_NAME,
torch_dtype=torch.float16,
device_map="auto"
)
def generate_response(message, history, system_prompt="Eres un asistente útil llamado Gerardo."):
# Formatear el historial de chat
messages = [{"role": "system", "content": system_prompt}]
for user_msg, bot_msg in history:
messages.extend([
{"role": "user", "content": user_msg},
{"role": "assistant", "content": bot_msg}
])
messages.append({"role": "user", "content": message})
# Generar respuesta con streaming
inputs = tokenizer.apply_chat_template(messages, return_tensors="pt").to(model.device)
streamer = TextIteratorStreamer(tokenizer, skip_prompt=True)
Thread(
target=model.generate,
kwargs=dict(
inputs,
streamer=streamer,
max_new_tokens=512,
temperature=0.7,
do_sample=True
)
).start()
# Retornar la respuesta progresivamente
response = ""
for token in streamer:
response += token
yield response
# Interfaz con Gradio
demo = gr.ChatInterface(
fn=generate_response,
title="🤖 Chatbot Gerardo",
description="Un asistente IA creado por Gerardo. ¡Pregúntame lo que quieras!",
examples=["Hola", "¿Cómo funciona la inteligencia artificial?"],
additional_inputs=[
gr.Textbox(
"Eres un asistente útil llamado Gerardo .",
label="Personalidad del bot"
)
],
theme="soft"
)
if __name__ == "__main__":
demo.launch()