Download app.py from Precisar/amiya-bot: direct link, hf CLI and curl.
- Browser
- Download file 2.6 kB
-
https://huggingface.co/spaces/Precisar/amiya-bot/resolve/main/app.py
- Command line
-
hf download hf://spaces/Precisar/amiya-bot/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Precisar/amiya-bot/resolve/main/app.py
2.6 kB
| import os | |
| import gradio as gr | |
| from huggingface_hub import InferenceClient | |
| # Modelo abierto alojado en Hugging Face | |
| # (si este diera problemas luego podemos cambiarlo por otro) | |
| MODEL_ID = "HuggingFaceH4/zephyr-7b-beta" | |
| HF_TOKEN = os.environ.get("HF_TOKEN") | |
| if HF_TOKEN is None: | |
| raise ValueError( | |
| "No se encontró el token HF_TOKEN. " | |
| "Ve a Settings → Variables and secrets y añade tu token de Hugging Face." | |
| ) | |
| client = InferenceClient(MODEL_ID, token=HF_TOKEN) | |
| SYSTEM_PROMPT = """ | |
| Eres Amiya, una asistente latinoamericana de educación mediática, | |
| cultura digital y derechos en internet. | |
| Respondes SIEMPRE en español claro, con ejemplos cotidianos. | |
| No usas tecnicismos innecesarios y hablas con calma. | |
| Si una pregunta se sale de tu tema (política partidista, salud específica, etc.), | |
| invitas a buscar ayuda experta. | |
| Si algo no está claro o no tienes información suficiente, lo dices sin inventar. | |
| """ | |
| def amiya_chat(message, history): | |
| messages = [{"role": "system", "content": SYSTEM_PROMPT}] | |
| for user_msg, bot_msg in history: | |
| if user_msg: | |
| messages.append({"role": "user", "content": user_msg}) | |
| if bot_msg: | |
| messages.append({"role": "assistant", "content": bot_msg}) | |
| messages.append({"role": "user", "content": message}) | |
| try: | |
| completion = client.chat_completion( | |
| messages=messages, | |
| max_tokens=400, | |
| temperature=0.4, | |
| ) | |
| # Soportar tanto respuesta tipo objeto como tipo dict | |
| choice = completion.choices[0] | |
| if hasattr(choice, "message"): | |
| reply = getattr(choice.message, "content", None) or choice.message["content"] | |
| else: | |
| reply = choice["message"]["content"] | |
| except Exception as e: | |
| # Mostrar el error dentro del chat para poder entender qué pasa | |
| reply = f"⚠️ Hubo un error al llamar al modelo: {e}" | |
| history.append((message, reply)) | |
| return "", history | |
| with gr.Blocks() as demo: | |
| gr.Markdown( | |
| "# Amiya – menos ruido, más criterio\n" | |
| "Bot de cultura digital y educación mediática para América Latina y el Caribe." | |
| ) | |
| chat = gr.Chatbot(label="Chat con Amiya", height=450) | |
| msg = gr.Textbox( | |
| label="Escribe tu pregunta", | |
| placeholder="Pregúntale a Amiya sobre IA, desinformación, algoritmos, bienestar digital…", | |
| ) | |
| clear = gr.Button("Limpiar conversación") | |
| msg.submit(amiya_chat, [msg, chat], [msg, chat]) | |
| clear.click(lambda: ("", []), None, [msg, chat]) | |
| if __name__ == "__main__": | |
| demo.launch() | |