Spaces:
Running
Running
Upload 2 files
Browse files- app.py +51 -0
- requirements.txt +2 -0
app.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Chat web con Minerva v0.1 (https://huggingface.co/AxoneSystem/Minerva-v0.1)."""
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import torch
|
| 4 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 5 |
+
|
| 6 |
+
REPO = "AxoneSystem/Minerva-v0.1"
|
| 7 |
+
tok = AutoTokenizer.from_pretrained(REPO)
|
| 8 |
+
modelo = AutoModelForCausalLM.from_pretrained(REPO).eval()
|
| 9 |
+
CONTEXTO = 512 # tokens que Minerva puede ver a la vez
|
| 10 |
+
MAX_RESPUESTA = 200
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def a_mensajes(historial):
|
| 14 |
+
"""Acepta el historial de Gradio en formato de mensajes o de pares (según la versión)."""
|
| 15 |
+
mensajes = []
|
| 16 |
+
for item in historial or []:
|
| 17 |
+
if isinstance(item, dict):
|
| 18 |
+
if item.get("role") in ("user", "assistant") and isinstance(item.get("content"), str):
|
| 19 |
+
mensajes.append({"role": item["role"], "content": item["content"]})
|
| 20 |
+
else:
|
| 21 |
+
usuario, asistente = item[0], item[1]
|
| 22 |
+
if usuario:
|
| 23 |
+
mensajes.append({"role": "user", "content": usuario})
|
| 24 |
+
if asistente:
|
| 25 |
+
mensajes.append({"role": "assistant", "content": asistente})
|
| 26 |
+
return mensajes
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def responder(mensaje, historial, temperatura):
|
| 30 |
+
mensajes = a_mensajes(historial) + [{"role": "user", "content": mensaje}]
|
| 31 |
+
entrada = tok.apply_chat_template(mensajes, add_generation_prompt=True, return_tensors="pt", return_dict=True)
|
| 32 |
+
ids = entrada["input_ids"][:, -(CONTEXTO - MAX_RESPUESTA):] # solo recuerda lo último
|
| 33 |
+
with torch.no_grad():
|
| 34 |
+
salida = modelo.generate(input_ids=ids, attention_mask=torch.ones_like(ids), max_new_tokens=MAX_RESPUESTA,
|
| 35 |
+
do_sample=True, temperature=temperatura, top_k=30)
|
| 36 |
+
return tok.decode(salida[0, ids.shape[1]:], skip_special_tokens=True).strip()
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
demo = gr.ChatInterface(
|
| 40 |
+
responder,
|
| 41 |
+
title="Minerva v0.1 🇪🇸",
|
| 42 |
+
description=("Un modelo de lenguaje en español diminuto (30M de parámetros), entrenado desde cero en un portátil. "
|
| 43 |
+
"Habla bien pero **se lo inventa casi todo**: es un modelo de juguete. "
|
| 44 |
+
"[Ver el modelo](https://huggingface.co/AxoneSystem/Minerva-v0.1)"),
|
| 45 |
+
examples=[["¿Quién eres?", 0.7], ["¿Por qué te llamas Minerva?", 0.7], ["¿Qué es la paella?", 0.7],
|
| 46 |
+
["Cuéntame un cuento corto.", 0.7]],
|
| 47 |
+
additional_inputs=[gr.Slider(0.1, 1.5, value=0.7, step=0.1, label="Temperatura (más alta = más loca)")],
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
if __name__ == "__main__":
|
| 51 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch
|
| 2 |
+
transformers
|