File size: 1,600 Bytes
b43d847
 
 
e59ec29
0b2a88c
b43d847
 
 
 
 
 
e59ec29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b43d847
 
e59ec29
 
 
 
b43d847
 
e59ec29
8e90fc6
5be90eb
b43d847
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
import gradio as gr
from transformers import AutoModel, AutoTokenizer
import torch
from PIL import Image

# Load the model and tokenizer
model_name = "ContactDoctor/Bio-Medical-MultiModal-Llama-3-8B-V1"
model = AutoModel.from_pretrained(model_name, trust_remote_code=True, device_map="auto", torch_dtype=torch.float16)
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)

def process_query(image, question):
    try:
        # Construct the messages for the model
        msgs = [{"role": "user", "content": question}]
        
        # Handle cases with and without an image
        if image is not None:
            # Convert the image to the required format
            image_input = [Image.fromarray(image).convert("RGB")]
            response = model.chat(
                image=image_input,
                msgs=msgs,
                tokenizer=tokenizer,
            )
        else:
            # For text-only queries, omit the `image` parameter
            response = model.chat(
                image=None,
                msgs=msgs,
                tokenizer=tokenizer,
            )
        
        return response
    except Exception as e:
        return f"Error: {str(e)}"

# Gradio interface
iface = gr.Interface(
    fn=process_query,
    inputs=[
        gr.Image(type="numpy", label="Upload Medical Image (Optional)"),
        gr.Textbox(label="Enter Your Medical Question"),
    ],
    outputs="text",
    title="Medical Multimodal Assistant",
    description="Upload a medical image and/or ask a question for AI-powered assistance.",
)

iface.launch()