Spaces:
Sleeping
Sleeping
Upload app.py with huggingface_hub
Browse files
app.py
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import AutoProcessor, Qwen2VLForConditionalGeneration
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import json
|
| 6 |
+
|
| 7 |
+
MODEL_ID = "Qwen/Qwen2-VL-2B-Instruct"
|
| 8 |
+
ADAPTER_ID = "hssling/derm-analyzer-adapter"
|
| 9 |
+
|
| 10 |
+
print("Starting App Engine...")
|
| 11 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 12 |
+
processor = AutoProcessor.from_pretrained(MODEL_ID)
|
| 13 |
+
model = Qwen2VLForConditionalGeneration.from_pretrained(
|
| 14 |
+
MODEL_ID,
|
| 15 |
+
torch_dtype=torch.float16 if device == "cuda" else torch.float32,
|
| 16 |
+
device_map="auto"
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
if ADAPTER_ID:
|
| 20 |
+
print(f"Loading custom fine-tuned LoRA weights: {ADAPTER_ID}")
|
| 21 |
+
try:
|
| 22 |
+
model.load_adapter(ADAPTER_ID)
|
| 23 |
+
except Exception as e:
|
| 24 |
+
print(f"Failed to load adapter. Using base model. Error: {e}")
|
| 25 |
+
|
| 26 |
+
def diagnose_skin(image: Image.Image = None, clinical_notes: str = '', temp: float = 0.4, max_tokens: int = 2000):
|
| 27 |
+
try:
|
| 28 |
+
if image is None:
|
| 29 |
+
return json.dumps({"error": "No image provided."})
|
| 30 |
+
|
| 31 |
+
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."
|
| 32 |
+
user_prompt = f"Clinical Context: {clinical_notes}\nAnalyze this dermatological image and describe the medical findings, providing treatment and management advice."
|
| 33 |
+
|
| 34 |
+
messages = [
|
| 35 |
+
{"role": "system", "content": system_prompt},
|
| 36 |
+
{
|
| 37 |
+
"role": "user",
|
| 38 |
+
"content": [
|
| 39 |
+
{"type": "image"},
|
| 40 |
+
{"type": "text", "text": user_prompt}
|
| 41 |
+
]
|
| 42 |
+
}
|
| 43 |
+
]
|
| 44 |
+
|
| 45 |
+
text_input = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
| 46 |
+
|
| 47 |
+
inputs = processor(
|
| 48 |
+
text=[text_input],
|
| 49 |
+
images=[image],
|
| 50 |
+
padding=True,
|
| 51 |
+
return_tensors="pt"
|
| 52 |
+
).to(device)
|
| 53 |
+
|
| 54 |
+
with torch.no_grad():
|
| 55 |
+
generated_ids = model.generate(**inputs, max_new_tokens=int(max_tokens), temperature=float(temp), top_p=0.9, do_sample=True)
|
| 56 |
+
|
| 57 |
+
generated_ids_trimmed = [
|
| 58 |
+
out_ids[len(in_ids):] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
|
| 59 |
+
]
|
| 60 |
+
|
| 61 |
+
output_text = processor.batch_decode(generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]
|
| 62 |
+
|
| 63 |
+
return output_text
|
| 64 |
+
|
| 65 |
+
except Exception as e:
|
| 66 |
+
return f"Error: {str(e)}"
|
| 67 |
+
|
| 68 |
+
demo = gr.Interface(
|
| 69 |
+
fn=diagnose_skin,
|
| 70 |
+
inputs=[
|
| 71 |
+
gr.Image(type="pil", label="Skin Image"),
|
| 72 |
+
gr.Textbox(label="Clinical Context", value="No additional clinical context provided."),
|
| 73 |
+
gr.Slider(minimum=0.0, maximum=1.0, value=0.4, step=0.1, label="Temperature"),
|
| 74 |
+
gr.Slider(minimum=256, maximum=4096, value=2000, step=256, label="Max Tokens")
|
| 75 |
+
],
|
| 76 |
+
outputs=gr.Markdown(label="Clinical Report Output"),
|
| 77 |
+
title="DermaAI API (Indian Context)",
|
| 78 |
+
description="Fine-tuned Medical LLM for Dermatology, focused on Fitzpatrick Skin Types IV-VI."
|
| 79 |
+
)
|
| 80 |
+
|
| 81 |
+
if __name__ == "__main__":
|
| 82 |
+
demo.launch()
|