Spaces:
Runtime error
Runtime error
Upload 3 files
Browse files- app.py +60 -0
- final_model.h5 +3 -0
- requirements.txt +4 -0
app.py
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import tensorflow as tf
|
| 3 |
+
import numpy as np
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import os
|
| 6 |
+
import google.generativeai as genai # ✅ Gemini API
|
| 7 |
+
|
| 8 |
+
# ---------------- Load model ----------------
|
| 9 |
+
MODEL_PATH = "final_model.h5"
|
| 10 |
+
if not os.path.exists(MODEL_PATH):
|
| 11 |
+
raise FileNotFoundError(f"{MODEL_PATH} not found. Place your trained model in the project folder.")
|
| 12 |
+
|
| 13 |
+
model = tf.keras.models.load_model(MODEL_PATH)
|
| 14 |
+
|
| 15 |
+
# ---------------- Gemini API ----------------
|
| 16 |
+
# 1. Go to https://aistudio.google.com/app/apikey to create a FREE API key
|
| 17 |
+
# 2. Replace below with your API key
|
| 18 |
+
GEMINI_API_KEY = "AIzaSyC6LKYAB5F1B_j3BOBVFB9xt1-rPbZIMF0"
|
| 19 |
+
genai.configure(api_key=GEMINI_API_KEY)
|
| 20 |
+
|
| 21 |
+
gemini_model = genai.GenerativeModel("gemini-1.5-flash") # ✅ Free, fast model
|
| 22 |
+
|
| 23 |
+
# ---------------- Prediction + Explanation ----------------
|
| 24 |
+
def predict_and_explain(image):
|
| 25 |
+
# Preprocess image
|
| 26 |
+
img = image.resize((224, 224)) # Adjust if your model uses a different size
|
| 27 |
+
img_array = np.array(img) / 255.0
|
| 28 |
+
img_array = np.expand_dims(img_array, axis=0)
|
| 29 |
+
|
| 30 |
+
# Predict
|
| 31 |
+
prediction = model.predict(img_array)[0][0]
|
| 32 |
+
if prediction > 0.5:
|
| 33 |
+
result = f"🟥 Malignant (Cancer Detected) with {prediction*100:.2f}% confidence"
|
| 34 |
+
prompt = "Explain in simple terms to a patient what it means that this skin lesion is Malignant."
|
| 35 |
+
else:
|
| 36 |
+
result = f"🟩 Benign (No Cancer) with {(1-prediction)*100:.2f}% confidence"
|
| 37 |
+
prompt = "Explain in simple terms to a patient what it means that this skin lesion is Benign."
|
| 38 |
+
|
| 39 |
+
# Generate explanation using Gemini
|
| 40 |
+
explanation = "Explanation not available."
|
| 41 |
+
try:
|
| 42 |
+
response = gemini_model.generate_content(prompt)
|
| 43 |
+
explanation = response.text
|
| 44 |
+
except Exception as e:
|
| 45 |
+
explanation = f"AI explanation failed: {e}"
|
| 46 |
+
|
| 47 |
+
return result, explanation
|
| 48 |
+
|
| 49 |
+
# ---------------- Gradio UI ----------------
|
| 50 |
+
demo = gr.Interface(
|
| 51 |
+
fn=predict_and_explain,
|
| 52 |
+
inputs=gr.Image(type="pil", label="Upload Skin Lesion Image"),
|
| 53 |
+
outputs=[gr.Textbox(label="Prediction"), gr.Textbox(label="Explanation")],
|
| 54 |
+
title="🧬 Skin Cancer Detection with AI Explanation (Gemini)",
|
| 55 |
+
description="Upload a skin lesion image. The model predicts if it is Malignant or Benign and explains the result in simple terms."
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
# ---------------- Launch ----------------
|
| 59 |
+
if __name__ == "__main__":
|
| 60 |
+
demo.launch()
|
final_model.h5
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:8ff8ac00211462b4b533154bae22362e5501838219a1dfb9eecf3bd8121ca533
|
| 3 |
+
size 24526712
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
tensorflow
|
| 2 |
+
opencv-python
|
| 3 |
+
numpy
|
| 4 |
+
gradio
|