Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -41,8 +41,10 @@ def start_new_session():
|
|
| 41 |
session_id = session_data.get("session_id")
|
| 42 |
if not session_id:
|
| 43 |
raise gr.Error("لم يتمكن الخادم من بدء جلسة جديدة.")
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
| 46 |
return session_id, status_message, gr.update(visible=True)
|
| 47 |
except requests.exceptions.RequestException as e:
|
| 48 |
error_message = f"فشل الاتصال بخادم الـ API: {e}"
|
|
@@ -52,16 +54,16 @@ def start_new_session():
|
|
| 52 |
def handle_end_session(session_id):
|
| 53 |
"""ينهي الجلسة الحالية على الخادم."""
|
| 54 |
if not session_id:
|
| 55 |
-
# إذا لم تكن هناك جلسة، فقط حدث الواجهة
|
| 56 |
return "لا توجد جلسة نشطة لإنهائها.", None, gr.update(visible=False)
|
| 57 |
|
| 58 |
print(f"Requesting to end session: {session_id}")
|
| 59 |
try:
|
| 60 |
response = requests.post(f"{API_END_SESSION_URL}?session_id={session_id}", timeout=30)
|
| 61 |
response.raise_for_status()
|
| 62 |
-
|
|
|
|
|
|
|
| 63 |
print(status_message)
|
| 64 |
-
# إعادة تعيين معرف الجلسة وإخفاء الواجهة
|
| 65 |
return status_message, None, gr.update(visible=False)
|
| 66 |
except requests.exceptions.RequestException as e:
|
| 67 |
error_message = f"حدث خطأ أثناء إنهاء الجلسة: {e}"
|
|
@@ -113,6 +115,8 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue", secondary_hue="sky"), cs
|
|
| 113 |
gr.Markdown("# 🚗 واجهة التحكم لنظام Baseer للقيادة الذاتية")
|
| 114 |
gr.Markdown("هذه الواجهة تعمل كـ **عميل (Client)** يتصل بـ **[Baseer Self-Driving API](https://huggingface.co/spaces/BaseerAI/baseer-server)** لمعالجة البيانات.")
|
| 115 |
|
|
|
|
|
|
|
| 116 |
with gr.Group():
|
| 117 |
gr.Markdown("## ⚙️ التحكم بالجلسة")
|
| 118 |
with gr.Row():
|
|
@@ -120,25 +124,21 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue", secondary_hue="sky"), cs
|
|
| 120 |
end_session_button = gr.Button("🔴 إنهاء الجلسة الحالية")
|
| 121 |
status_textbox = gr.Textbox(label="حالة الجلسة", interactive=False, value="في انتظار بدء الجلسة...")
|
| 122 |
|
|
|
|
|
|
|
| 123 |
with gr.Group(visible=False) as main_controls_group:
|
| 124 |
with gr.Row():
|
| 125 |
# -- العمود الأيسر: الإعدادات والمدخلات --
|
| 126 |
-
with gr.Column(scale=
|
| 127 |
with gr.Group():
|
| 128 |
-
gr.Markdown("## 🗂️ ارفع ملفات السيناريو")
|
| 129 |
-
|
| 130 |
-
# استخدام gr.Image لعرض الصور المصغرة في الأمثلة
|
| 131 |
api_rgb_image_path = gr.Image(type="filepath", label="صورة الكاميرا الأمامية (RGB)")
|
| 132 |
api_measurements_path = gr.File(label="ملف القياسات (JSON)", type="filepath")
|
| 133 |
api_target_point_list = gr.JSON(label="📍 النقطة المستهدفة (x, y) بالنسبة للسيارة", value=[0.0, 100.0])
|
| 134 |
api_run_button = gr.Button("🚀 أرسل البيانات للمعالجة", variant="primary")
|
| 135 |
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
with gr.Row(visible=False):
|
| 139 |
-
example_speed = gr.Textbox(label="Speed")
|
| 140 |
-
example_pos = gr.Textbox(label="Position")
|
| 141 |
-
|
| 142 |
with gr.Group():
|
| 143 |
gr.Markdown("### ✨ أمثلة جاهزة")
|
| 144 |
# التحقق من وجود مجلد الأمثلة لتجنب الأخطاء
|
|
@@ -146,7 +146,6 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue", secondary_hue="sky"), cs
|
|
| 146 |
os.path.exists(os.path.join(EXAMPLES_DIR, "sample1", "measurements.json")) and \
|
| 147 |
os.path.exists(os.path.join(EXAMPLES_DIR, "sample2", "measurements.json")):
|
| 148 |
|
| 149 |
-
# --- [تعديل 2] ��وسيع قائمة الأمثلة ---
|
| 150 |
# قراءة بيانات المثال الأول
|
| 151 |
with open(os.path.join(EXAMPLES_DIR, "sample1", "measurements.json"), 'r') as f:
|
| 152 |
data1 = json.load(f)
|
|
@@ -154,42 +153,42 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue", secondary_hue="sky"), cs
|
|
| 154 |
# قراءة بيانات المثال الثاني
|
| 155 |
with open(os.path.join(EXAMPLES_DIR, "sample2", "measurements.json"), 'r') as f:
|
| 156 |
data2 = json.load(f)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
|
| 158 |
gr.Examples(
|
| 159 |
examples=[
|
| 160 |
-
# بيانات المثال الأول
|
| 161 |
[
|
| 162 |
os.path.join(EXAMPLES_DIR, "sample1", "rgb.jpg"),
|
| 163 |
os.path.join(EXAMPLES_DIR, "sample1", "measurements.json"),
|
| 164 |
-
str(data1.get('speed',
|
| 165 |
-
str([data1.get('x',
|
| 166 |
],
|
| 167 |
-
# بيانات المثال الثاني
|
| 168 |
[
|
| 169 |
os.path.join(EXAMPLES_DIR, "sample2", "rgb.jpg"),
|
| 170 |
os.path.join(EXAMPLES_DIR, "sample2", "measurements.json"),
|
| 171 |
-
str(data2.get('speed',
|
| 172 |
-
str([data2.get('x',
|
| 173 |
]
|
| 174 |
],
|
| 175 |
-
# --- [تعديل 3] ربط البيانات بالمكونات ---
|
| 176 |
inputs=[api_rgb_image_path, api_measurements_path, example_speed, example_pos],
|
| 177 |
label="اختر سيناريو اختبار",
|
| 178 |
-
|
| 179 |
)
|
| 180 |
else:
|
| 181 |
gr.Markdown("لم يتم العثور على مجلد الأمثلة (`examples`) أو محتوياته.")
|
| 182 |
|
| 183 |
-
|
| 184 |
# -- العمود الأيمن: المخرجات --
|
| 185 |
-
with gr.Column(scale=
|
| 186 |
with gr.Group():
|
| 187 |
-
gr.Markdown("## 📊 النتائج من الخادم")
|
| 188 |
-
api_output_image = gr.Image(label="لوحة التحكم المرئية (من الـ API)", type="pil", interactive=False)
|
| 189 |
api_control_json = gr.JSON(label="أوامر التحكم (من الـ API)")
|
| 190 |
|
| 191 |
# --- ربط منطق الواجهة ---
|
| 192 |
-
# هذا الجزء لا يحتاج إلى تعديل
|
| 193 |
start_session_button.click(
|
| 194 |
fn=start_new_session,
|
| 195 |
inputs=None,
|
|
@@ -207,5 +206,6 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue", secondary_hue="sky"), cs
|
|
| 207 |
inputs=[session_id_state, api_rgb_image_path, api_measurements_path, api_target_point_list],
|
| 208 |
outputs=[api_output_image, api_control_json],
|
| 209 |
)
|
|
|
|
| 210 |
if __name__ == "__main__":
|
| 211 |
demo.queue().launch(debug=True)
|
|
|
|
| 41 |
session_id = session_data.get("session_id")
|
| 42 |
if not session_id:
|
| 43 |
raise gr.Error("لم يتمكن الخادم من بدء جلسة جديدة.")
|
| 44 |
+
|
| 45 |
+
# --- [تحسين] رسالة أكثر وضوحاً ---
|
| 46 |
+
status_message = f"✅ تم بدء جلسة جديدة بنجاح. أنت الآن متصل بالخادم."
|
| 47 |
+
print(f"Session started successfully: {session_id}")
|
| 48 |
return session_id, status_message, gr.update(visible=True)
|
| 49 |
except requests.exceptions.RequestException as e:
|
| 50 |
error_message = f"فشل الاتصال بخادم الـ API: {e}"
|
|
|
|
| 54 |
def handle_end_session(session_id):
|
| 55 |
"""ينهي الجلسة الحالية على الخادم."""
|
| 56 |
if not session_id:
|
|
|
|
| 57 |
return "لا توجد جلسة نشطة لإنهائها.", None, gr.update(visible=False)
|
| 58 |
|
| 59 |
print(f"Requesting to end session: {session_id}")
|
| 60 |
try:
|
| 61 |
response = requests.post(f"{API_END_SESSION_URL}?session_id={session_id}", timeout=30)
|
| 62 |
response.raise_for_status()
|
| 63 |
+
|
| 64 |
+
# --- [تحسين] رسالة أكثر وضوحاً ---
|
| 65 |
+
status_message = f"🔌 تم إنهاء الجلسة بنجاح. انقطع الاتصال بالخادم."
|
| 66 |
print(status_message)
|
|
|
|
| 67 |
return status_message, None, gr.update(visible=False)
|
| 68 |
except requests.exceptions.RequestException as e:
|
| 69 |
error_message = f"حدث خطأ أثناء إنهاء الجلسة: {e}"
|
|
|
|
| 115 |
gr.Markdown("# 🚗 واجهة التحكم لنظام Baseer للقيادة الذاتية")
|
| 116 |
gr.Markdown("هذه الواجهة تعمل كـ **عميل (Client)** يتصل بـ **[Baseer Self-Driving API](https://huggingface.co/spaces/BaseerAI/baseer-server)** لمعالجة البيانات.")
|
| 117 |
|
| 118 |
+
gr.Markdown("---") # <-- فاصل لتحسين الشكل
|
| 119 |
+
|
| 120 |
with gr.Group():
|
| 121 |
gr.Markdown("## ⚙️ التحكم بالجلسة")
|
| 122 |
with gr.Row():
|
|
|
|
| 124 |
end_session_button = gr.Button("🔴 إنهاء الجلسة الحالية")
|
| 125 |
status_textbox = gr.Textbox(label="حالة الجلسة", interactive=False, value="في انتظار بدء الجلسة...")
|
| 126 |
|
| 127 |
+
gr.Markdown("") # <-- فاصل لتحسين الشكل
|
| 128 |
+
|
| 129 |
with gr.Group(visible=False) as main_controls_group:
|
| 130 |
with gr.Row():
|
| 131 |
# -- العمود الأيسر: الإعدادات والمدخلات --
|
| 132 |
+
with gr.Column(scale=2): # <-- توزيع وزن للأعمدة
|
| 133 |
with gr.Group():
|
| 134 |
+
gr.Markdown("### 🗂️ ارفع ملفات السيناريو")
|
|
|
|
|
|
|
| 135 |
api_rgb_image_path = gr.Image(type="filepath", label="صورة الكاميرا الأمامية (RGB)")
|
| 136 |
api_measurements_path = gr.File(label="ملف القياسات (JSON)", type="filepath")
|
| 137 |
api_target_point_list = gr.JSON(label="📍 النقطة المستهدفة (x, y) بالنسبة للسيارة", value=[0.0, 100.0])
|
| 138 |
api_run_button = gr.Button("🚀 أرسل البيانات للمعالجة", variant="primary")
|
| 139 |
|
| 140 |
+
gr.Markdown("") # <-- فاصل
|
| 141 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
with gr.Group():
|
| 143 |
gr.Markdown("### ✨ أمثلة جاهزة")
|
| 144 |
# التحقق من وجود مجلد الأمثلة لتجنب الأخطاء
|
|
|
|
| 146 |
os.path.exists(os.path.join(EXAMPLES_DIR, "sample1", "measurements.json")) and \
|
| 147 |
os.path.exists(os.path.join(EXAMPLES_DIR, "sample2", "measurements.json")):
|
| 148 |
|
|
|
|
| 149 |
# قراءة بيانات المثال الأول
|
| 150 |
with open(os.path.join(EXAMPLES_DIR, "sample1", "measurements.json"), 'r') as f:
|
| 151 |
data1 = json.load(f)
|
|
|
|
| 153 |
# قراءة بيانات المثال الثاني
|
| 154 |
with open(os.path.join(EXAMPLES_DIR, "sample2", "measurements.json"), 'r') as f:
|
| 155 |
data2 = json.load(f)
|
| 156 |
+
|
| 157 |
+
# مكونات نصية مخفية لإظهار البيانات في الأعمدة
|
| 158 |
+
with gr.Row(visible=False):
|
| 159 |
+
example_speed = gr.Textbox(label="Speed")
|
| 160 |
+
example_pos = gr.Textbox(label="Position")
|
| 161 |
|
| 162 |
gr.Examples(
|
| 163 |
examples=[
|
|
|
|
| 164 |
[
|
| 165 |
os.path.join(EXAMPLES_DIR, "sample1", "rgb.jpg"),
|
| 166 |
os.path.join(EXAMPLES_DIR, "sample1", "measurements.json"),
|
| 167 |
+
str(round(data1.get('speed', 0), 2)),
|
| 168 |
+
str([round(data1.get('x', 0), 2), round(data1.get('y', 0), 2)])
|
| 169 |
],
|
|
|
|
| 170 |
[
|
| 171 |
os.path.join(EXAMPLES_DIR, "sample2", "rgb.jpg"),
|
| 172 |
os.path.join(EXAMPLES_DIR, "sample2", "measurements.json"),
|
| 173 |
+
str(round(data2.get('speed', 0), 2)),
|
| 174 |
+
str([round(data2.get('x', 0), 2), round(data2.get('y', 0), 2)])
|
| 175 |
]
|
| 176 |
],
|
|
|
|
| 177 |
inputs=[api_rgb_image_path, api_measurements_path, example_speed, example_pos],
|
| 178 |
label="اختر سيناريو اختبار",
|
| 179 |
+
headers=["صورة الكاميرا", "ملف القياسات", "السرعة (m/s)", "الإحداثيات"]
|
| 180 |
)
|
| 181 |
else:
|
| 182 |
gr.Markdown("لم يتم العثور على مجلد الأمثلة (`examples`) أو محتوياته.")
|
| 183 |
|
|
|
|
| 184 |
# -- العمود الأيمن: المخرجات --
|
| 185 |
+
with gr.Column(scale=3): # <-- توزيع وزن للأعمدة
|
| 186 |
with gr.Group():
|
| 187 |
+
gr.Markdown("### 📊 النتائج من الخادم")
|
| 188 |
+
api_output_image = gr.Image(label="لوحة التحكم المرئية (من الـ API)", type="pil", interactive=False, height=600)
|
| 189 |
api_control_json = gr.JSON(label="أوامر التحكم (من الـ API)")
|
| 190 |
|
| 191 |
# --- ربط منطق الواجهة ---
|
|
|
|
| 192 |
start_session_button.click(
|
| 193 |
fn=start_new_session,
|
| 194 |
inputs=None,
|
|
|
|
| 206 |
inputs=[session_id_state, api_rgb_image_path, api_measurements_path, api_target_point_list],
|
| 207 |
outputs=[api_output_image, api_control_json],
|
| 208 |
)
|
| 209 |
+
|
| 210 |
if __name__ == "__main__":
|
| 211 |
demo.queue().launch(debug=True)
|