mohammed-aljafry commited on
Commit
ef36b58
·
verified ·
1 Parent(s): 46fdcef

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -46
app.py CHANGED
@@ -1,4 +1,4 @@
1
- # app.py
2
 
3
  import os
4
  import json
@@ -42,7 +42,6 @@ def start_new_session():
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)
@@ -61,7 +60,6 @@ def handle_end_session(session_id):
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)
@@ -70,7 +68,8 @@ def handle_end_session(session_id):
70
  print(error_message)
71
  raise gr.Error(error_message)
72
 
73
- def run_single_frame_via_api(session_id, rgb_image_path, measurements_path, target_point_list):
 
74
  if not session_id:
75
  raise gr.Error("لا توجد جلسة نشطة. يرجى بدء جلسة جديدة أولاً.")
76
  if not (rgb_image_path and measurements_path):
@@ -80,16 +79,20 @@ def run_single_frame_via_api(session_id, rgb_image_path, measurements_path, targ
80
  with open(measurements_path, 'r') as f:
81
  measurements_dict = json.load(f)
82
  image_b64 = image_to_base64(rgb_image_pil)
 
 
 
83
  payload = {
84
  "session_id": session_id,
85
  "image_b64": image_b64,
86
  "measurements": {
87
- "pos_global": [measurements_dict.get('x', 0.0), measurements_dict.get('y', 0.0)],
88
  "theta": measurements_dict.get('theta', 0.0),
89
- "speed": measurements_dict.get('speed', 5.0),
90
- "target_point": target_point_list
91
  }
92
  }
 
93
  print(f"Sending run_step request for session: {session_id}")
94
  response = requests.post(API_RUN_STEP_URL, json=payload, timeout=60)
95
  response.raise_for_status()
@@ -115,7 +118,7 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue", secondary_hue="sky"), cs
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("## ⚙️ التحكم بالجلسة")
@@ -124,64 +127,40 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue", secondary_hue="sky"), cs
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
- # التحقق من وجود مجلد الأمثلة لتجنب الأخطاء
145
- if os.path.isdir(EXAMPLES_DIR) and \
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)
152
-
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
  )
180
  else:
181
- gr.Markdown("لم يتم العثور على مجلد الأمثلة (`examples`) أو محتوياته.")
182
 
183
  # -- العمود الأيمن: المخرجات --
184
- with gr.Column(scale=3): # <-- توزيع وزن للأعمدة
185
  with gr.Group():
186
  gr.Markdown("### 📊 النتائج من الخادم")
187
  api_output_image = gr.Image(label="لوحة التحكم المرئية (من الـ API)", type="pil", interactive=False, height=600)
@@ -200,9 +179,10 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue", secondary_hue="sky"), cs
200
  outputs=[status_textbox, session_id_state, main_controls_group]
201
  )
202
 
 
203
  api_run_button.click(
204
  fn=run_single_frame_via_api,
205
- inputs=[session_id_state, api_rgb_image_path, api_measurements_path, api_target_point_list],
206
  outputs=[api_output_image, api_control_json],
207
  )
208
 
 
1
+ # app.py - الكود النهائي المعدل ليتوافق مع بنية measurements.json الحقيقية
2
 
3
  import os
4
  import json
 
42
  if not session_id:
43
  raise gr.Error("لم يتمكن الخادم من بدء جلسة جديدة.")
44
 
 
45
  status_message = f"✅ تم بدء جلسة جديدة بنجاح. أنت الآن متصل بالخادم."
46
  print(f"Session started successfully: {session_id}")
47
  return session_id, status_message, gr.update(visible=True)
 
60
  response = requests.post(f"{API_END_SESSION_URL}?session_id={session_id}", timeout=30)
61
  response.raise_for_status()
62
 
 
63
  status_message = f"🔌 تم إنهاء الجلسة بنجاح. انقطع الاتصال بالخادم."
64
  print(status_message)
65
  return status_message, None, gr.update(visible=False)
 
68
  print(error_message)
69
  raise gr.Error(error_message)
70
 
71
+ # --- [تم تعديل هذه الدالة] ---
72
+ def run_single_frame_via_api(session_id, rgb_image_path, measurements_path):
73
  if not session_id:
74
  raise gr.Error("لا توجد جلسة نشطة. يرجى بدء جلسة جديدة أولاً.")
75
  if not (rgb_image_path and measurements_path):
 
79
  with open(measurements_path, 'r') as f:
80
  measurements_dict = json.load(f)
81
  image_b64 = image_to_base64(rgb_image_pil)
82
+
83
+ # --- [التعديل الرئيسي هنا] ---
84
+ # بناء الحمولة (Payload) بناءً على بنية الملف الحقيقية
85
  payload = {
86
  "session_id": session_id,
87
  "image_b64": image_b64,
88
  "measurements": {
89
+ "pos_global": measurements_dict.get('pos_global', [0.0, 0.0]),
90
  "theta": measurements_dict.get('theta', 0.0),
91
+ "speed": measurements_dict.get('speed', 0.0),
92
+ "target_point": measurements_dict.get('target_point', [0.0, 100.0])
93
  }
94
  }
95
+
96
  print(f"Sending run_step request for session: {session_id}")
97
  response = requests.post(API_RUN_STEP_URL, json=payload, timeout=60)
98
  response.raise_for_status()
 
118
  gr.Markdown("# 🚗 واجهة التحكم لنظام Baseer للقيادة الذاتية")
119
  gr.Markdown("هذه الواجهة تعمل كـ **عميل (Client)** يتصل بـ **[Baseer Self-Driving API](https://huggingface.co/spaces/BaseerAI/baseer-server)** لمعالجة البيانات.")
120
 
121
+ gr.Markdown("---")
122
 
123
  with gr.Group():
124
  gr.Markdown("## ⚙️ التحكم بالجلسة")
 
127
  end_session_button = gr.Button("🔴 إنهاء الجلسة الحالية")
128
  status_textbox = gr.Textbox(label="حالة الجلسة", interactive=False, value="في انتظار بدء الجلسة...")
129
 
130
+ gr.Markdown("")
131
 
132
  with gr.Group(visible=False) as main_controls_group:
133
  with gr.Row():
134
  # -- العمود الأيسر: الإعدادات والمدخلات --
135
+ with gr.Column(scale=2):
136
  with gr.Group():
137
  gr.Markdown("### 🗂️ ارفع ملفات السيناريو")
138
  api_rgb_image_path = gr.Image(type="filepath", label="صورة الكاميرا الأمامية (RGB)")
139
  api_measurements_path = gr.File(label="ملف القياسات (JSON)", type="filepath")
140
+
141
+ # --- [تم حذف gr.JSON من هنا] ---
142
+
143
  api_run_button = gr.Button("🚀 أرسل البيانات للمعالجة", variant="primary")
144
 
145
+ gr.Markdown("")
146
 
147
  with gr.Group():
148
  gr.Markdown("### ✨ أمثلة جاهزة")
149
+ # تم تبسيط هذا الجزء ليعمل مع المدخلات الجديدة
150
+ if os.path.isdir(EXAMPLES_DIR):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
151
  gr.Examples(
152
  examples=[
153
+ [os.path.join(EXAMPLES_DIR, "sample1", "rgb.jpg"), os.path.join(EXAMPLES_DIR, "sample1", "measurements.json")],
154
+ [os.path.join(EXAMPLES_DIR, "sample2", "rgb.jpg"), os.path.join(EXAMPLES_DIR, "sample2", "measurements.json")]
 
 
 
 
 
 
 
 
 
 
155
  ],
156
+ inputs=[api_rgb_image_path, api_measurements_path],
157
  label="اختر سيناريو اختبار",
158
  )
159
  else:
160
+ gr.Markdown("لم يتم العثور على مجلد الأمثلة (`examples`).")
161
 
162
  # -- العمود الأيمن: المخرجات --
163
+ with gr.Column(scale=3):
164
  with gr.Group():
165
  gr.Markdown("### 📊 النتائج من الخادم")
166
  api_output_image = gr.Image(label="لوحة التحكم المرئية (من الـ API)", type="pil", interactive=False, height=600)
 
179
  outputs=[status_textbox, session_id_state, main_controls_group]
180
  )
181
 
182
+ # --- [تم تعديل هذا الجزء] ---
183
  api_run_button.click(
184
  fn=run_single_frame_via_api,
185
+ inputs=[session_id_state, api_rgb_image_path, api_measurements_path], # تم حذف target_point_list
186
  outputs=[api_output_image, api_control_json],
187
  )
188