Update app.py
Browse files
app.py
CHANGED
|
@@ -1,13 +1,10 @@
|
|
| 1 |
-
|
| 2 |
-
from fastapi.responses import JSONResponse
|
| 3 |
-
from PIL import Image
|
| 4 |
import numpy as np
|
|
|
|
| 5 |
import tensorflow as tf
|
| 6 |
import requests
|
| 7 |
-
import io
|
| 8 |
-
|
| 9 |
-
app = FastAPI(title="رتینوپاتی دیابتی API")
|
| 10 |
|
|
|
|
| 11 |
MODEL_URL = "https://huggingface.co/megsciip/eye1/resolve/main/eye_modelv2.tflite"
|
| 12 |
MODEL_PATH = "eye_modelv2.tflite"
|
| 13 |
|
|
@@ -22,11 +19,13 @@ def download_model():
|
|
| 22 |
|
| 23 |
download_model()
|
| 24 |
|
|
|
|
| 25 |
interpreter = tf.lite.Interpreter(model_path=MODEL_PATH)
|
| 26 |
interpreter.allocate_tensors()
|
| 27 |
input_details = interpreter.get_input_details()
|
| 28 |
output_details = interpreter.get_output_details()
|
| 29 |
|
|
|
|
| 30 |
IMG_SIZE = (224, 224)
|
| 31 |
CLASS_NAMES = [
|
| 32 |
"۰ - بدون رتینوپاتی (چشم سالم)",
|
|
@@ -36,7 +35,8 @@ CLASS_NAMES = [
|
|
| 36 |
"۴ - رتینوپاتی تکثیری (Proliferative DR)"
|
| 37 |
]
|
| 38 |
|
| 39 |
-
|
|
|
|
| 40 |
img = img.resize(IMG_SIZE)
|
| 41 |
if img.mode != 'RGB':
|
| 42 |
img = img.convert('RGB')
|
|
@@ -45,40 +45,57 @@ def preprocess_image(img: Image.Image):
|
|
| 45 |
img_array = tf.keras.applications.resnet50.preprocess_input(img_array)
|
| 46 |
return img_array
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
if
|
| 51 |
-
|
| 52 |
|
| 53 |
try:
|
| 54 |
-
|
| 55 |
-
img = Image.open(io.BytesIO(contents))
|
| 56 |
-
|
| 57 |
-
input_data = preprocess_image(img)
|
| 58 |
interpreter.set_tensor(input_details[0]['index'], input_data)
|
| 59 |
interpreter.invoke()
|
| 60 |
predictions = interpreter.get_tensor(output_details[0]['index'])[0]
|
| 61 |
|
| 62 |
-
|
| 63 |
-
confidence = float(predictions[
|
| 64 |
|
| 65 |
-
result = {
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
}
|
| 71 |
|
| 72 |
-
return
|
| 73 |
-
|
| 74 |
except Exception as e:
|
| 75 |
-
|
| 76 |
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
-
#
|
| 82 |
-
if __name__ == "__main__":
|
| 83 |
-
import uvicorn
|
| 84 |
-
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
| 1 |
+
import gradio as gr
|
|
|
|
|
|
|
| 2 |
import numpy as np
|
| 3 |
+
from PIL import Image
|
| 4 |
import tensorflow as tf
|
| 5 |
import requests
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
# --- دانلود مدل TFLite ---
|
| 8 |
MODEL_URL = "https://huggingface.co/megsciip/eye1/resolve/main/eye_modelv2.tflite"
|
| 9 |
MODEL_PATH = "eye_modelv2.tflite"
|
| 10 |
|
|
|
|
| 19 |
|
| 20 |
download_model()
|
| 21 |
|
| 22 |
+
# --- لود مدل ---
|
| 23 |
interpreter = tf.lite.Interpreter(model_path=MODEL_PATH)
|
| 24 |
interpreter.allocate_tensors()
|
| 25 |
input_details = interpreter.get_input_details()
|
| 26 |
output_details = interpreter.get_output_details()
|
| 27 |
|
| 28 |
+
# --- تنظیمات ---
|
| 29 |
IMG_SIZE = (224, 224)
|
| 30 |
CLASS_NAMES = [
|
| 31 |
"۰ - بدون رتینوپاتی (چشم سالم)",
|
|
|
|
| 35 |
"۴ - رتینوپاتی تکثیری (Proliferative DR)"
|
| 36 |
]
|
| 37 |
|
| 38 |
+
# --- پیشپردازش ساده ---
|
| 39 |
+
def preprocess_image(img):
|
| 40 |
img = img.resize(IMG_SIZE)
|
| 41 |
if img.mode != 'RGB':
|
| 42 |
img = img.convert('RGB')
|
|
|
|
| 45 |
img_array = tf.keras.applications.resnet50.preprocess_input(img_array)
|
| 46 |
return img_array
|
| 47 |
|
| 48 |
+
# --- پیشبینی ساده ---
|
| 49 |
+
def predict_retinopathy(input_image):
|
| 50 |
+
if input_image is None:
|
| 51 |
+
return None, "⚠️ تصویر آپلود کنید!"
|
| 52 |
|
| 53 |
try:
|
| 54 |
+
input_data = preprocess_image(input_image)
|
|
|
|
|
|
|
|
|
|
| 55 |
interpreter.set_tensor(input_details[0]['index'], input_data)
|
| 56 |
interpreter.invoke()
|
| 57 |
predictions = interpreter.get_tensor(output_details[0]['index'])[0]
|
| 58 |
|
| 59 |
+
predicted_class = int(np.argmax(predictions))
|
| 60 |
+
confidence = float(predictions[predicted_class])
|
| 61 |
|
| 62 |
+
result = f"**تشخیص:** {CLASS_NAMES[predicted_class]}\n"
|
| 63 |
+
result += f"**اطمینان:** {confidence:.1%}\n\n"
|
| 64 |
+
result += "**احتمالات:**\n"
|
| 65 |
+
for i, prob in enumerate(predictions):
|
| 66 |
+
result += f"- {CLASS_NAMES[i]}: {prob:.1%}\n"
|
|
|
|
| 67 |
|
| 68 |
+
return input_image, result
|
|
|
|
| 69 |
except Exception as e:
|
| 70 |
+
return input_image, f"خطا: {str(e)}\nسعی کن عکس رنگی JPG آپلود کنی."
|
| 71 |
|
| 72 |
+
# --- اینترفیس: تصویر در چپ (با سایز ثابت) + نتایج در راست ---
|
| 73 |
+
with gr.Blocks() as demo:
|
| 74 |
+
gr.Markdown("# تشخیص رتینوپاتی دیابتی\nآپلود تصویر شبکیه و تشخیص فوری!")
|
| 75 |
+
|
| 76 |
+
with gr.Row():
|
| 77 |
+
# ستون چپ: کادر آپلود و نمایش تصویر (سایز ثابت)
|
| 78 |
+
input_img = gr.Image(
|
| 79 |
+
label="تصویر شبکیه (آپلود کنید)",
|
| 80 |
+
type="pil",
|
| 81 |
+
height=500, # ارتفاع ثابت
|
| 82 |
+
width=500, # عرض ثابت (مربعی)
|
| 83 |
+
interactive=True
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
+
# ستون راست: کادر نتیجه (کنار تصویر)
|
| 87 |
+
output_text = gr.Textbox(
|
| 88 |
+
label="نتیجه تشخیص",
|
| 89 |
+
lines=15, # خطوط کافی برای نمایش همه احتمالات
|
| 90 |
+
interactive=False,
|
| 91 |
+
scale=1 # نسبت عرض ستون (میتونی تنظیم کنی)
|
| 92 |
+
)
|
| 93 |
+
|
| 94 |
+
btn = gr.Button("تشخیص بده")
|
| 95 |
+
btn.click(
|
| 96 |
+
fn=predict_retinopathy,
|
| 97 |
+
inputs=input_img,
|
| 98 |
+
outputs=[input_img, output_text] # تصویر رو در همان کادر refresh میکنه
|
| 99 |
+
)
|
| 100 |
|
| 101 |
+
demo.launch(share=True) # لینک عمومی میدهد
|
|
|
|
|
|
|
|
|