Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import json
|
| 3 |
import gradio as gr
|
|
@@ -12,95 +14,205 @@ import requests
|
|
| 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
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
# ==============================================================================
|
| 37 |
-
# 3.
|
| 38 |
# ==============================================================================
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
|
|
|
| 73 |
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
|
|
|
|
|
|
| 78 |
|
|
|
|
|
|
|
| 79 |
with gr.Row():
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py - النسخة النهائية باستخدام gr.Dataset
|
| 2 |
+
|
| 3 |
import os
|
| 4 |
import json
|
| 5 |
import gradio as gr
|
|
|
|
| 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. الدوال المساعدة والدوال التي تتصل بالـ API
|
| 23 |
# ==============================================================================
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
+
def image_to_base64(pil_image):
|
| 26 |
+
buffered = io.BytesIO()
|
| 27 |
+
pil_image.save(buffered, format="JPEG")
|
| 28 |
+
return base64.b64encode(buffered.getvalue()).decode('utf-8')
|
| 29 |
+
|
| 30 |
+
def base64_to_image(b64_string):
|
| 31 |
+
img_data = base64.b64decode(b64_string)
|
| 32 |
+
return Image.open(io.BytesIO(img_data))
|
| 33 |
+
|
| 34 |
+
def start_new_session():
|
| 35 |
+
print("Requesting to start a new session...")
|
| 36 |
+
try:
|
| 37 |
+
response = requests.post(API_START_SESSION_URL, timeout=30)
|
| 38 |
+
response.raise_for_status()
|
| 39 |
+
session_data = response.json()
|
| 40 |
+
session_id = session_data.get("session_id")
|
| 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 |
+
FULL_EXAMPLES_DATA = []
|
| 110 |
+
# قائمة تحتوي فقط على البيانات التي سيتم عرضها في الجدول
|
| 111 |
+
SAMPLES_FOR_DISPLAY = []
|
| 112 |
+
|
| 113 |
+
if os.path.isdir(EXAMPLES_DIR):
|
| 114 |
+
try:
|
| 115 |
+
for i in range(1, 4):
|
| 116 |
+
sample_name = f"sample{i}"
|
| 117 |
+
img_path = os.path.join(EXAMPLES_DIR, sample_name, "rgb.jpg")
|
| 118 |
+
json_path = os.path.join(EXAMPLES_DIR, sample_name, "measurements.json")
|
| 119 |
+
|
| 120 |
+
with open(json_path, 'r') as f:
|
| 121 |
+
data = json.load(f)
|
| 122 |
+
|
| 123 |
+
# إضافة البيانات الكاملة
|
| 124 |
+
FULL_EXAMPLES_DATA.append({
|
| 125 |
+
"img_path": img_path,
|
| 126 |
+
"json_path": json_path
|
| 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. تعريف واجهة Gradio
|
| 144 |
# ==============================================================================
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
|
| 146 |
+
# لم نعد بحاجة إلى CSS معقد
|
| 147 |
+
CUSTOM_CSS = ".gradio-container {max-width: 100% !important;}"
|
| 148 |
|
| 149 |
+
with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue", secondary_hue="sky"), css=CUSTOM_CSS) as demo:
|
| 150 |
+
session_id_state = gr.State(value=None)
|
| 151 |
+
|
| 152 |
+
gr.Markdown("# 🚗 Baseer Self-Driving System Dashboard")
|
| 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 |
+
start_session_button = gr.Button("🟢 Start New Session", variant="primary")
|
| 160 |
+
end_session_button = gr.Button("🔴 End Current Session")
|
| 161 |
+
status_textbox = gr.Textbox(label="Session Status", interactive=False, value="Waiting to start session...")
|
| 162 |
+
|
| 163 |
+
gr.Markdown("")
|
| 164 |
+
|
| 165 |
+
with gr.Group(visible=False) as main_controls_group:
|
| 166 |
+
with gr.Row(equal_height=False):
|
| 167 |
+
with gr.Column(scale=2):
|
| 168 |
+
with gr.Group():
|
| 169 |
+
gr.Markdown("### 🗂️ Upload Scenario Files")
|
| 170 |
+
api_rgb_image_path = gr.Image(type="filepath", label="(RGB Image)")
|
| 171 |
+
api_measurements_path = gr.File(label="(Measurements JSON)", type="filepath")
|
| 172 |
+
api_run_button = gr.Button("🚀 Send Data for Processing", variant="primary")
|
| 173 |
+
|
| 174 |
+
with gr.Group():
|
| 175 |
+
gr.Markdown("### ✨ Preloaded Examples")
|
| 176 |
+
if SAMPLES_FOR_DISPLAY:
|
| 177 |
+
def on_example_select(evt: gr.SelectData):
|
| 178 |
+
selected_example = FULL_EXAMPLES_DATA[evt.index]
|
| 179 |
+
return gr.update(value=selected_example["img_path"]), gr.update(value=selected_example["json_path"])
|
| 180 |
+
|
| 181 |
+
with gr.Column(visible=False):
|
| 182 |
+
example_image = gr.Image(label="Image", type="filepath")
|
| 183 |
+
example_speed = gr.Textbox(label="Speed")
|
| 184 |
+
example_theta = gr.Textbox(label="Direction (theta)")
|
| 185 |
+
example_position = gr.Textbox(label="Position")
|
| 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)
|