File size: 1,548 Bytes
f279b7c
6cd9f8a
f279b7c
 
 
 
6cd9f8a
fcbb3f1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6cd9f8a
1
2
3
4
5
6
7
8
9
10
11
12
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
import os
from huggingface_hub import login

# ✅ Login sécurisé avec le secret
hf_token = os.environ.get("HUGGINGFACE_API_KEY")
login(hf_token)

from transformers import AutoModelForCausalLM, AutoProcessor
import torch
import gradio as gr
from PIL import Image

model_id = "ContactDoctor/Bio-Medical-MultiModal-Llama-3-8B-V1"

model = AutoModelForCausalLM.from_pretrained(
    model_id,
    trust_remote_code=True,
    torch_dtype=torch.float16,
    device_map="auto"
)

processor = AutoProcessor.from_pretrained(model_id)

def generate_answer(image, question):
    if not question or question.strip() == "":
        return "❌ Please enter a medical question."

    prompt = f"### User: {question}\n### Assistant:"
    
    try:
        if image is None:
            inputs = processor(prompt, return_tensors="pt").to(model.device)
        else:
            inputs = processor(prompt, images=image, return_tensors="pt").to(model.device)

        outputs = model.generate(**inputs, max_new_tokens=256)
        return processor.tokenizer.decode(outputs[0], skip_special_tokens=True)[len(prompt):].strip()
    except Exception as e:
        return f"⚠️ Internal Error: {str(e)}"

demo = gr.Interface(
    fn=generate_answer,
    inputs=[
        gr.Image(type="pil", label="Upload a medical image (optional)"),
        gr.Textbox(label="Medical Question")
    ],
    outputs="text",
    title="🧠 ContactDoctor - Biomedical LLM",
    description="Multimodal Medical Assistant: upload an image and ask a medical question."
)

demo.launch()