Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import tensorflow as tf | |
| import numpy as np | |
| from PIL import Image | |
| import os | |
| import google.generativeai as genai # β Gemini API | |
| # ---------------- Load Model ---------------- | |
| MODEL_PATH = "final_model.h5" | |
| if not os.path.exists(MODEL_PATH): | |
| raise FileNotFoundError( | |
| f"{MODEL_PATH} not found. Please upload your trained model (final_model.h5)." | |
| ) | |
| # Load Keras model | |
| model = tf.keras.models.load_model(MODEL_PATH) | |
| # ---------------- Gemini API ---------------- | |
| # 1. Get a free API key: https://aistudio.google.com/app/apikey | |
| # 2. On Hugging Face, store your key as a secret: | |
| # Settings β Repository secrets β Add "GEMINI_API_KEY" | |
| GEMINI_API_KEY = os.getenv("GEMINI_API_KEY", "AIzaSyDOhJk8UOVz20YYTd5_e685C68qaOp8nd0") # β safer than hardcoding | |
| if GEMINI_API_KEY: | |
| genai.configure(api_key=GEMINI_API_KEY) | |
| gemini_model = genai.GenerativeModel("gemini-1.5-flash") # β Free, fast model | |
| else: | |
| gemini_model = None | |
| print("β οΈ Warning: GEMINI_API_KEY not set. Explanations will be skipped.") | |
| # ---------------- Prediction + Explanation ---------------- | |
| def predict_and_explain(image: Image.Image): | |
| # Preprocess image for model | |
| img = image.resize((224, 224)) # Adjust to your training input size | |
| img_array = np.array(img) / 255.0 | |
| img_array = np.expand_dims(img_array, axis=0) | |
| # Model prediction | |
| prediction = model.predict(img_array, verbose=0)[0][0] | |
| if prediction > 0.5: | |
| result = f"π₯ Malignant (Cancer Detected) β Confidence: {prediction*100:.2f}%" | |
| prompt = "Explain in simple terms to a patient what it means that this skin lesion is malignant." | |
| else: | |
| result = f"π© Benign (No Cancer) β Confidence: {(1-prediction)*100:.2f}%" | |
| prompt = "Explain in simple terms to a patient what it means that this skin lesion is benign." | |
| # Generate explanation with Gemini | |
| explanation = "β οΈ Gemini explanation not available (API key missing)." | |
| if gemini_model: | |
| try: | |
| response = gemini_model.generate_content(prompt) | |
| explanation = response.text | |
| except Exception as e: | |
| explanation = f"β οΈ AI explanation failed: {str(e)}" | |
| return result, explanation | |
| # ---------------- Gradio UI ---------------- | |
| demo = gr.Interface( | |
| fn=predict_and_explain, | |
| inputs=gr.Image(type="pil", label="π€ Upload Skin Lesion Image"), | |
| outputs=[ | |
| gr.Textbox(label="π Prediction", interactive=False), | |
| gr.Textbox(label="π Explanation (AI-powered)", interactive=False), | |
| ], | |
| title="𧬠Skin Cancer Detection with AI Explanation (Gemini)", | |
| description="Upload a skin lesion image. The model predicts if it is **Malignant** or **Benign** and Gemini explains the result in simple terms.", | |
| ) | |
| # ---------------- Launch ---------------- | |
| if __name__ == "__main__": | |
| demo.launch(share=True) | |