Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,3 @@
|
|
| 1 |
-
# app.py - النسخة النهائية باستخدام gr.Dataset
|
| 2 |
-
|
| 3 |
import os
|
| 4 |
import json
|
| 5 |
import gradio as gr
|
|
@@ -14,205 +12,95 @@ import requests
|
|
| 14 |
API_URL_BASE = "https://BaseerAI-baseer-server.hf.space"
|
| 15 |
API_START_SESSION_URL = f"{API_URL_BASE}/start_session"
|
| 16 |
API_RUN_STEP_URL = f"{API_URL_BASE}/run_step"
|
| 17 |
-
API_END_SESSION_URL = f"{API_URL_BASE}/end_session"
|
| 18 |
-
|
| 19 |
-
EXAMPLES_DIR = "examples"
|
| 20 |
|
| 21 |
# ==============================================================================
|
| 22 |
-
# 2.
|
| 23 |
# ==============================================================================
|
| 24 |
-
|
| 25 |
-
def image_to_base64(pil_image):
|
| 26 |
buffered = io.BytesIO()
|
| 27 |
-
|
| 28 |
-
return base64.b64encode(buffered.getvalue()).decode(
|
| 29 |
-
|
| 30 |
-
def
|
| 31 |
-
|
| 32 |
-
return Image.open(io.BytesIO(
|
| 33 |
-
|
| 34 |
-
def
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
if not session_id:
|
| 42 |
-
raise gr.Error("لم يتمكن الخادم من بدء جلسة جديدة.")
|
| 43 |
-
status_message = f"✅ تم بدء جلسة جديدة بنجاح. أنت الآن متصل بالخادم."
|
| 44 |
-
print(f"Session started successfully: {session_id}")
|
| 45 |
-
return session_id, status_message, gr.update(visible=True)
|
| 46 |
-
except requests.exceptions.RequestException as e:
|
| 47 |
-
error_message = f"فشل الاتصال بخادم الـ API: {e}"
|
| 48 |
-
print(error_message)
|
| 49 |
-
raise gr.Error(error_message)
|
| 50 |
-
|
| 51 |
-
def handle_end_session(session_id):
|
| 52 |
-
if not session_id:
|
| 53 |
-
return "لا توجد جلسة نشطة لإنهائها.", None, gr.update(visible=False)
|
| 54 |
-
print(f"Requesting to end session: {session_id}")
|
| 55 |
-
try:
|
| 56 |
-
response = requests.post(f"{API_END_SESSION_URL}?session_id={session_id}", timeout=30)
|
| 57 |
-
response.raise_for_status()
|
| 58 |
-
status_message = f"🔌 تم إنهاء الجلسة بنجاح. انقطع الاتصال بالخادم."
|
| 59 |
-
print(status_message)
|
| 60 |
-
return status_message, None, gr.update(visible=False)
|
| 61 |
-
except requests.exceptions.RequestException as e:
|
| 62 |
-
error_message = f"حدث خطأ أثناء إنهاء الجلسة: {e}"
|
| 63 |
-
print(error_message)
|
| 64 |
-
raise gr.Error(error_message)
|
| 65 |
-
|
| 66 |
-
def run_single_frame_via_api(session_id, rgb_image_path, measurements_path):
|
| 67 |
-
if not session_id:
|
| 68 |
-
raise gr.Error("لا توجد جلسة نشطة. يرجى بدء جلسة جديدة أولاً.")
|
| 69 |
-
if not (rgb_image_path and measurements_path):
|
| 70 |
-
raise gr.Error("الرجاء توفير الصورة الأمامية وملف القياسات على الأقل.")
|
| 71 |
-
try:
|
| 72 |
-
rgb_image_pil = Image.open(rgb_image_path).convert("RGB")
|
| 73 |
-
# التأكد من أن المسار هو ملف صالح قبل فتحه
|
| 74 |
-
if not isinstance(measurements_path, str) or not os.path.exists(measurements_path):
|
| 75 |
-
raise gr.Error(f"مسار ملف القياسات غير صالح أو مفقود: {measurements_path}")
|
| 76 |
-
with open(measurements_path, 'r') as f:
|
| 77 |
-
measurements_dict = json.load(f)
|
| 78 |
-
image_b64 = image_to_base64(rgb_image_pil)
|
| 79 |
-
payload = {
|
| 80 |
-
"session_id": session_id, "image_b64": image_b64,
|
| 81 |
-
"measurements": {
|
| 82 |
-
"pos_global": measurements_dict.get('pos_global', [0.0, 0.0]),
|
| 83 |
-
"theta": measurements_dict.get('theta', 0.0),
|
| 84 |
-
"speed": measurements_dict.get('speed', 0.0),
|
| 85 |
-
"target_point": measurements_dict.get('target_point', [0.0, 100.0])
|
| 86 |
-
}
|
| 87 |
-
}
|
| 88 |
-
print(f"Sending run_step request for session: {session_id}")
|
| 89 |
-
response = requests.post(API_RUN_STEP_URL, json=payload, timeout=60)
|
| 90 |
-
response.raise_for_status()
|
| 91 |
-
api_result = response.json()
|
| 92 |
-
dashboard_b64 = api_result.get("dashboard_b64")
|
| 93 |
-
control_commands = api_result.get("control_commands", {})
|
| 94 |
-
if not dashboard_b64:
|
| 95 |
-
raise gr.Error("لم يتم إرجاع صورة لوحة التحكم من الـ API.")
|
| 96 |
-
output_image = base64_to_image(dashboard_b64)
|
| 97 |
-
return output_image, control_commands
|
| 98 |
-
except FileNotFoundError:
|
| 99 |
-
raise gr.Error(f"لم يتم العثور على الملف: {measurements_path}")
|
| 100 |
-
except requests.exceptions.RequestException as e:
|
| 101 |
-
raise gr.Error(f"حدث خطأ أثناء الاتصال بالـ API: {e}")
|
| 102 |
-
except Exception as e:
|
| 103 |
-
raise gr.Error(f"حدث خطأ غير متوقع: {e}")
|
| 104 |
|
| 105 |
# ==============================================================================
|
| 106 |
-
# 3.
|
| 107 |
# ==============================================================================
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
# إضافة بيانات العرض فقط
|
| 130 |
-
SAMPLES_FOR_DISPLAY.append([
|
| 131 |
-
img_path,
|
| 132 |
-
f"{data.get('speed', 0.0):.2f}",
|
| 133 |
-
f"{data.get('theta', 0.0):.2f}",
|
| 134 |
-
str(data.get('pos_global', 'N/A')),
|
| 135 |
-
str(data.get('target_point', 'N/A'))
|
| 136 |
-
])
|
| 137 |
-
except FileNotFoundError:
|
| 138 |
-
print(f"تحذير: لم يتم العثور على بعض ملفات الأمثلة في مجلد '{EXAMPLES_DIR}'.")
|
| 139 |
-
FULL_EXAMPLES_DATA = []
|
| 140 |
-
SAMPLES_FOR_DISPLAY = []
|
| 141 |
|
| 142 |
# ==============================================================================
|
| 143 |
-
# 4.
|
| 144 |
# ==============================================================================
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
|
| 146 |
-
|
| 147 |
-
CUSTOM_CSS = ".gradio-container {max-width: 100% !important;}"
|
| 148 |
|
| 149 |
-
with gr.
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
gr.Markdown("This interface acts as a **client** that connects to the **[Baseer Self-Driving API](https://huggingface.co/spaces/BaseerAI/Baseer_Server)** for data processing.")
|
| 154 |
-
gr.Markdown("---")
|
| 155 |
|
| 156 |
-
with gr.Group():
|
| 157 |
-
gr.Markdown("## ⚙️ Session Control")
|
| 158 |
with gr.Row():
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
example_target = gr.Textbox(label="Target Point")
|
| 187 |
-
|
| 188 |
-
example_components = [example_image, example_speed, example_theta, example_position, example_target]
|
| 189 |
-
|
| 190 |
-
if SAMPLES_FOR_DISPLAY:
|
| 191 |
-
example_dataset = gr.Dataset(
|
| 192 |
-
components=example_components,
|
| 193 |
-
samples=SAMPLES_FOR_DISPLAY,
|
| 194 |
-
label="Select Test Scenario"
|
| 195 |
-
)
|
| 196 |
-
|
| 197 |
-
example_dataset.select(
|
| 198 |
-
fn=on_example_select,
|
| 199 |
-
inputs=None,
|
| 200 |
-
outputs=[api_rgb_image_path, api_measurements_path]
|
| 201 |
-
)
|
| 202 |
-
else:
|
| 203 |
-
gr.Markdown("No valid examples found.")
|
| 204 |
-
|
| 205 |
-
with gr.Column(scale=3):
|
| 206 |
-
with gr.Group():
|
| 207 |
-
gr.Markdown("### 📊 Results from API")
|
| 208 |
-
api_output_image = gr.Image(label="Dashboard Image (from API)", type="pil", interactive=False, height=600)
|
| 209 |
-
api_control_json = gr.JSON(label="Control Commands (from API)")
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
start_session_button.click(fn=start_new_session, inputs=None, outputs=[session_id_state, status_textbox, main_controls_group])
|
| 213 |
-
end_session_button.click(fn=handle_end_session, inputs=[session_id_state], outputs=[status_textbox, session_id_state, main_controls_group])
|
| 214 |
-
api_run_button.click(fn=run_single_frame_via_api, inputs=[session_id_state, api_rgb_image_path, api_measurements_path], outputs=[api_output_image, api_control_json])
|
| 215 |
-
|
| 216 |
-
if __name__ == "__main__":
|
| 217 |
-
# قم بإعادة تشغيل الـ Space بعد لصق هذا الكود
|
| 218 |
-
demo.queue().launch(debug=True)
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import json
|
| 3 |
import gradio as gr
|
|
|
|
| 12 |
API_URL_BASE = "https://BaseerAI-baseer-server.hf.space"
|
| 13 |
API_START_SESSION_URL = f"{API_URL_BASE}/start_session"
|
| 14 |
API_RUN_STEP_URL = f"{API_URL_BASE}/run_step"
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
# ==============================================================================
|
| 17 |
+
# 2. الوظائف المساعدة
|
| 18 |
# ==============================================================================
|
| 19 |
+
def encode_image_to_base64(image):
|
|
|
|
| 20 |
buffered = io.BytesIO()
|
| 21 |
+
image.save(buffered, format="PNG")
|
| 22 |
+
return base64.b64encode(buffered.getvalue()).decode("utf-8")
|
| 23 |
+
|
| 24 |
+
def decode_base64_to_image(base64_string):
|
| 25 |
+
image_data = base64.b64decode(base64_string)
|
| 26 |
+
return Image.open(io.BytesIO(image_data))
|
| 27 |
+
|
| 28 |
+
def start_session(task_type):
|
| 29 |
+
response = requests.post(API_START_SESSION_URL, json={"task_type": task_type})
|
| 30 |
+
return response.json()
|
| 31 |
+
|
| 32 |
+
def run_step(session_id, step_input):
|
| 33 |
+
response = requests.post(API_RUN_STEP_URL, json={"session_id": session_id, "step_input": step_input})
|
| 34 |
+
return response.json()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
# ==============================================================================
|
| 37 |
+
# 3. تنفيذ خطوة واحدة
|
| 38 |
# ==============================================================================
|
| 39 |
+
def run_single_frame(model_from_state, rgb_image_path, rgb_left_image_path, rgb_right_image_path,
|
| 40 |
+
rgb_center_image_path, lidar_image_path, measurements_path, target_point_list):
|
| 41 |
+
|
| 42 |
+
if model_from_state is None:
|
| 43 |
+
print("API session detected or model not loaded. Loading default model...")
|
| 44 |
+
session = start_session("leaderboard")
|
| 45 |
+
model_from_state = session.get("session_id")
|
| 46 |
+
|
| 47 |
+
step_input = {
|
| 48 |
+
"rgb": encode_image_to_base64(rgb_image_path) if rgb_image_path else None,
|
| 49 |
+
"rgb_left": encode_image_to_base64(rgb_left_image_path) if rgb_left_image_path else None,
|
| 50 |
+
"rgb_right": encode_image_to_base64(rgb_right_image_path) if rgb_right_image_path else None,
|
| 51 |
+
"rgb_center": encode_image_to_base64(rgb_center_image_path) if rgb_center_image_path else None,
|
| 52 |
+
"lidar": encode_image_to_base64(lidar_image_path) if lidar_image_path else None,
|
| 53 |
+
"measurements": json.loads(measurements_path) if measurements_path else {},
|
| 54 |
+
"target_point": target_point_list
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
+
response = run_step(model_from_state, step_input)
|
| 58 |
+
control = response.get("control")
|
| 59 |
+
return control, model_from_state
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
# ==============================================================================
|
| 62 |
+
# 4. واجهة Gradio
|
| 63 |
# ==============================================================================
|
| 64 |
+
def build_ui():
|
| 65 |
+
with gr.Blocks(css="""
|
| 66 |
+
.gr-button { background-color: #0066cc; color: white; font-size: 16px; border-radius: 10px; }
|
| 67 |
+
.gr-box { border: 2px solid #0066cc; padding: 10px; border-radius: 12px; }
|
| 68 |
+
.gr-textbox label { font-weight: bold; font-size: 14px; }
|
| 69 |
+
h1 { text-align: center; color: #004080; font-family: Arial; }
|
| 70 |
+
""") as demo:
|
| 71 |
|
| 72 |
+
gr.Markdown("""<h1>واجهة المحاكاة الذكية - BaseerAI</h1>""")
|
|
|
|
| 73 |
|
| 74 |
+
with gr.Row():
|
| 75 |
+
rgb = gr.Image(label="صورة RGB (أمامية)", type="pil")
|
| 76 |
+
rgb_left = gr.Image(label="صورة RGB (يسار)", type="pil")
|
| 77 |
+
rgb_right = gr.Image(label="صورة RGB (يمين)", type="pil")
|
|
|
|
|
|
|
| 78 |
|
|
|
|
|
|
|
| 79 |
with gr.Row():
|
| 80 |
+
rgb_center = gr.Image(label="صورة RGB (وسط)", type="pil")
|
| 81 |
+
lidar = gr.Image(label="صورة Lidar", type="pil")
|
| 82 |
+
|
| 83 |
+
measurements = gr.Textbox(label="القياسات (بصيغة JSON)", placeholder='مثال: {"speed": 5.0}', lines=4)
|
| 84 |
+
target_point = gr.Textbox(label="نقطة الهدف (x, y)", placeholder="مثال: 3.2, 5.7")
|
| 85 |
+
|
| 86 |
+
run_button = gr.Button("تشغيل النموذج")
|
| 87 |
+
output = gr.Textbox(label="التحكم الناتج", lines=3)
|
| 88 |
+
|
| 89 |
+
state = gr.State()
|
| 90 |
+
|
| 91 |
+
def parse_target_point(text):
|
| 92 |
+
try:
|
| 93 |
+
x, y = map(float, text.split(","))
|
| 94 |
+
return [x, y]
|
| 95 |
+
except:
|
| 96 |
+
return [0.0, 0.0]
|
| 97 |
+
|
| 98 |
+
run_button.click(fn=run_single_frame,
|
| 99 |
+
inputs=[state, rgb, rgb_left, rgb_right, rgb_center, lidar, measurements, target_point],
|
| 100 |
+
outputs=[output, state])
|
| 101 |
+
|
| 102 |
+
return demo
|
| 103 |
+
|
| 104 |
+
if __name__ == '__main__':
|
| 105 |
+
ui = build_ui()
|
| 106 |
+
ui.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|