Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,18 +1,18 @@
|
|
1 |
import os
|
2 |
from huggingface_hub import login
|
3 |
|
4 |
-
# 🔐
|
5 |
hf_token = os.environ.get("HUGGINGFACE_API_KEY")
|
6 |
-
|
7 |
-
login(hf_token)
|
8 |
|
9 |
from transformers import AutoModelForCausalLM, AutoProcessor
|
10 |
import torch
|
11 |
import gradio as gr
|
|
|
12 |
|
13 |
model_id = "ContactDoctor/Bio-Medical-MultiModal-Llama-3-8B-V1"
|
14 |
|
15 |
-
# ✅ Chargement du modèle
|
16 |
model = AutoModelForCausalLM.from_pretrained(
|
17 |
model_id,
|
18 |
trust_remote_code=True,
|
@@ -20,6 +20,7 @@ model = AutoModelForCausalLM.from_pretrained(
|
|
20 |
device_map="auto"
|
21 |
)
|
22 |
|
|
|
23 |
processor = AutoProcessor.from_pretrained(
|
24 |
model_id,
|
25 |
trust_remote_code=True
|
@@ -27,34 +28,42 @@ processor = AutoProcessor.from_pretrained(
|
|
27 |
|
28 |
# 🧠 Fonction principale
|
29 |
def generate_answer(image, question):
|
30 |
-
|
|
|
|
|
|
|
31 |
question = "Please describe this medical image."
|
32 |
-
|
33 |
prompt = f"### User: {question}\n### Assistant:"
|
34 |
-
|
35 |
try:
|
36 |
if image is None:
|
37 |
inputs = processor(prompt, return_tensors="pt").to(model.device)
|
38 |
else:
|
|
|
39 |
inputs = processor(prompt, images=image, return_tensors="pt").to(model.device)
|
40 |
|
|
|
41 |
outputs = model.generate(**inputs, max_new_tokens=256)
|
42 |
-
|
43 |
-
|
|
|
|
|
|
|
44 |
except Exception as e:
|
|
|
45 |
return f"⚠️ Internal Error: {str(e)}"
|
46 |
|
47 |
-
# 🎛️ Interface Gradio
|
48 |
demo = gr.Interface(
|
49 |
fn=generate_answer,
|
50 |
inputs=[
|
51 |
-
gr.Image(type="pil", label="Image médicale (optionnelle)"),
|
52 |
-
gr.Textbox(label="
|
53 |
],
|
54 |
outputs="text",
|
55 |
title="🧠 ContactDoctor - Biomedical LLM",
|
56 |
-
description="Assistant médical multimodal. Posez une question ou
|
57 |
)
|
58 |
|
59 |
-
# 🚀 Lancement
|
60 |
demo.launch()
|
|
|
1 |
import os
|
2 |
from huggingface_hub import login
|
3 |
|
4 |
+
# 🔐 Authentification Hugging Face
|
5 |
hf_token = os.environ.get("HUGGINGFACE_API_KEY")
|
6 |
+
login(hf_token)
|
|
|
7 |
|
8 |
from transformers import AutoModelForCausalLM, AutoProcessor
|
9 |
import torch
|
10 |
import gradio as gr
|
11 |
+
from PIL import Image
|
12 |
|
13 |
model_id = "ContactDoctor/Bio-Medical-MultiModal-Llama-3-8B-V1"
|
14 |
|
15 |
+
# ✅ Chargement du modèle
|
16 |
model = AutoModelForCausalLM.from_pretrained(
|
17 |
model_id,
|
18 |
trust_remote_code=True,
|
|
|
20 |
device_map="auto"
|
21 |
)
|
22 |
|
23 |
+
# ✅ Chargement du processor
|
24 |
processor = AutoProcessor.from_pretrained(
|
25 |
model_id,
|
26 |
trust_remote_code=True
|
|
|
28 |
|
29 |
# 🧠 Fonction principale
|
30 |
def generate_answer(image, question):
|
31 |
+
print("📥 Question reçue:", question)
|
32 |
+
print("🖼️ Image présente ?", image is not None)
|
33 |
+
|
34 |
+
if not question or question.strip() == "":
|
35 |
question = "Please describe this medical image."
|
36 |
+
|
37 |
prompt = f"### User: {question}\n### Assistant:"
|
|
|
38 |
try:
|
39 |
if image is None:
|
40 |
inputs = processor(prompt, return_tensors="pt").to(model.device)
|
41 |
else:
|
42 |
+
print("✅ Image fournie, traitement en cours...")
|
43 |
inputs = processor(prompt, images=image, return_tensors="pt").to(model.device)
|
44 |
|
45 |
+
print("🚀 Génération...")
|
46 |
outputs = model.generate(**inputs, max_new_tokens=256)
|
47 |
+
|
48 |
+
decoded = processor.tokenizer.decode(outputs[0], skip_special_tokens=True)
|
49 |
+
print("✅ Réponse générée.")
|
50 |
+
return decoded[len(prompt):].strip()
|
51 |
+
|
52 |
except Exception as e:
|
53 |
+
print("❌ Exception attrapée :", str(e))
|
54 |
return f"⚠️ Internal Error: {str(e)}"
|
55 |
|
56 |
+
# 🎛️ Interface Gradio
|
57 |
demo = gr.Interface(
|
58 |
fn=generate_answer,
|
59 |
inputs=[
|
60 |
+
gr.Image(type="pil", label="🩻 Image médicale (optionnelle)"),
|
61 |
+
gr.Textbox(label="❓ Votre question médicale")
|
62 |
],
|
63 |
outputs="text",
|
64 |
title="🧠 ContactDoctor - Biomedical LLM",
|
65 |
+
description="Assistant médical multimodal. Posez une question ou uploadez une image."
|
66 |
)
|
67 |
|
68 |
+
# 🚀 Lancement
|
69 |
demo.launch()
|