Adnanilyas commited on
Commit
108bbe2
·
verified ·
1 Parent(s): 2fc40f0

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +83 -0
app.py ADDED
@@ -0,0 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from transformers import AutoModelForImageClassification, AutoImageProcessor
3
+ from PIL import Image
4
+ import torch
5
+
6
+ # Set the title of the application
7
+ st.title("Dermavision")
8
+ st.write(
9
+ "Upload an image of the affected skin area, and the app will classify the disease and provide analysis."
10
+ )
11
+
12
+ # Cache model and processor loading
13
+ @st.cache_resource
14
+ def load_model():
15
+ repo_name = "Jayanth2002/dinov2-base-finetuned-SkinDisease"
16
+ processor = AutoImageProcessor.from_pretrained(repo_name)
17
+ model = AutoModelForImageClassification.from_pretrained(repo_name)
18
+ return model, processor
19
+
20
+ model, processor = load_model()
21
+
22
+ # Define the class names
23
+ class_names = [
24
+ 'Basal Cell Carcinoma', 'Darier_s Disease', 'Epidermolysis Bullosa Pruriginosa',
25
+ 'Hailey-Hailey Disease', 'Herpes Simplex', 'Impetigo', 'Larva Migrans',
26
+ 'Leprosy Borderline', 'Leprosy Lepromatous', 'Leprosy Tuberculoid', 'Lichen Planus',
27
+ 'Lupus Erythematosus Chronicus Discoides', 'Melanoma', 'Molluscum Contagiosum',
28
+ 'Mycosis Fungoides', 'Neurofibromatosis', 'Papilomatosis Confluentes And Reticulate',
29
+ 'Pediculosis Capitis', 'Pityriasis Rosea', 'Porokeratosis Actinic', 'Psoriasis',
30
+ 'Tinea Corporis', 'Tinea Nigra', 'Tungiasis', 'actinic keratosis', 'dermatofibroma',
31
+ 'nevus', 'pigmented benign keratosis', 'seborrheic keratosis', 'squamous cell carcinoma',
32
+ 'vascular lesion'
33
+ ]
34
+
35
+ # Define reasons, treatments, and home remedies for each disease
36
+ # (This section is omitted for brevity but should remain unchanged from your original code)
37
+
38
+ # Function to classify the image
39
+ def classify_image(image):
40
+ inputs = processor(image.convert("RGB"), return_tensors="pt")
41
+ with torch.no_grad():
42
+ outputs = model(**inputs)
43
+ predicted_class_idx = outputs.logits.argmax(-1).item()
44
+ confidence_score = torch.nn.functional.softmax(outputs.logits, dim=-1).max().item()
45
+ predicted_label = class_names[predicted_class_idx]
46
+ return predicted_label, confidence_score
47
+
48
+ # File uploader for user image
49
+ uploaded_file = st.file_uploader("Upload a skin image", type=["jpg", "jpeg", "png"])
50
+
51
+ if uploaded_file is not None:
52
+ # Display the uploaded image
53
+ image = Image.open(uploaded_file)
54
+ st.image(image, caption="Uploaded Image", use_column_width=True)
55
+
56
+ # Analyze the image
57
+ with st.spinner("Analyzing the image..."):
58
+ predicted_label, confidence_score = classify_image(image)
59
+ if predicted_label not in disease_analysis:
60
+ st.error("Unable to classify the disease. Please upload a clearer image or consult a dermatologist.")
61
+ else:
62
+ reason = disease_analysis.get(predicted_label, {}).get("reason", "Reason unknown.")
63
+ treatment = disease_analysis.get(predicted_label, {}).get("treatment", "Consult a dermatologist.")
64
+ home_remedy = disease_analysis.get(predicted_label, {}).get("home_remedy", "No specific home remedies available.")
65
+
66
+ # Display the results
67
+ st.success("Analysis Complete!")
68
+ st.markdown(f"### **Classification**: {predicted_label}")
69
+ st.markdown(f"**Confidence**: {confidence_score:.2%}")
70
+ st.markdown(f"**Reason**: {reason}")
71
+ st.markdown(f"**Treatment**: {treatment}")
72
+ st.markdown(f"**Home Remedy**: {home_remedy}")
73
+ st.markdown("**Note:** Please consult a doctor for final recommendations and a detailed treatment plan.")
74
+
75
+ # Optional feedback form
76
+ st.markdown("---")
77
+ st.header("We Value Your Feedback!")
78
+ feedback = st.text_area("Please share your feedback to help us improve:")
79
+ if st.button("Submit Feedback"):
80
+ if feedback:
81
+ st.success("Thank you for your feedback!")
82
+ else:
83
+ st.warning("Feedback cannot be empty.")