Spaces:
Sleeping
Sleeping
Download app.py from mlfonso18/Prompt_Final: direct link, hf CLI and curl.
- Browser
- Download file 1.74 kB
-
https://huggingface.co/spaces/mlfonso18/Prompt_Final/resolve/main/app.py
- Command line
-
hf download hf://spaces/mlfonso18/Prompt_Final/app.py
-
curl -L -o app.py https://huggingface.co/spaces/mlfonso18/Prompt_Final/resolve/main/app.py
1.74 kB
| import os | |
| import requests | |
| import gradio as gr | |
| # Token desde Secrets | |
| HF_TOKEN = os.environ.get("HF_TOKEN") | |
| API_URL = "https://api-inference.huggingface.co/models/microsoft/phi-2" | |
| HEADERS = { | |
| "Authorization": f"Bearer {HF_TOKEN}", | |
| "Content-Type": "application/json" | |
| } | |
| def generar_texto(pregunta): | |
| if not pregunta.strip(): | |
| return "❗ Escribe una pregunta." | |
| prompt = ( | |
| "Responde de forma clara, sencilla y educativa.\n" | |
| f"Pregunta: {pregunta}\n" | |
| "Respuesta:" | |
| ) | |
| payload = { | |
| "inputs": prompt, | |
| "parameters": { | |
| "max_new_tokens": 150, | |
| "temperature": 0.6, | |
| "top_p": 0.9, | |
| "return_full_text": False | |
| } | |
| } | |
| try: | |
| r = requests.post(API_URL, headers=HEADERS, json=payload, timeout=60) | |
| if r.status_code == 200: | |
| return r.json()[0]["generated_text"] | |
| else: | |
| return "⚠️ El modelo está ocupado. Intenta nuevamente." | |
| except Exception: | |
| return "⚠️ Error de conexión con el modelo." | |
| with gr.Blocks() as demo: | |
| gr.Markdown("# 🤖 IA Educativa Gratuita") | |
| gr.Markdown("## Modelo: Phi-2 (estable y sin costo)") | |
| entrada = gr.Textbox( | |
| label="Escribe tu pregunta", | |
| placeholder="Ej: ¿Qué es la fotosíntesis?", | |
| lines=3 | |
| ) | |
| salida = gr.Textbox( | |
| label="Respuesta de la IA", | |
| lines=8 | |
| ) | |
| boton = gr.Button("Generar respuesta") | |
| boton.click( | |
| fn=generar_texto, | |
| inputs=entrada, | |
| outputs=salida | |
| ) | |
| demo.launch( | |
| server_name="0.0.0.0", | |
| server_port=7860, | |
| css=""" | |
| h1 { font-size: 34px; } | |
| h2 { font-size: 22px; } | |
| label { font-size: 16px; } | |
| """ | |
| ) | |