maxxxi100 commited on
Commit
0d8ffe1
·
verified ·
1 Parent(s): 266e6c2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +78 -129
app.py CHANGED
@@ -1,155 +1,104 @@
 
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
3
- from fastapi import FastAPI, Request
4
  from fastapi.middleware.cors import CORSMiddleware
5
- import os
6
-
7
- # ==========================================================
8
- # 1️⃣ CONFIGURACIÓN DEL MODELO
9
- # ==========================================================
10
-
11
- MODEL_NAME = "meta-llama/Llama-3-8b-instruct"
12
-
13
- def generate_llm_response(message, system_message, max_tokens=512, temperature=0.7, top_p=0.95, hf_token=None):
14
- """Genera una respuesta real usando el modelo LLM de Hugging Face."""
15
- messages = [
16
- {"role": "system", "content": system_message},
17
- {"role": "user", "content": message}
18
- ]
19
- try:
20
- client = InferenceClient(
21
- model=MODEL_NAME,
22
- token=hf_token or os.getenv("HF_TOKEN")
23
- )
24
- response = ""
25
- for chunk in client.chat_completion(
26
- messages=messages,
27
- max_tokens=max_tokens,
28
- temperature=temperature,
29
- top_p=top_p,
30
- stream=True,
31
- ):
32
- delta = chunk.choices[0].delta.get("content", "")
33
- response += delta
34
- return response.strip()
35
- except Exception as e:
36
- return f"⚠️ Error al conectar amb el model: {e}"
37
-
38
-
39
- # ==========================================================
40
- # 2️⃣ FUNCIÓN PARA GRADIO
41
- # ==========================================================
42
-
43
- def respond(
44
- message,
45
- history: list[dict[str, str]],
46
- system_message,
47
- max_tokens,
48
- temperature,
49
- top_p,
50
- hf_token: gr.OAuthToken,
51
- ):
52
- """Función de respuesta para el chat dentro de Gradio."""
53
- try:
54
- client = InferenceClient(
55
- model=MODEL_NAME,
56
- token=hf_token.token
57
- )
58
- messages = [{"role": "system", "content": system_message}]
59
- messages.extend(history)
60
- messages.append({"role": "user", "content": message})
61
-
62
- for chunk in client.chat_completion(
63
- messages=messages,
64
- max_tokens=max_tokens,
65
- temperature=temperature,
66
- top_p=top_p,
67
- stream=True,
68
- ):
69
- delta = chunk.choices[0].delta.get("content", "")
70
- yield delta
71
- except Exception as e:
72
- yield f"⚠️ Error al conectar amb el model: {e}"
73
-
74
-
75
- # ==========================================================
76
- # 3️⃣ INTERFAZ DE GRADIO (tipo mensajes moderno)
77
- # ==========================================================
78
-
79
  chatbot_llm = gr.ChatInterface(
80
- fn=respond,
81
- type="messages", # ✅ formato nuevo sin warning
82
  textbox=gr.Textbox(placeholder="Escriu la teva pregunta al LLM...", container=False, scale=7),
83
  theme="soft",
84
- title="Asistent LLM (Hugging Face Client)",
 
85
  additional_inputs=[
86
- gr.Textbox(value="Ets l'assistent sanitari de My Health. Respon en català, de manera concisa i útil.", label="Missatge del sistema"),
87
  gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Tokens màxims"),
88
  gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperatura"),
89
  gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (mostreig nucli)"),
90
  ],
91
  )
92
 
 
 
 
 
93
 
94
- # ==========================================================
95
- # 4️⃣ FASTAPI BACKEND
96
- # ==========================================================
 
 
 
 
 
97
 
98
- app = FastAPI(title="My Health API")
 
 
 
99
 
100
- # Permitir peticiones CORS
101
  app.add_middleware(
102
  CORSMiddleware,
103
- allow_origins=["*"], # en producción, limitar a tu dominio
104
- allow_credentials=True,
105
  allow_methods=["*"],
106
  allow_headers=["*"],
107
  )
108
 
109
- @app.post("/chat")
110
- async def chat_endpoint(request: Request):
111
- """Recibe JSON {message: "..."} y responde con el LLM."""
112
- data = await request.json()
113
- message = data.get("message", "")
114
- system_message = data.get("system_message", "Ets l'assistent sanitari de My Health. Respon en català.")
115
-
116
- if not message:
117
- return {"response": "⚠️ Missatge buit"}
118
-
119
- response = generate_llm_response(message, system_message)
120
- return {"response": response}
121
-
122
-
123
- # ==========================================================
124
- # 5️⃣ DASHBOARD HTML
125
- # ==========================================================
126
-
127
- HTML_FILE_PATH = "My_health.html"
128
-
129
- def load_html_content():
130
- if not os.path.exists(HTML_FILE_PATH):
131
- return "<h1>Error: archivo HTML no encontrado.</h1>"
132
- with open(HTML_FILE_PATH, "r", encoding="utf-8") as f:
133
- return f.read()
134
-
135
-
136
- # ==========================================================
137
- # 6️⃣ GRADIO UI
138
- # ==========================================================
139
-
140
- with gr.Blocks() as demo:
141
- gr.Markdown("# 🩺 My Health - Integració Gradio + API + HTML")
142
-
143
- with gr.Sidebar():
144
- gr.LoginButton()
145
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
146
  with gr.Tabs():
147
- with gr.TabItem("🏠 Dashboard HTML"):
148
  gr.HTML(value=load_html_content())
149
- gr.Markdown("Aquesta pestanya mostra el **dashboard** amb el chat flotant.")
150
-
151
- with gr.TabItem("💬 Chat LLM"):
152
  chatbot_llm.render()
153
 
154
- # ✅ Montamos Gradio en FastAPI sin reasignar app
155
- gr.mount_gradio_app(app, demo, path="/")
 
 
 
 
 
 
 
1
+ import os
2
  import gradio as gr
3
+ from fastapi import FastAPI
 
4
  from fastapi.middleware.cors import CORSMiddleware
5
+ from fastapi.responses import HTMLResponse
6
+ import uvicorn
7
+
8
+ # ===============================
9
+ # 1️⃣ Función de Respuesta LLM (Mock)
10
+ # ===============================
11
+ def respond(message, history, system_message, max_tokens, temperature, top_p):
12
+ """
13
+ Simula una respuesta de un LLM.
14
+ """
15
+ messages = [{"role": "system", "content": system_message}]
16
+ messages.extend(history)
17
+ messages.append({"role": "user", "content": message})
18
+
19
+ if message.lower().strip() in ["hola", "hi"]:
20
+ response = "Hola Max 000! Soc un chatbot basat en LLM. Com et puc ajudar amb la teva salut avui?"
21
+ elif "informació" in message.lower():
22
+ response = "La informació que cerques es pot trobar a la secció d'informes o diagnòstics."
23
+ else:
24
+ response = f"He rebut el teu missatge: '{message}'. Prova preguntant sobre cites o historial clínic."
25
+
26
+ return [{"role": "assistant", "content": response}]
27
+
28
+ # ===============================
29
+ # 2️⃣ Gradio Chat Interface
30
+ # ===============================
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
  chatbot_llm = gr.ChatInterface(
32
+ respond,
 
33
  textbox=gr.Textbox(placeholder="Escriu la teva pregunta al LLM...", container=False, scale=7),
34
  theme="soft",
35
+ title="Asistente LLM (Hugging Face Client)",
36
+ type="messages", # ✅ Corrección para evitar warning
37
  additional_inputs=[
38
+ gr.Textbox(value="Ets l'assistent sanitari de My health. Respon en català, de manera concisa i útil.", label="Missatge del sistema"),
39
  gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Tokens màxims"),
40
  gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperatura"),
41
  gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (mostreig nucli)"),
42
  ],
43
  )
44
 
45
+ # ===============================
46
+ # 3️⃣ Función para cargar HTML Dashboard
47
+ # ===============================
48
+ HTML_FILE_PATH = "My_health.html"
49
 
50
+ def load_html_content():
51
+ try:
52
+ if not os.path.exists(HTML_FILE_PATH):
53
+ return f"<h1>Error: Archivo {HTML_FILE_PATH} no encontrado.</h1>"
54
+ with open(HTML_FILE_PATH, "r", encoding="utf-8") as f:
55
+ return f.read()
56
+ except Exception as e:
57
+ return f"<h1>Error al cargar el HTML:</h1><p>{e}</p>"
58
 
59
+ # ===============================
60
+ # 4️⃣ FastAPI App para API y HTML
61
+ # ===============================
62
+ app = FastAPI()
63
 
64
+ # Permitir requests desde cualquier frontend
65
  app.add_middleware(
66
  CORSMiddleware,
67
+ allow_origins=["*"],
 
68
  allow_methods=["*"],
69
  allow_headers=["*"],
70
  )
71
 
72
+ @app.get("/", response_class=HTMLResponse)
73
+ async def home():
74
+ return load_html_content()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75
 
76
+ @app.post("/chat")
77
+ async def chat_endpoint(payload: dict):
78
+ message = payload.get("message", "")
79
+ system_message = payload.get("system_message", "Ets l'assistent sanitari de My health. Respon en català, de manera concisa i útil.")
80
+ response = respond(message, history=[], system_message=system_message, max_tokens=512, temperature=0.7, top_p=0.95)
81
+ return {"response": response[0]["content"]}
82
+
83
+ # ===============================
84
+ # 5️⃣ Gradio + FastAPI Integration
85
+ # ===============================
86
+ from fastapi.middleware.wsgi import WSGIMiddleware
87
+
88
+ gradio_app = gr.Blocks()
89
+ with gradio_app:
90
+ gr.Markdown("# Aplicació My Health - Integració Gradio/HTML")
91
  with gr.Tabs():
92
+ with gr.TabItem("Dashboard LMS (UI Estàtica amb Chat Flotant)"):
93
  gr.HTML(value=load_html_content())
94
+ with gr.TabItem("Chat LLM (Accés Directe a Model)"):
 
 
95
  chatbot_llm.render()
96
 
97
+ app.mount("/gradio", WSGIMiddleware(gradio_app))
98
+
99
+ # ===============================
100
+ # 6️⃣ Launch Uvicorn (Red / Contenedor)
101
+ # ===============================
102
+ if __name__ == "__main__":
103
+ port = int(os.environ.get("PORT", 8000))
104
+ uvicorn.run(app, host="0.0.0.0", port=port)