File size: 1,864 Bytes
e41d0cb 398bcaf e41d0cb 398bcaf e41d0cb 398bcaf e41d0cb 398bcaf e41d0cb 398bcaf e41d0cb 398bcaf e41d0cb 398bcaf e41d0cb 398bcaf e41d0cb 398bcaf e41d0cb 398bcaf e41d0cb 398bcaf e41d0cb 398bcaf e41d0cb 398bcaf e41d0cb 398bcaf |
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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
import gradio as gr
from llava_med import LlavaMedProcessor, LlavaMedForCausalLM
from PIL import Image
import torch
# Load model and processor
model = LlavaMedForCausalLM.from_pretrained(
"microsoft/llava-med-v1.5-mistral-7b",
torch_dtype=torch.float32, # Use float32 for CPU stability
low_cpu_mem_usage=True,
device_map="cpu"
)
processor = LlavaMedProcessor.from_pretrained(
"microsoft/llava-med-v1.5-mistral-7b"
)
def analyze_medical_image(image, question):
# Prepare inputs
prompt = f"Question: {question} Answer:"
# Process inputs
inputs = processor(
text=prompt,
images=image,
return_tensors="pt",
padding=True
).to("cpu")
# Generate response
with torch.no_grad():
outputs = model.generate(
**inputs,
max_new_tokens=256,
do_sample=True,
temperature=0.7,
top_p=0.9
)
# Decode response
response = processor.batch_decode(
outputs,
skip_special_tokens=True
)[0].split("Answer:")[-1].strip()
return response
# Gradio interface
with gr.Blocks() as demo:
gr.Markdown("# LLaVA-Med Medical Analysis (CPU)")
gr.Markdown("Official Microsoft LLaVA-Med 1.5-Mistral-7B implementation")
with gr.Row():
with gr.Column():
image_input = gr.Image(label="Medical Image", type="pil")
question_input = gr.Textbox(label="Clinical Question", placeholder="Enter your medical question...")
submit_btn = gr.Button("Analyze")
with gr.Column():
output_text = gr.Textbox(label="Clinical Analysis", interactive=False)
submit_btn.click(
fn=analyze_medical_image,
inputs=[image_input, question_input],
outputs=output_text
)
demo.queue(max_size=5).launch() |