Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,80 +1,140 @@
|
|
| 1 |
-
# app.py — DeepSeek-OCR +
|
| 2 |
import os, tempfile, traceback
|
| 3 |
import gradio as gr
|
| 4 |
import torch
|
| 5 |
from PIL import Image
|
| 6 |
-
from transformers import AutoModel, AutoTokenizer
|
| 7 |
import spaces
|
| 8 |
-
from huggingface_hub import hf_hub_download
|
| 9 |
-
from llama_cpp import Llama
|
| 10 |
|
| 11 |
# ===============================================================
|
| 12 |
-
# CHAT:
|
| 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 |
-
return
|
| 60 |
-
|
| 61 |
-
def
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
# ===============================================================
|
| 80 |
# DeepSeek-OCR (intacto) con fallback si no hay FlashAttention2
|
|
@@ -152,57 +212,15 @@ def process_image(image, model_size, task_type, is_eval_mode):
|
|
| 152 |
text_result = plain_text_result if plain_text_result else markdown_content
|
| 153 |
return result_image, markdown_content, text_result
|
| 154 |
|
| 155 |
-
# ===============================================================
|
| 156 |
-
# Chat (inyecta OCR) — con R1 local
|
| 157 |
-
# ===============================================================
|
| 158 |
-
def _truncate(text, max_chars=3000): return (text or "")[:max_chars]
|
| 159 |
-
|
| 160 |
-
def _system_prompt():
|
| 161 |
-
return ("Eres un asistente clínico educativo. No sustituyes el juicio médico. "
|
| 162 |
-
"Usa CONTEXTO_OCR si existe; si falta, pídelo. Evita diagnósticos definitivos.")
|
| 163 |
-
|
| 164 |
-
def _ocr_context(ocr_md, ocr_txt): return _truncate(ocr_md) or _truncate(ocr_txt) or ""
|
| 165 |
-
|
| 166 |
-
def to_chat_messages(chat_msgs, ocr_md, ocr_txt):
|
| 167 |
-
sys = _system_prompt()
|
| 168 |
-
ctx = _ocr_context(ocr_md, ocr_txt)
|
| 169 |
-
if ctx:
|
| 170 |
-
sys += ("\n\n---\n"
|
| 171 |
-
"CONTEXTO_OCR (fuente principal; si falta un dato, dilo explícitamente):\n"
|
| 172 |
-
f"{ctx}\n---")
|
| 173 |
-
msgs = [{"role": "system", "content": sys}]
|
| 174 |
-
for m in (chat_msgs or []):
|
| 175 |
-
if m.get("role") in ("user", "assistant"):
|
| 176 |
-
msgs.append({"role": m["role"], "content": m.get("content", "")})
|
| 177 |
-
return msgs
|
| 178 |
-
|
| 179 |
-
def r1_reply(user_msg, chat_msgs, ocr_md, ocr_txt):
|
| 180 |
-
if not user_msg:
|
| 181 |
-
user_msg = "Analiza el CONTEXTO_OCR anterior y responde a partir de ese contenido."
|
| 182 |
-
try:
|
| 183 |
-
msgs = to_chat_messages(chat_msgs, ocr_md, ocr_txt) + [{"role": "user", "content": user_msg}]
|
| 184 |
-
answer = r1_chat_local(msgs, temperature=0.2, max_tokens=512)
|
| 185 |
-
updated = (chat_msgs or []) + [{"role": "user", "content": user_msg},
|
| 186 |
-
{"role": "assistant", "content": answer}]
|
| 187 |
-
return updated, "", gr.update(value="")
|
| 188 |
-
except Exception as e:
|
| 189 |
-
err = f"{e.__class__.__name__}: {str(e) or repr(e)}"
|
| 190 |
-
tb = traceback.format_exc(limit=2)
|
| 191 |
-
updated = (chat_msgs or []) + [{"role": "user", "content": user_msg or ""},
|
| 192 |
-
{"role": "assistant", "content": f"⚠️ Error LLM: {err}"}]
|
| 193 |
-
return updated, "", gr.update(value=f"{err}\n{tb}")
|
| 194 |
-
|
| 195 |
-
def clear_chat(): return [], "", gr.update(value="")
|
| 196 |
-
|
| 197 |
# ===============================================================
|
| 198 |
# UI (Gradio 5)
|
| 199 |
# ===============================================================
|
| 200 |
-
with gr.Blocks(title="DeepSeek-OCR +
|
| 201 |
gr.Markdown(
|
| 202 |
"""
|
| 203 |
-
# DeepSeek-OCR → Chat Médico con **
|
| 204 |
1) **Sube una imagen** y corre **OCR** (imagen anotada, Markdown y texto).
|
| 205 |
-
2) **Chatea** con **
|
| 206 |
*Uso educativo; no reemplaza consejo médico.*
|
| 207 |
"""
|
| 208 |
)
|
|
@@ -231,10 +249,10 @@ with gr.Blocks(title="DeepSeek-OCR + R1 Medical (GGUF rápido)", theme=gr.themes
|
|
| 231 |
md_preview = gr.Textbox(label="Snapshot Markdown OCR", lines=10, interactive=False)
|
| 232 |
txt_preview = gr.Textbox(label="Snapshot Texto OCR", lines=10, interactive=False)
|
| 233 |
|
| 234 |
-
gr.Markdown("## Chat Clínico (
|
| 235 |
with gr.Row():
|
| 236 |
with gr.Column(scale=2):
|
| 237 |
-
chatbot = gr.Chatbot(label="Asistente OCR (
|
| 238 |
user_in = gr.Textbox(label="Mensaje", placeholder="Escribe tu consulta… (vacío = analiza solo el OCR)", lines=2)
|
| 239 |
with gr.Row():
|
| 240 |
send_btn = gr.Button("Enviar", variant="primary")
|
|
@@ -252,7 +270,7 @@ with gr.Blocks(title="DeepSeek-OCR + R1 Medical (GGUF rápido)", theme=gr.themes
|
|
| 252 |
outputs=[ocr_md_state, ocr_txt_state, md_preview, txt_preview],
|
| 253 |
)
|
| 254 |
|
| 255 |
-
send_btn.click(fn=
|
| 256 |
outputs=[chatbot, user_in, error_box])
|
| 257 |
clear_btn.click(fn=clear_chat, outputs=[chatbot, user_in, error_box])
|
| 258 |
|
|
|
|
| 1 |
+
# app.py — DeepSeek-OCR + BioMedLM (remoto o local) — Gradio 5
|
| 2 |
import os, tempfile, traceback
|
| 3 |
import gradio as gr
|
| 4 |
import torch
|
| 5 |
from PIL import Image
|
| 6 |
+
from transformers import AutoModel, AutoTokenizer, AutoModelForCausalLM
|
| 7 |
import spaces
|
| 8 |
+
from huggingface_hub import hf_hub_download, InferenceClient
|
|
|
|
| 9 |
|
| 10 |
# ===============================================================
|
| 11 |
+
# CHAT: BioMedLM — Remoto (HF Inference) o Local (Transformers)
|
| 12 |
+
# - Modo remoto: BIO_REMOTE=1 (recomendado en Spaces Zero/CPU)
|
| 13 |
+
# - Modo local: BIO_REMOTE=0 (usa PyTorch; 13B, CPU puede ser lento)
|
| 14 |
+
# - Variables: BIO_MODEL_ID=stanford-crfm/BioMedLM, HF_TOKEN
|
| 15 |
# ===============================================================
|
| 16 |
+
BIO_REMOTE = os.getenv("BIO_REMOTE", "0") == "1"
|
| 17 |
+
BIO_MODEL_ID = os.getenv("BIO_MODEL_ID", "stanford-crfm/BioMedLM").strip()
|
| 18 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 19 |
+
|
| 20 |
+
# Parámetros de generación por defecto
|
| 21 |
+
GEN_TEMPERATURE = float(os.getenv("GEN_TEMPERATURE", "0.2"))
|
| 22 |
+
GEN_TOP_P = float(os.getenv("GEN_TOP_P", "0.9"))
|
| 23 |
+
GEN_MAX_NEW_TOKENS = int(os.getenv("GEN_MAX_NEW_TOKENS", "512"))
|
| 24 |
+
GEN_REP_PENALTY = float(os.getenv("GEN_REP_PENALTY", "1.1"))
|
| 25 |
+
|
| 26 |
+
_bio_model = None
|
| 27 |
+
_bio_tokenizer = None
|
| 28 |
+
_hf_client = None
|
| 29 |
+
|
| 30 |
+
def get_biomedlm():
|
| 31 |
+
"""Obtiene el manejador del modelo BioMedLM según modo remoto/local."""
|
| 32 |
+
global _bio_model, _bio_tokenizer, _hf_client
|
| 33 |
+
if BIO_REMOTE:
|
| 34 |
+
if _hf_client is None:
|
| 35 |
+
_hf_client = InferenceClient(model=BIO_MODEL_ID, token=HF_TOKEN)
|
| 36 |
+
return ("remote", _hf_client)
|
| 37 |
+
else:
|
| 38 |
+
if _bio_model is None:
|
| 39 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 40 |
+
dtype = torch.bfloat16 if (device == "cuda" and torch.cuda.is_bf16_supported()) else (
|
| 41 |
+
torch.float16 if device == "cuda" else torch.float32
|
| 42 |
+
)
|
| 43 |
+
_bio_tokenizer = AutoTokenizer.from_pretrained(BIO_MODEL_ID, use_fast=True)
|
| 44 |
+
_bio_model = AutoModelForCausalLM.from_pretrained(
|
| 45 |
+
BIO_MODEL_ID,
|
| 46 |
+
torch_dtype=dtype,
|
| 47 |
+
)
|
| 48 |
+
_bio_model = _bio_model.to(device)
|
| 49 |
+
return ("local", (_bio_model, _bio_tokenizer))
|
| 50 |
+
|
| 51 |
+
def _system_prompt():
|
| 52 |
+
return ("Eres un asistente clínico educativo. No sustituyes el juicio médico. "
|
| 53 |
+
"Usa CONTEXTO_OCR si existe; si falta, pídelo. Evita diagnósticos definitivos.")
|
| 54 |
+
|
| 55 |
+
def _truncate(text, max_chars=3000):
|
| 56 |
+
return (text or "")[:max_chars]
|
| 57 |
+
|
| 58 |
+
def _ocr_context(ocr_md, ocr_txt):
|
| 59 |
+
return _truncate(ocr_md) or _truncate(ocr_txt) or ""
|
| 60 |
+
|
| 61 |
+
def build_prompt(chat_msgs, ocr_md, ocr_txt, user_msg):
|
| 62 |
+
"""Crea un prompt estilo 'instruct' apto para BioMedLM (no es modelo chat)."""
|
| 63 |
+
sys = _system_prompt()
|
| 64 |
+
ctx = _ocr_context(ocr_md, ocr_txt)
|
| 65 |
+
|
| 66 |
+
history_lines = []
|
| 67 |
+
for m in (chat_msgs or []):
|
| 68 |
+
role = m.get("role")
|
| 69 |
+
content = (m.get("content") or "").strip()
|
| 70 |
+
if not content:
|
| 71 |
+
continue
|
| 72 |
+
if role == "user":
|
| 73 |
+
history_lines.append(f"User: {content}")
|
| 74 |
+
elif role == "assistant":
|
| 75 |
+
history_lines.append(f"Assistant: {content}")
|
| 76 |
+
|
| 77 |
+
if user_msg:
|
| 78 |
+
history_lines.append(f"User: {user_msg}")
|
| 79 |
+
|
| 80 |
+
convo = "\n".join(history_lines).strip()
|
| 81 |
+
prompt = f"### System\n{sys}\n\n"
|
| 82 |
+
if ctx:
|
| 83 |
+
prompt += f"### Context (OCR)\n{ctx}\n\n"
|
| 84 |
+
prompt += f"### Conversation\n{convo}\nAssistant:"
|
| 85 |
+
return prompt
|
| 86 |
+
|
| 87 |
+
def biomedlm_reply(user_msg, chat_msgs, ocr_md, ocr_txt):
|
| 88 |
+
"""Genera respuesta con BioMedLM (remoto o local)."""
|
| 89 |
+
try:
|
| 90 |
+
if not user_msg:
|
| 91 |
+
user_msg = "Analiza el CONTEXTO_OCR anterior y responde a partir de ese contenido."
|
| 92 |
+
prompt = build_prompt(chat_msgs, ocr_md, ocr_txt, user_msg)
|
| 93 |
+
mode, handle = get_biomedlm()
|
| 94 |
+
|
| 95 |
+
if mode == "remote":
|
| 96 |
+
# HF Inference (text-generation)
|
| 97 |
+
out = handle.text_generation(
|
| 98 |
+
prompt,
|
| 99 |
+
max_new_tokens=GEN_MAX_NEW_TOKENS,
|
| 100 |
+
temperature=GEN_TEMPERATURE,
|
| 101 |
+
top_p=GEN_TOP_P,
|
| 102 |
+
repetition_penalty=GEN_REP_PENALTY,
|
| 103 |
+
# Paradas suaves; evita que el modelo “rompa” secciones
|
| 104 |
+
stop_sequences=["\nUser:", "### System", "### Context", "### Conversation"]
|
| 105 |
+
)
|
| 106 |
+
answer = out
|
| 107 |
+
else:
|
| 108 |
+
# Local (PyTorch)
|
| 109 |
+
model, tok = handle
|
| 110 |
+
inputs = tok(prompt, return_tensors="pt").to(model.device)
|
| 111 |
+
gen_ids = model.generate(
|
| 112 |
+
**inputs,
|
| 113 |
+
do_sample=True,
|
| 114 |
+
temperature=GEN_TEMPERATURE,
|
| 115 |
+
top_p=GEN_TOP_P,
|
| 116 |
+
repetition_penalty=GEN_REP_PENALTY,
|
| 117 |
+
max_new_tokens=GEN_MAX_NEW_TOKENS,
|
| 118 |
+
eos_token_id=tok.eos_token_id,
|
| 119 |
+
)
|
| 120 |
+
answer = tok.decode(gen_ids[0][inputs["input_ids"].shape[1]:], skip_special_tokens=True)
|
| 121 |
+
|
| 122 |
+
updated = (chat_msgs or []) + [
|
| 123 |
+
{"role": "user", "content": user_msg},
|
| 124 |
+
{"role": "assistant", "content": answer.strip()}
|
| 125 |
+
]
|
| 126 |
+
return updated, "", gr.update(value="")
|
| 127 |
+
except Exception as e:
|
| 128 |
+
err = f"{e.__class__.__name__}: {str(e) or repr(e)}"
|
| 129 |
+
tb = traceback.format_exc(limit=2)
|
| 130 |
+
updated = (chat_msgs or []) + [
|
| 131 |
+
{"role": "user", "content": user_msg or ""},
|
| 132 |
+
{"role": "assistant", "content": f"⚠️ Error LLM: {err}"}
|
| 133 |
+
]
|
| 134 |
+
return updated, "", gr.update(value=f"{err}\n{tb}")
|
| 135 |
+
|
| 136 |
+
def clear_chat():
|
| 137 |
+
return [], "", gr.update(value="")
|
| 138 |
|
| 139 |
# ===============================================================
|
| 140 |
# DeepSeek-OCR (intacto) con fallback si no hay FlashAttention2
|
|
|
|
| 212 |
text_result = plain_text_result if plain_text_result else markdown_content
|
| 213 |
return result_image, markdown_content, text_result
|
| 214 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 215 |
# ===============================================================
|
| 216 |
# UI (Gradio 5)
|
| 217 |
# ===============================================================
|
| 218 |
+
with gr.Blocks(title="DeepSeek-OCR + BioMedLM", theme=gr.themes.Soft()) as demo:
|
| 219 |
gr.Markdown(
|
| 220 |
"""
|
| 221 |
+
# DeepSeek-OCR → Chat Médico con **BioMedLM**
|
| 222 |
1) **Sube una imagen** y corre **OCR** (imagen anotada, Markdown y texto).
|
| 223 |
+
2) **Chatea** con **BioMedLM** usando automáticamente el **OCR** como contexto.
|
| 224 |
*Uso educativo; no reemplaza consejo médico.*
|
| 225 |
"""
|
| 226 |
)
|
|
|
|
| 249 |
md_preview = gr.Textbox(label="Snapshot Markdown OCR", lines=10, interactive=False)
|
| 250 |
txt_preview = gr.Textbox(label="Snapshot Texto OCR", lines=10, interactive=False)
|
| 251 |
|
| 252 |
+
gr.Markdown("## Chat Clínico (BioMedLM)")
|
| 253 |
with gr.Row():
|
| 254 |
with gr.Column(scale=2):
|
| 255 |
+
chatbot = gr.Chatbot(label="Asistente OCR (BioMedLM)", type="messages", height=420)
|
| 256 |
user_in = gr.Textbox(label="Mensaje", placeholder="Escribe tu consulta… (vacío = analiza solo el OCR)", lines=2)
|
| 257 |
with gr.Row():
|
| 258 |
send_btn = gr.Button("Enviar", variant="primary")
|
|
|
|
| 270 |
outputs=[ocr_md_state, ocr_txt_state, md_preview, txt_preview],
|
| 271 |
)
|
| 272 |
|
| 273 |
+
send_btn.click(fn=biomedlm_reply, inputs=[user_in, chatbot, ocr_md_state, ocr_txt_state],
|
| 274 |
outputs=[chatbot, user_in, error_box])
|
| 275 |
clear_btn.click(fn=clear_chat, outputs=[chatbot, user_in, error_box])
|
| 276 |
|