Update app.py
Browse files
app.py
CHANGED
|
@@ -1,75 +1,80 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import mlflow
|
| 3 |
-
import
|
| 4 |
-
import time
|
| 5 |
-
import tempfile
|
| 6 |
-
import shutil
|
| 7 |
from ultralytics import YOLO
|
| 8 |
from PIL import Image
|
| 9 |
import cv2
|
| 10 |
import numpy as np
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
# ==============================
|
| 13 |
-
# MLflow
|
| 14 |
# ==============================
|
| 15 |
tracking_uri = os.getenv("MLFLOW_TRACKING_URI")
|
| 16 |
username = os.getenv("MLFLOW_TRACKING_USERNAME")
|
| 17 |
password = os.getenv("MLFLOW_TRACKING_PASSWORD")
|
| 18 |
|
| 19 |
if not all([tracking_uri, username, password]):
|
| 20 |
-
raise ValueError("MLflow Secrets مش
|
| 21 |
|
| 22 |
os.environ["MLFLOW_TRACKING_URI"] = tracking_uri
|
| 23 |
os.environ["MLFLOW_TRACKING_USERNAME"] = username
|
| 24 |
os.environ["MLFLOW_TRACKING_PASSWORD"] = password
|
| 25 |
|
| 26 |
-
#
|
|
|
|
|
|
|
|
|
|
| 27 |
mlflow.set_experiment("YOLOv12s_Inference_Logs")
|
| 28 |
-
|
|
|
|
| 29 |
|
| 30 |
# ==============================
|
| 31 |
-
#
|
| 32 |
# ==============================
|
| 33 |
-
|
|
|
|
| 34 |
|
| 35 |
# ==============================
|
| 36 |
-
# Inference
|
| 37 |
# ==============================
|
| 38 |
def run_inference(media_file, media_type):
|
| 39 |
if media_file is None:
|
| 40 |
-
return None, None, "ارفع
|
| 41 |
|
| 42 |
media_path = media_file.name
|
| 43 |
|
| 44 |
-
with mlflow.start_run(run_name=f"
|
| 45 |
mlflow.log_param("media_type", media_type)
|
| 46 |
-
mlflow.log_param("
|
| 47 |
mlflow.log_param("timestamp", time.strftime("%Y-%m-%d %H:%M:%S"))
|
| 48 |
|
|
|
|
|
|
|
| 49 |
if media_type == "Image":
|
| 50 |
img = Image.open(media_path).convert("RGB")
|
| 51 |
img_array = np.array(img)
|
| 52 |
|
| 53 |
results = model(img_array)[0]
|
| 54 |
annotated = results.plot()
|
| 55 |
-
output_img = Image.fromarray(annotated[..., ::-1])
|
| 56 |
|
| 57 |
-
#
|
| 58 |
with tempfile.TemporaryDirectory() as tmpdir:
|
| 59 |
in_path = os.path.join(tmpdir, "input.jpg")
|
| 60 |
out_path = os.path.join(tmpdir, "output.jpg")
|
| 61 |
img.save(in_path)
|
| 62 |
output_img.save(out_path)
|
| 63 |
|
| 64 |
-
mlflow.log_artifact(in_path,
|
| 65 |
-
mlflow.log_artifact(out_path,
|
| 66 |
|
| 67 |
detections = len(results.boxes) if results.boxes is not None else 0
|
| 68 |
-
mlflow.log_metric("
|
| 69 |
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
return output_img, None, f"✅ تم التسجيل!\nRun ID: {run.info.run_id}\n[شوف الـ Log على DagsHub]({run_url})"
|
| 73 |
|
| 74 |
else: # Video
|
| 75 |
cap = cv2.VideoCapture(media_path)
|
|
@@ -96,48 +101,106 @@ def run_inference(media_file, media_type):
|
|
| 96 |
cap.release()
|
| 97 |
writer.release()
|
| 98 |
|
| 99 |
-
|
| 100 |
-
mlflow.log_artifact(
|
| 101 |
-
mlflow.
|
| 102 |
-
|
| 103 |
-
mlflow.log_metric("frames_processed", frame_count)
|
| 104 |
mlflow.log_metric("total_detections", total_detections)
|
| 105 |
-
mlflow.log_metric("avg_detections_per_frame", total_detections / frame_count if frame_count > 0 else 0)
|
| 106 |
-
|
| 107 |
-
run_url = f"https://dagshub.com/Mosensei7/AutonomousVehiclesDetectionDEPI/mlflow/#/experiments/{mlflow.get_experiment_by_name('YOLOv12s_Inference_Logs').experiment_id}/runs/{run.info.run_id}"
|
| 108 |
|
| 109 |
-
# حذف الفيديو المؤقت
|
| 110 |
if os.path.exists(output_video):
|
| 111 |
os.remove(output_video)
|
| 112 |
|
| 113 |
-
return None, output_video, f"
|
| 114 |
|
| 115 |
# ==============================
|
| 116 |
-
#
|
| 117 |
# ==============================
|
| 118 |
css = """
|
| 119 |
-
body {
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
"""
|
| 124 |
|
| 125 |
-
with gr.Blocks(css=css, theme=gr.themes.
|
| 126 |
gr.Markdown("""
|
| 127 |
-
# 🚀 YOLOv12s
|
| 128 |
-
|
|
|
|
| 129 |
""")
|
| 130 |
|
| 131 |
with gr.Row():
|
| 132 |
with gr.Column(scale=1):
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
|
| 137 |
with gr.Column(scale=2):
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
|
|
|
| 141 |
|
| 142 |
btn.click(
|
| 143 |
fn=run_inference,
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import mlflow
|
| 3 |
+
import dagshub
|
|
|
|
|
|
|
|
|
|
| 4 |
from ultralytics import YOLO
|
| 5 |
from PIL import Image
|
| 6 |
import cv2
|
| 7 |
import numpy as np
|
| 8 |
+
import os
|
| 9 |
+
import time
|
| 10 |
+
import tempfile
|
| 11 |
|
| 12 |
# ==============================
|
| 13 |
+
# MLflow Authentication via Secrets (زي ما أنت عايز بالظبط)
|
| 14 |
# ==============================
|
| 15 |
tracking_uri = os.getenv("MLFLOW_TRACKING_URI")
|
| 16 |
username = os.getenv("MLFLOW_TRACKING_USERNAME")
|
| 17 |
password = os.getenv("MLFLOW_TRACKING_PASSWORD")
|
| 18 |
|
| 19 |
if not all([tracking_uri, username, password]):
|
| 20 |
+
raise ValueError("❌ MLflow Secrets مش موجودة أو غلط! روح Settings → Secrets وتأكد من الأسماء التلاتة")
|
| 21 |
|
| 22 |
os.environ["MLFLOW_TRACKING_URI"] = tracking_uri
|
| 23 |
os.environ["MLFLOW_TRACKING_USERNAME"] = username
|
| 24 |
os.environ["MLFLOW_TRACKING_PASSWORD"] = password
|
| 25 |
|
| 26 |
+
# تفعيل DagsHub
|
| 27 |
+
dagshub.init(repo_owner="Mosensei7", repo_name="AutonomousVehiclesDetectionDEPI", mlflow=True)
|
| 28 |
+
|
| 29 |
+
# إنشاء experiment مخصص
|
| 30 |
mlflow.set_experiment("YOLOv12s_Inference_Logs")
|
| 31 |
+
|
| 32 |
+
print("✅ MLflow + DagsHub connected successfully via Secrets!")
|
| 33 |
|
| 34 |
# ==============================
|
| 35 |
+
# تحميل الموديل الصحيح
|
| 36 |
# ==============================
|
| 37 |
+
# لو رافع الموديل على HF كـ repo، استخدم الاسم الكامل
|
| 38 |
+
model = YOLO("Mosensei7/yolov12s-egyptian-autonomous-vehicles/best.pt") # غيّر الاسم لو مختلف
|
| 39 |
|
| 40 |
# ==============================
|
| 41 |
+
# Inference مع رفع كل حاجة على DagsHub
|
| 42 |
# ==============================
|
| 43 |
def run_inference(media_file, media_type):
|
| 44 |
if media_file is None:
|
| 45 |
+
return None, None, "⚠️ ارفع صورة أو فيديو أولاً"
|
| 46 |
|
| 47 |
media_path = media_file.name
|
| 48 |
|
| 49 |
+
with mlflow.start_run(run_name=f"YOLOv12s_Inference_{int(time.time())}") as run:
|
| 50 |
mlflow.log_param("media_type", media_type)
|
| 51 |
+
mlflow.log_param("model_version", "YOLOv12s")
|
| 52 |
mlflow.log_param("timestamp", time.strftime("%Y-%m-%d %H:%M:%S"))
|
| 53 |
|
| 54 |
+
run_url = f"https://dagshub.com/Mosensei7/AutonomousVehiclesDetectionDEPI/mlflow/#/experiments/{mlflow.get_experiment_by_name('YOLOv12s_Inference_Logs').experiment_id}/runs/{run.info.run_id}"
|
| 55 |
+
|
| 56 |
if media_type == "Image":
|
| 57 |
img = Image.open(media_path).convert("RGB")
|
| 58 |
img_array = np.array(img)
|
| 59 |
|
| 60 |
results = model(img_array)[0]
|
| 61 |
annotated = results.plot()
|
| 62 |
+
output_img = Image.fromarray(annotated[..., ::-1])
|
| 63 |
|
| 64 |
+
# رفع input/output
|
| 65 |
with tempfile.TemporaryDirectory() as tmpdir:
|
| 66 |
in_path = os.path.join(tmpdir, "input.jpg")
|
| 67 |
out_path = os.path.join(tmpdir, "output.jpg")
|
| 68 |
img.save(in_path)
|
| 69 |
output_img.save(out_path)
|
| 70 |
|
| 71 |
+
mlflow.log_artifact(in_path, "input")
|
| 72 |
+
mlflow.log_artifact(out_path, "output")
|
| 73 |
|
| 74 |
detections = len(results.boxes) if results.boxes is not None else 0
|
| 75 |
+
mlflow.log_metric("detections", detections)
|
| 76 |
|
| 77 |
+
return output_img, None, f"**✅ تم الكشف والتسجيل بنجاح!**\n\n**Run ID:** `{run.info.run_id}`\n[👀 شوف الـ Log كامل على DagsHub]({run_url})"
|
|
|
|
|
|
|
| 78 |
|
| 79 |
else: # Video
|
| 80 |
cap = cv2.VideoCapture(media_path)
|
|
|
|
| 101 |
cap.release()
|
| 102 |
writer.release()
|
| 103 |
|
| 104 |
+
mlflow.log_artifact(media_path, "input_video")
|
| 105 |
+
mlflow.log_artifact(output_video, "output_video")
|
| 106 |
+
mlflow.log_metric("frames", frame_count)
|
|
|
|
|
|
|
| 107 |
mlflow.log_metric("total_detections", total_detections)
|
| 108 |
+
mlflow.log_metric("avg_detections_per_frame", round(total_detections / frame_count, 2) if frame_count > 0 else 0)
|
|
|
|
|
|
|
| 109 |
|
| 110 |
+
# حذف الفيديو المؤقت
|
| 111 |
if os.path.exists(output_video):
|
| 112 |
os.remove(output_video)
|
| 113 |
|
| 114 |
+
return None, output_video, f"**✅ تم معالجة الفيديو والتسجيل بنجاح!**\n\n**Run ID:** `{run.info.run_id}`\n[👀 شوف الـ Log كامل على DagsHub]({run_url})"
|
| 115 |
|
| 116 |
# ==============================
|
| 117 |
+
# التصميم الفضائي الرهيب جدًا (Cyberpunk 2077 style)
|
| 118 |
# ==============================
|
| 119 |
css = """
|
| 120 |
+
body {
|
| 121 |
+
background: linear-gradient(135deg, #000428, #004e92);
|
| 122 |
+
color: #00ffea;
|
| 123 |
+
font-family: 'Rajdhani', sans-serif;
|
| 124 |
+
}
|
| 125 |
+
.gradio-container {
|
| 126 |
+
max-width: 1200px;
|
| 127 |
+
margin: 0 auto;
|
| 128 |
+
background: linear-gradient(145deg, rgba(0,20,40,0.9), rgba(0,10,30,0.95));
|
| 129 |
+
border: 3px solid #00ffea;
|
| 130 |
+
border-radius: 30px;
|
| 131 |
+
padding: 40px;
|
| 132 |
+
box-shadow:
|
| 133 |
+
0 0 30px #00ffea,
|
| 134 |
+
inset 0 0 30px rgba(0,255,234,0.2);
|
| 135 |
+
}
|
| 136 |
+
h1 {
|
| 137 |
+
text-align: center;
|
| 138 |
+
font-size: 3.8em;
|
| 139 |
+
color: #00ffea;
|
| 140 |
+
text-shadow:
|
| 141 |
+
0 0 10px #00ffea,
|
| 142 |
+
0 0 20px #00ffea,
|
| 143 |
+
0 0 40px #00ffea;
|
| 144 |
+
letter-spacing: 4px;
|
| 145 |
+
margin-bottom: 20px;
|
| 146 |
+
}
|
| 147 |
+
p {
|
| 148 |
+
text-align: center;
|
| 149 |
+
font-size: 1.4em;
|
| 150 |
+
color: #00ffea;
|
| 151 |
+
text-shadow: 0 0 10px #00ffea;
|
| 152 |
+
}
|
| 153 |
+
button {
|
| 154 |
+
background: linear-gradient(45deg, #ff00ff, #00ffff, #00ff00);
|
| 155 |
+
background-size: 300% 300%;
|
| 156 |
+
animation: gradient 5s ease infinite;
|
| 157 |
+
border: none;
|
| 158 |
+
border-radius: 25px;
|
| 159 |
+
font-size: 1.6em;
|
| 160 |
+
height: 70px;
|
| 161 |
+
font-weight: bold;
|
| 162 |
+
box-shadow: 0 0 30px #00ffff;
|
| 163 |
+
transition: all 0.3s;
|
| 164 |
+
}
|
| 165 |
+
button:hover {
|
| 166 |
+
transform: scale(1.05);
|
| 167 |
+
box-shadow: 0 0 50px #00ffff;
|
| 168 |
+
}
|
| 169 |
+
@keyframes gradient {
|
| 170 |
+
0% { background-position: 0% 50%; }
|
| 171 |
+
50% { background-position: 100% 50%; }
|
| 172 |
+
100% { background-position: 0% 50%; }
|
| 173 |
+
}
|
| 174 |
"""
|
| 175 |
|
| 176 |
+
with gr.Blocks(css=css, theme=gr.themes.Soft()) as demo:
|
| 177 |
gr.Markdown("""
|
| 178 |
+
# 🚀 YOLOv12s
|
| 179 |
+
### كشف المركبات في الشوارع المصرية بذكاء اصطناعي 🇪🇬
|
| 180 |
+
**كل عملية كشف بتترفع تلقائيًا على DagsHub MLflow مع كل التفاصيل**
|
| 181 |
""")
|
| 182 |
|
| 183 |
with gr.Row():
|
| 184 |
with gr.Column(scale=1):
|
| 185 |
+
gr.Markdown("### 📤 ارفع ملفك")
|
| 186 |
+
media = gr.File(
|
| 187 |
+
label="صورة أو فيديو",
|
| 188 |
+
file_types=[".jpg", ".jpeg", ".png", ".mp4", ".avi"],
|
| 189 |
+
height=200
|
| 190 |
+
)
|
| 191 |
+
media_type = gr.Radio(
|
| 192 |
+
["Image", "Video"],
|
| 193 |
+
label="نوع الميديا",
|
| 194 |
+
value="Image",
|
| 195 |
+
info="اختار نوع الملف اللي هترفعه"
|
| 196 |
+
)
|
| 197 |
+
btn = gr.Button("🔥 ابدأ الكشف الآن", variant="primary", size="lg")
|
| 198 |
|
| 199 |
with gr.Column(scale=2):
|
| 200 |
+
gr.Markdown("### 📊 النتيجة")
|
| 201 |
+
img_out = gr.Image(label="نتيجة الكشف على الصورة", height=550)
|
| 202 |
+
vid_out = gr.Video(label="نتيجة الكشف على الفيديو", height=550)
|
| 203 |
+
info = gr.Markdown("**جاهز للكشف... ارفع ملف واضغط ابدأ**")
|
| 204 |
|
| 205 |
btn.click(
|
| 206 |
fn=run_inference,
|