mgbam commited on
Commit
bfa4db2
·
verified ·
1 Parent(s): 5b2305a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -37
app.py CHANGED
@@ -1,22 +1,6 @@
1
  import streamlit as st
2
  from transformers import pipeline, AutoModelForImageClassification, AutoFeatureExtractor
3
  from PIL import Image
4
- import openai
5
- import os
6
- from dotenv import load_dotenv
7
-
8
- # =======================
9
- # Load Environment Variables from .env File
10
- # =======================
11
- load_dotenv() # Explicitly load the .env file
12
-
13
- # Set OpenAI API key
14
- openai.api_key = os.getenv("OPENAI_API_KEY")
15
-
16
- # Debugging: Check if API key is loaded
17
- if not openai.api_key or not openai.api_key.startswith("sk-"):
18
- st.error("OpenAI API key is not set or is invalid. Please check the `.env` file or your environment variable setup.")
19
- st.stop()
20
 
21
  # =======================
22
  # Streamlit Page Config
@@ -35,7 +19,6 @@ st.set_page_config(
35
  def load_model():
36
  """
37
  Load the pre-trained skin cancer classification model using PyTorch.
38
- Use the AutoModelForImageClassification and AutoFeatureExtractor for explicit local caching.
39
  """
40
  try:
41
  extractor = AutoFeatureExtractor.from_pretrained("Anwarkh1/Skin_Cancer-Image_Classification")
@@ -48,28 +31,33 @@ def load_model():
48
  model = load_model()
49
 
50
  # =======================
51
- # Generate OpenAI Explanation
52
  # =======================
53
- def generate_openai_explanation(label, confidence):
54
  """
55
- Generate a detailed explanation for the classification result using OpenAI's GPT model.
56
  """
57
- prompt = (
58
- f"The AI model has classified an image of a skin lesion as **{label}** with a confidence of **{confidence:.2%}**.\n"
59
- f"Explain what this classification means, including potential characteristics of this lesion type, "
60
- f"what steps a patient should take next, and how the AI might have arrived at this conclusion. "
61
- f"Use language that is easy for a non-medical audience to understand."
62
- )
63
- try:
64
- response = openai.Completion.create(
65
- model="text-davinci-003", # Replace with "gpt-4" if available
66
- prompt=prompt,
67
- max_tokens=300,
68
- temperature=0.7
 
 
 
69
  )
70
- return response.choices[0].text.strip()
71
- except Exception as e:
72
- return f"Error generating explanation: {e}"
 
 
73
 
74
  # =======================
75
  # Streamlit App Title and Sidebar
@@ -116,8 +104,7 @@ if uploaded_image:
116
  st.error("Low confidence in the prediction. Results should be interpreted with caution.")
117
 
118
  # Generate explanation
119
- with st.spinner("Generating a detailed explanation..."):
120
- explanation = generate_openai_explanation(label, confidence)
121
 
122
  st.markdown("### Explanation")
123
  st.write(explanation)
 
1
  import streamlit as st
2
  from transformers import pipeline, AutoModelForImageClassification, AutoFeatureExtractor
3
  from PIL import Image
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
 
5
  # =======================
6
  # Streamlit Page Config
 
19
  def load_model():
20
  """
21
  Load the pre-trained skin cancer classification model using PyTorch.
 
22
  """
23
  try:
24
  extractor = AutoFeatureExtractor.from_pretrained("Anwarkh1/Skin_Cancer-Image_Classification")
 
31
  model = load_model()
32
 
33
  # =======================
34
+ # Local Explanation Generator
35
  # =======================
36
+ def generate_local_explanation(label, confidence):
37
  """
38
+ Generate a simple explanation for the classification result.
39
  """
40
+ explanations = {
41
+ "Melanoma": (
42
+ "Melanoma is a serious type of skin cancer that develops in the cells that produce melanin. "
43
+ "If detected early, it is often treatable. You should consult a dermatologist immediately."
44
+ ),
45
+ "Basal Cell Carcinoma": (
46
+ "Basal Cell Carcinoma is a common form of skin cancer that grows slowly and is typically not life-threatening. "
47
+ "Still, it requires medical attention to prevent further complications."
48
+ ),
49
+ "Benign Lesion": (
50
+ "A benign lesion is a non-cancerous growth on the skin. While it is usually harmless, "
51
+ "consulting a dermatologist can help ensure no further treatment is needed."
52
+ ),
53
+ "Other": (
54
+ "The AI could not confidently classify the lesion. It's strongly recommended to consult a dermatologist for further evaluation."
55
  )
56
+ }
57
+
58
+ explanation = explanations.get(label, explanations["Other"])
59
+ confidence_msg = f"The model is {confidence:.2%} confident in this prediction. "
60
+ return confidence_msg + explanation
61
 
62
  # =======================
63
  # Streamlit App Title and Sidebar
 
104
  st.error("Low confidence in the prediction. Results should be interpreted with caution.")
105
 
106
  # Generate explanation
107
+ explanation = generate_local_explanation(label, confidence)
 
108
 
109
  st.markdown("### Explanation")
110
  st.write(explanation)