AhmadMughal commited on
Commit
cf81698
·
verified ·
1 Parent(s): 9992cbf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -44
app.py CHANGED
@@ -3,7 +3,7 @@ from transformers import AutoModelForImageClassification, AutoFeatureExtractor
3
  from PIL import Image
4
  import torch
5
 
6
- # Load the Hugging Face model and feature extractor
7
  @st.cache_resource
8
  def load_model():
9
  model_name = "syaha/skin_cancer_detection_model"
@@ -13,54 +13,55 @@ def load_model():
13
 
14
  model, feature_extractor = load_model()
15
 
16
- # App title
17
- st.title("Skin Cancer Detection Application")
18
  st.write("""
19
- Upload an image of the affected skin area, and this app will identify and classify it based on pre-trained skin lesion types.
20
  """)
21
 
22
- # File uploader
23
  uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
24
 
25
  if uploaded_file:
26
- # Display the uploaded image
27
- image = Image.open(uploaded_file)
28
- st.image(image, caption="Uploaded Image", use_column_width=True)
29
-
30
- # Preprocess the image
31
- inputs = feature_extractor(images=image, return_tensors="pt")
32
-
33
- # Perform inference
34
- with torch.no_grad():
35
- outputs = model(**inputs)
36
- probabilities = torch.nn.functional.softmax(outputs.logits, dim=-1)
37
- predicted_class = probabilities.argmax().item()
38
- confidence = probabilities.max().item()
 
39
 
40
- # Map labels (update based on the model's labels)
41
- labels = {
42
- 0: "Melanocytic nevi",
43
- 1: "Melanoma",
44
- 2: "Benign keratosis-like lesions",
45
- 3: "Basal cell carcinoma",
46
- 4: "Actinic keratoses",
47
- 5: "Vascular lesions",
48
- 6: "Dermatofibroma"
49
- }
50
- diagnosis = labels.get(predicted_class, "Unknown")
51
 
52
- # Display results
53
- st.write("### Results")
54
- st.write(f"**Predicted Class**: {diagnosis}")
55
- st.write(f"**Confidence**: {confidence:.2f}")
56
-
57
- # Generate report
58
- st.write("### Report")
59
- st.write(f"**Diagnosis**: {diagnosis}")
60
- st.write(f"**Confidence Level**: {confidence:.2%}")
61
- st.download_button(
62
- label="Download Report",
63
- data=f"Diagnosis: {diagnosis}\nConfidence Level: {confidence:.2%}",
64
- file_name="report.txt",
65
- mime="text/plain",
66
- )
 
3
  from PIL import Image
4
  import torch
5
 
6
+ # Cache the model and feature extractor
7
  @st.cache_resource
8
  def load_model():
9
  model_name = "syaha/skin_cancer_detection_model"
 
13
 
14
  model, feature_extractor = load_model()
15
 
16
+ # App title and instructions
17
+ st.title("Skin Cancer Detection App")
18
  st.write("""
19
+ Upload an image of the affected skin area, and this app will classify it based on pre-trained skin lesion types.
20
  """)
21
 
22
+ # File uploader for user input
23
  uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
24
 
25
  if uploaded_file:
26
+ try:
27
+ # Display the uploaded image
28
+ image = Image.open(uploaded_file)
29
+ st.image(image, caption="Uploaded Image", use_column_width=True)
30
+
31
+ # Preprocess the image
32
+ inputs = feature_extractor(images=image, return_tensors="pt")
33
+
34
+ # Perform inference
35
+ with torch.no_grad():
36
+ outputs = model(**inputs)
37
+ probabilities = torch.nn.functional.softmax(outputs.logits, dim=-1)
38
+ predicted_class = probabilities.argmax().item()
39
+ confidence = probabilities.max().item()
40
 
41
+ # Map predicted class to human-readable labels
42
+ labels = {
43
+ 0: "Melanocytic nevi",
44
+ 1: "Melanoma",
45
+ 2: "Benign keratosis-like lesions",
46
+ 3: "Basal cell carcinoma",
47
+ 4: "Actinic keratoses",
48
+ 5: "Vascular lesions",
49
+ 6: "Dermatofibroma"
50
+ }
51
+ diagnosis = labels.get(predicted_class, "Unknown")
52
 
53
+ # Display results
54
+ st.subheader("Results")
55
+ st.write(f"**Diagnosis:** {diagnosis}")
56
+ st.write(f"**Confidence Level:** {confidence:.2%}")
57
+
58
+ # Provide an option to download the report
59
+ report = f"Diagnosis: {diagnosis}\nConfidence Level: {confidence:.2%}"
60
+ st.download_button(
61
+ label="Download Report",
62
+ data=report,
63
+ file_name="report.txt",
64
+ mime="text/plain",
65
+ )
66
+ except Exception as e:
67
+ st.error(f"An error occurred while processing the image: {e}")