Spaces:
Runtime error
Runtime error
Download app.py from mlfonso18/Prompt_IA2: direct link, hf CLI and curl.
- Browser
- Download file 1.88 kB
-
https://huggingface.co/spaces/mlfonso18/Prompt_IA2/resolve/main/app.py
- Command line
-
hf download hf://spaces/mlfonso18/Prompt_IA2/app.py
-
curl -L -o app.py https://huggingface.co/spaces/mlfonso18/Prompt_IA2/resolve/main/app.py
1.88 kB
| import gradio as gr | |
| from transformers import AutoModelForCausalLM, AutoTokenizer | |
| import torch | |
| # Lista de modelos gratuitos y ligeros | |
| MODELOS = { | |
| "GPT-2": "gpt2", | |
| "DistilGPT-2": "distilgpt2", | |
| "GPT-Neo 125M": "EleutherAI/gpt-neo-125M", | |
| "GPT-Neo 1.3B": "EleutherAI/gpt-neo-1.3B", | |
| "Bloom 560M": "bigscience/bloom-560m" | |
| } | |
| # Cargar modelos y tokenizadores (cache) | |
| cache_modelos = {} | |
| for nombre, modelo in MODELOS.items(): | |
| print(f"Cargando {nombre}...") | |
| tokenizer = AutoTokenizer.from_pretrained(modelo) | |
| model = AutoModelForCausalLM.from_pretrained(modelo) | |
| cache_modelos[nombre] = (tokenizer, model) | |
| def generar_texto(texto_usuario, modelo_seleccionado): | |
| tokenizer, model = cache_modelos[modelo_seleccionado] | |
| entradas = tokenizer.encode(texto_usuario, return_tensors="pt") | |
| salida = model.generate( | |
| entradas, | |
| max_length=150, | |
| do_sample=True, | |
| top_p=0.95, | |
| top_k=60, | |
| temperature=0.8, | |
| pad_token_id=tokenizer.eos_token_id | |
| ) | |
| respuesta = tokenizer.decode(salida[0], skip_special_tokens=True) | |
| return respuesta | |
| # Interfaz Gradio | |
| interfaz = gr.Interface( | |
| fn=generar_texto, | |
| inputs=[ | |
| gr.Textbox(label="Escribe tu texto aquí", placeholder="Escribe algo..."), | |
| gr.Dropdown(choices=list(MODELOS.keys()), label="Elige el modelo de IA") | |
| ], | |
| outputs="text", | |
| title="📝 Generador de Texto con 5 IA Gratuitas", | |
| description="¡Escribe tu texto y elige entre 5 modelos gratuitos de IA para generar contenido creativo automáticamente!", | |
| examples=[ | |
| ["Escribe un cuento corto sobre un robot que aprende a bailar", "GPT-2"], | |
| ["Genera una descripción creativa para un juego de fantasía", "GPT-Neo 125M"], | |
| ["Escribe un mensaje motivacional para estudiantes", "DistilGPT-2"] | |
| ], | |
| allow_flagging="never" | |
| ) | |
| interfaz.launch() | |