| import gradio as gr |
| import os |
| import shutil |
| import zipfile |
| import tempfile |
| import time |
|
|
| |
| from process_all_apartments import process_all, DEFAULT_PROMPT |
|
|
| def process_zip_upload(zip_file, api_key, speed_mode): |
| if zip_file is None: |
| return None, "Please upload a ZIP file." |
| |
| |
| temp_dir = tempfile.mkdtemp(prefix="hf_apartments_") |
| |
| try: |
| |
| extract_dir = os.path.join(temp_dir, "extracted") |
| os.makedirs(extract_dir, exist_ok=True) |
| |
| with zipfile.ZipFile(zip_file.name, 'r') as zip_ref: |
| zip_ref.extractall(extract_dir) |
| |
| |
| |
| |
| |
| |
| |
| |
| target_dir = extract_dir |
| |
| items = os.listdir(extract_dir) |
| if len(items) == 1 and os.path.isdir(os.path.join(extract_dir, items[0])): |
| target_dir = os.path.join(extract_dir, items[0]) |
| |
| |
| if speed_mode == 'super-fast': |
| max_size, fps = 360, 0.5 |
| elif speed_mode == 'fast': |
| max_size, fps = 480, 1.0 |
| else: |
| max_size, fps = 720, 2.0 |
| |
| api_key = api_key.strip() if api_key else None |
| |
| |
| process_all( |
| base_dir=target_dir, |
| prompt=DEFAULT_PROMPT, |
| gemini_api_key=api_key, |
| max_size=max_size, |
| fps=fps, |
| force=True |
| ) |
| |
| |
| output_zip_path = os.path.join(temp_dir, "Processed_Apartments_Reports.zip") |
| shutil.make_archive(output_zip_path.replace('.zip', ''), 'zip', target_dir) |
| |
| return output_zip_path, f"Successfully processed in '{speed_mode}' mode! Download your reports below." |
| |
| except Exception as e: |
| import traceback |
| error_msg = f"Error during processing: {str(e)}\n\n{traceback.format_exc()}" |
| return None, error_msg |
|
|
| |
| with gr.Blocks(title="Apartment Damage Detection & Inspection") as demo: |
| gr.Markdown("# ๐ข Automated Apartment Video Inspector") |
| gr.Markdown("Upload a ZIP file containing your apartment folders (e.g., `Apartment_01`, `Apartment_02`). Each folder should contain a `Before` and `After` folder with the respective `.mp4` videos.") |
| |
| with gr.Row(): |
| with gr.Column(): |
| zip_input = gr.File(label="Upload ZIP file of Apartments", file_types=[".zip"]) |
| api_key_input = gr.Textbox(label="Gemini API Key (Optional but recommended)", type="password", placeholder="AI Studio Key for damage inspection...") |
| speed_input = gr.Radio(choices=["super-fast", "fast", "normal"], value="fast", label="Processing Speed Mode") |
| process_btn = gr.Button("๐ Process Videos", variant="primary") |
| |
| with gr.Column(): |
| status_output = gr.Textbox(label="Status Log", lines=10) |
| zip_output = gr.File(label="Download Processed Reports") |
| |
| process_btn.click( |
| fn=process_zip_upload, |
| inputs=[zip_input, api_key_input, speed_input], |
| outputs=[zip_output, status_output] |
| ) |
|
|
| if __name__ == "__main__": |
| demo.launch(server_name="0.0.0.0", server_port=7860) |
|
|