Muhammad Abdiel Al Hafiz
commited on
Commit
·
ee36b3c
1
Parent(s):
95a20a3
try integrate gemini
Browse files
app.py
CHANGED
|
@@ -2,12 +2,22 @@ import gradio as gr
|
|
| 2 |
import tensorflow as tf
|
| 3 |
import numpy as np
|
| 4 |
from PIL import Image
|
|
|
|
|
|
|
| 5 |
|
| 6 |
model_path = 'model'
|
| 7 |
model = tf.saved_model.load(model_path)
|
| 8 |
|
|
|
|
|
|
|
|
|
|
| 9 |
labels = ['cataract', 'diabetic_retinopathy', 'glaucoma', 'normal']
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
def predict_image(image):
|
| 12 |
image_resized = image.resize((224, 224))
|
| 13 |
image_array = np.array(image_resized).astype(np.float32) / 255.0
|
|
@@ -20,7 +30,9 @@ def predict_image(image):
|
|
| 20 |
top_label = labels[top_index]
|
| 21 |
top_probability = predictions.numpy()[0][top_index]
|
| 22 |
|
| 23 |
-
|
|
|
|
|
|
|
| 24 |
|
| 25 |
# Example images
|
| 26 |
example_images = [
|
|
@@ -36,7 +48,7 @@ example_images = [
|
|
| 36 |
interface = gr.Interface(
|
| 37 |
fn=predict_image,
|
| 38 |
inputs=gr.Image(type="pil"),
|
| 39 |
-
outputs=gr.Label(num_top_classes=1, label="Prediction"),
|
| 40 |
examples=example_images,
|
| 41 |
title="Eye Diseases Classifier",
|
| 42 |
description="Upload an image of an eye fundus, and the model will predict it.\n\n**Disclaimer:** This model is intended as a form of learning process in the field of health-related machine learning and was trained with a limited amount and variety of data with a total of about 4000 data, so the prediction results may not always be correct. There is still a lot of room for improvisation on this model in the future.",
|
|
|
|
| 2 |
import tensorflow as tf
|
| 3 |
import numpy as np
|
| 4 |
from PIL import Image
|
| 5 |
+
import google.generativeasi as genai
|
| 6 |
+
import os
|
| 7 |
|
| 8 |
model_path = 'model'
|
| 9 |
model = tf.saved_model.load(model_path)
|
| 10 |
|
| 11 |
+
api_key = os.getenv("GEMINI_API_KEY")
|
| 12 |
+
genai.configure(api_key=api_key)
|
| 13 |
+
|
| 14 |
labels = ['cataract', 'diabetic_retinopathy', 'glaucoma', 'normal']
|
| 15 |
|
| 16 |
+
def get_disease_detail(disease_name):
|
| 17 |
+
prompt = f"Diagnosis: {disease_name}\n\nWhat is it?\n(Description about {disease_name})\n\nWhat cause it?\n(Explain what causes {disease_name})\n\nSuggestion\n(Suggestion to user)\n\nReminder: Always seek professional help, such as a doctor."
|
| 18 |
+
response = genai.GenerativeModel("gemini-1.5-flash").generate_content(prompt)
|
| 19 |
+
return response.text
|
| 20 |
+
|
| 21 |
def predict_image(image):
|
| 22 |
image_resized = image.resize((224, 224))
|
| 23 |
image_array = np.array(image_resized).astype(np.float32) / 255.0
|
|
|
|
| 30 |
top_label = labels[top_index]
|
| 31 |
top_probability = predictions.numpy()[0][top_index]
|
| 32 |
|
| 33 |
+
explanation = get_disease_detail(top_label)
|
| 34 |
+
|
| 35 |
+
return {top_label: top_probability, "explanation": explanation}
|
| 36 |
|
| 37 |
# Example images
|
| 38 |
example_images = [
|
|
|
|
| 48 |
interface = gr.Interface(
|
| 49 |
fn=predict_image,
|
| 50 |
inputs=gr.Image(type="pil"),
|
| 51 |
+
outputs=[gr.Label(num_top_classes=1, label="Prediction"), gr.Textbox(label="Explanation")],
|
| 52 |
examples=example_images,
|
| 53 |
title="Eye Diseases Classifier",
|
| 54 |
description="Upload an image of an eye fundus, and the model will predict it.\n\n**Disclaimer:** This model is intended as a form of learning process in the field of health-related machine learning and was trained with a limited amount and variety of data with a total of about 4000 data, so the prediction results may not always be correct. There is still a lot of room for improvisation on this model in the future.",
|