Spaces:
Sleeping
Sleeping
File size: 1,739 Bytes
660ad59 346864b 660ad59 346864b 660ad59 346864b 660ad59 346864b 660ad59 346864b 660ad59 346864b 660ad59 346864b 660ad59 84d9901 660ad59 346864b 660ad59 346864b 660ad59 346864b 660ad59 b40c6e0 84d9901 346864b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | 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; }
"""
)
|