Spaces:
Sleeping
Sleeping
Download app.py from Alejandrott24/IAPrub: direct link, hf CLI and curl.
- Browser
- Download file 1.72 kB
-
https://huggingface.co/spaces/Alejandrott24/IAPrub/resolve/main/app.py
- Command line
-
hf download hf://spaces/Alejandrott24/IAPrub/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Alejandrott24/IAPrub/resolve/main/app.py
1.72 kB
| import gradio as gr | |
| import json | |
| from transformers import AutoTokenizer, AutoModelForCausalLM | |
| import torch | |
| print("Cargando Qwen2.5-Coder-1.5B-Instruct...") | |
| modelo_nombre = "Qwen/Qwen2.5-Coder-1.5B-Instruct" | |
| tokenizer = AutoTokenizer.from_pretrained(modelo_nombre, trust_remote_code=True) | |
| modelo = AutoModelForCausalLM.from_pretrained( | |
| modelo_nombre, | |
| torch_dtype=torch.float32, | |
| low_cpu_mem_usage=True, | |
| trust_remote_code=True | |
| ) | |
| print("✅ Qwen2.5-Coder listo!") | |
| def chat_ia(datos_json): | |
| try: | |
| datos = json.loads(datos_json) | |
| mensaje = datos.get("prompt", "") | |
| max_tokens = datos.get("max_tokens", 300) | |
| # Formato de chat de Qwen | |
| messages = [{"role": "user", "content": mensaje}] | |
| texto = tokenizer.apply_chat_template( | |
| messages, | |
| tokenize=False, | |
| add_generation_prompt=True | |
| ) | |
| inputs = tokenizer([texto], return_tensors="pt") | |
| outputs = modelo.generate( | |
| **inputs, | |
| max_new_tokens=max_tokens, | |
| temperature=0.7, | |
| do_sample=True, | |
| top_p=0.95 | |
| ) | |
| respuesta = tokenizer.decode(outputs[0][len(inputs[0]):], skip_special_tokens=True) | |
| return json.dumps({ | |
| "status": "ok", | |
| "respuesta": respuesta, | |
| "modelo": "Qwen2.5-Coder-1.5B" | |
| }, indent=2) | |
| except Exception as e: | |
| return json.dumps({"status": "error", "mensaje": str(e)}) | |
| demo = gr.Interface( | |
| fn=chat_ia, | |
| inputs=gr.Textbox(label="JSON Input", lines=5), | |
| outputs=gr.Textbox(label="Respuesta", lines=15), | |
| title="Qwen2.5-Coder - Rápido y Conversacional" | |
| ) | |
| demo.launch() |