maxxxi100's picture
Update app.py
6a250ce verified
import gradio as gr
import os
# -------------------------
# 1️⃣ Lógica del chat
# -------------------------
chat_history = []
def respond(message, history, system_message="Ets l'assistent sanitari de My Health. Respon en català, de manera concisa i útil."):
global chat_history
chat_history.append({"role": "user", "content": message})
if message.lower().strip() in ["hola", "hi"]:
reply = "Hola! Soc un chatbot basat en LLM. Com et puc ajudar amb la teva salut avui?"
elif "informació" in message.lower():
reply = "La informació que cerques es pot trobar a la secció d'informes o diagnòstics."
else:
reply = f"He rebut el teu missatge: '{message}'. Pots provar amb una pregunta sobre el teu historial clínic."
chat_history.append({"role": "assistant", "content": reply})
return reply
# -------------------------
# 2️⃣ Función para API (HTML flotante)
# -------------------------
def api_chat(message):
return respond(message, chat_history)
# -------------------------
# 3️⃣ Cargar HTML
# -------------------------
HTML_FILE_PATH = "My_health.html"
def load_html_content():
if not os.path.exists(HTML_FILE_PATH):
return "<h1>Error: archivo HTML no encontrado.</h1>"
with open(HTML_FILE_PATH, "r", encoding="utf-8") as f:
return f.read()
# -------------------------
# 4️⃣ Interfaz Gradio
# -------------------------
chatbot_llm = gr.ChatInterface(
fn=respond,
textbox=gr.Textbox(placeholder="Escriu la teva pregunta al LLM...", container=False, scale=7),
theme="soft",
title="Asistent LLM",
additional_inputs=[
gr.Textbox(value="Ets l'assistent sanitari de My Health. Respon en català, de manera concisa i útil.", label="Missatge del sistema"),
],
)
with gr.Blocks() as demo:
gr.Markdown("# My Health Dashboard - HTML + LLM")
with gr.Tabs():
with gr.TabItem("Dashboard HTML + Chat Flotant"):
gr.HTML(load_html_content())
with gr.TabItem("Chat LLM"):
chatbot_llm.render()
with gr.TabItem("API Endpoint Simulado"):
gr.Markdown("Aquest endpoint es pot cridar des del teu HTML flotant: `api_chat(message)`")
# -------------------------
# 5️⃣ Lanzamiento
# -------------------------
if __name__ == "__main__":
demo.launch(server_name="0.0.0.0", server_port=7860, share=True)