File size: 3,316 Bytes
3ead2fc
 
 
 
 
 
0348ef8
 
3ead2fc
 
 
 
 
0348ef8
 
 
3ead2fc
 
 
0348ef8
 
3ead2fc
 
 
 
 
0348ef8
9c09240
3ead2fc
 
 
0348ef8
3ead2fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import gradio as gr
import torch
from transformers import AutoProcessor, Qwen2VLForConditionalGeneration
from PIL import Image
import json

import os

MODEL_ID = "Qwen/Qwen2-VL-2B-Instruct"
ADAPTER_ID = "hssling/derm-analyzer-adapter"

print("Starting App Engine...")
device = "cuda" if torch.cuda.is_available() else "cpu"
token = os.environ.get("HF_TOKEN")

processor = AutoProcessor.from_pretrained(MODEL_ID, token=token)
model = Qwen2VLForConditionalGeneration.from_pretrained(
    MODEL_ID,
    torch_dtype=torch.float16 if device == "cuda" else torch.float32,
    device_map="auto",
    token=token
)

if ADAPTER_ID:
    print(f"Loading custom fine-tuned LoRA weights: {ADAPTER_ID}")
    try:
        model.load_adapter(ADAPTER_ID, token=token)
        print("✅ Adapter loaded successfully over the base Qwen2-VL engine.")
    except Exception as e:
        print(f"Failed to load adapter. Using base model. Error: {e}")

def diagnose_skin(image, clinical_notes, temp, max_tokens):
    try:
        if image is None:
            return json.dumps({"error": "No image provided."})

        system_prompt = "You are DermaAI, an expert Dermatologist trained extensively on Indian skin types (Fitzpatrick IV-VI) and tropical diseases. Analyze the skin lesion and output a structured clinical report including Findings, Differential Diagnosis, and recommended Indian Pharmacological Management."
        user_prompt = f"Clinical Context: {clinical_notes}\nAnalyze this dermatological image and describe the medical findings, providing treatment and management advice."

        messages = [
            {"role": "system", "content": system_prompt},
            {
                "role": "user",
                "content": [
                    {"type": "image"},
                    {"type": "text", "text": user_prompt}
                ]
            }
        ]

        text_input = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
        
        inputs = processor(
            text=[text_input],
            images=[image],
            padding=True,
            return_tensors="pt"
        ).to(device)

        with torch.no_grad():
            generated_ids = model.generate(**inputs, max_new_tokens=int(max_tokens), temperature=float(temp), top_p=0.9, do_sample=True)

        generated_ids_trimmed = [
            out_ids[len(in_ids):] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
        ]
        
        output_text = processor.batch_decode(generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]

        return output_text

    except Exception as e:
        return f"Error: {str(e)}"

demo = gr.Interface(
    fn=diagnose_skin,
    inputs=[
        gr.Image(type="pil", label="Skin Image"),
        gr.Textbox(label="Clinical Context", value="No additional clinical context provided."),
        gr.Slider(minimum=0.0, maximum=1.0, value=0.4, step=0.1, label="Temperature"),
        gr.Slider(minimum=256, maximum=4096, value=2000, step=256, label="Max Tokens")
    ],
    outputs=gr.Markdown(label="Clinical Report Output"),
    title="DermaAI API (Indian Context)",
    description="Fine-tuned Medical LLM for Dermatology, focused on Fitzpatrick Skin Types IV-VI."
)

if __name__ == "__main__":
    demo.launch()