Download app.py from mlfonso18/PROMPT: direct link, hf CLI and curl.
- Browser
- Download file 1.31 kB
-
https://huggingface.co/spaces/mlfonso18/PROMPT/resolve/main/app.py
- Command line
-
hf download hf://spaces/mlfonso18/PROMPT/app.py
-
curl -L -o app.py https://huggingface.co/spaces/mlfonso18/PROMPT/resolve/main/app.py
1.31 kB
| import gradio as gr | |
| from transformers import AutoModelForCausalLM, AutoTokenizer | |
| import torch | |
| # CPU (estable en HF Free) | |
| device = torch.device("cpu") | |
| # Modelo en español | |
| MODELO = "mrm8488/spanish-gpt2" | |
| # Cargar modelo | |
| tokenizer = AutoTokenizer.from_pretrained(MODELO) | |
| model = AutoModelForCausalLM.from_pretrained(MODELO) | |
| model.to(device) | |
| model.eval() | |
| def generar_texto(texto_usuario): | |
| if not texto_usuario.strip(): | |
| return "⚠️ Escribe un texto para que la IA continúe." | |
| entradas = tokenizer.encode(texto_usuario, return_tensors="pt").to(device) | |
| salida = model.generate( | |
| entradas, | |
| max_new_tokens=120, | |
| do_sample=True, | |
| temperature=0.7, | |
| top_p=0.9, | |
| repetition_penalty=1.3, | |
| no_repeat_ngram_size=3, | |
| pad_token_id=tokenizer.eos_token_id | |
| ) | |
| return tokenizer.decode(salida[0], skip_special_tokens=True) | |
| # Interfaz Gradio | |
| interfaz = gr.Interface( | |
| fn=generar_texto, | |
| inputs=gr.Textbox( | |
| label="Escribe tu texto", | |
| placeholder="Ejemplo: Érase una vez en Panamá un niño que descubrió un robot…" | |
| ), | |
| outputs="text", | |
| title="✍️ Generador de Texto en Español con IA", | |
| description="Modelo entrenado en español, ideal para cuentos y trabajos escolares." | |
| ) | |
| interfaz.launch() | |