Spaces:
Running on Zero
Running on Zero
Asmit Nayak commited on
Commit ·
b49a99a
1
Parent(s): 9012453
Add detailed results table and CSV download functionality
Browse files864 unmodified lines86586686786886987087187286886987056 unmodified lines92792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599610 unmodified lines1007100810099521010101110121013101420 unmodified lines103510361037979103810391040104110421 unmodified line104410451046987104710481049105010519 unmodified lines10611062106310031064106510661067106820 unmodified lines108910901091103010921093103210941095109610974 unmodified lines1102110311041043110511061107110816 unmodified lines11251126112710661067106811281129113011311132113311341135113611371138113910751140114111421143114410 unmodified lines1155115611571092115811591160116111627 unmodified lines1170117111721106117311741175117611773 unmodified lines1181118211831116118411851186118724 unmodified lines1212121312141215121612171218114711481149121912201221122212231224864 unmodified lines results_display = gr.HTML( value="<div style='text-align: center; padding: 40px; color: var(--body-text-color); opacity: 0.7;'>Enter a URL and click analyze to see results here.</div>" ) results_dataframe = gr.Dataframe( label="Detailed Results (Scroll right to see all columns)", visible=False ) with gr.Column(scale=3): # Screenshot section - only screenshot in right column56 unmodified lines interactive=False ) # Detailed results table spanning both columns (full width) results_dataframe = gr.Dataframe( label="Detailed Results (Scroll right to see all columns)", visible=False ) # Download button for results CSV download_btn = gr.DownloadButton( label="📥 Download Results as CSV", visible=False, variant="secondary" ) # Event handlers def save_results_to_csv(df, url): """Save the results dataframe to a CSV file named after the analyzed site.""" if df is None or (isinstance(df, pd.DataFrame) and df.empty): return None # Create a safe filename from the URL safe_filename = url.lower().replace("http://", "").replace("https://", "").strip().replace("www.", "") \ .replace(".", "_") \ .replace("/", "_") \ .replace("-", "_") \ .replace("=", "_") \ .replace("?", "_") \ .replace("&", "_") \ .replace("%", "_") \ .replace(":", "_") \ .replace("#", "_") \ .replace("'", "_") \ .replace('"', "_") \ .replace("*", "_") \ .replace("<", "_") \ .replace(">", "_") \ .replace("|", "_") \ .replace(" ", "_") # Trim if too long and ensure it doesn't end with underscore safe_filename = safe_filename[:100].rstrip("_") # Create final filename csv_filename = f"{safe_filename}.csv" # Create a temporary file for download temp_csv = tempfile.NamedTemporaryFile(mode='w', delete=False, suffix='.csv', prefix='analysis_') csv_path = temp_csv.name temp_csv.close() # Save dataframe to CSV if isinstance(df, pd.DataFrame): df.to_csv(csv_path, index=False) else: # If it's not a DataFrame, try to convert it pd.DataFrame(df).to_csv(csv_path, index=False) # Rename to the site-specific filename for download final_path = os.path.join(os.path.dirname(csv_path), csv_filename) shutil.copy(csv_path, final_path) os.remove(csv_path) print(f"[CONSOLE] CSV file created for download: {final_path} (filename: {csv_filename})") return final_path def handle_url_analysis(url, api_key): """Handle URL analysis with screenshot capture.""" print(f"[CONSOLE] handle_url_analysis called with URL: {url}")10 unmodified lines gr.update(visible=True), # Show placeholder initially gr.update(visible=False), # Hide screenshot initially "<div style='text-align: center; padding: 20px; color: var(--body-text-color); opacity: 0.7;'>Preparing analysis...</div>", # Clear previous errors gr.update(visible=False) # Hide dataframe gr.update(visible=False), # Hide dataframe gr.update(visible=False) # Hide download button ) analysis_generator = take_screenshot_and_process(url, api_key)20 unmodified lines gr.update(visible=False), # Hide placeholder gr.update(value=image_path, visible=True, label="📷 Original Screenshot"), # Show original screenshot "<div style='text-align: center; padding: 20px; color: var(--body-text-color); opacity: 0.7;'>Analysis in progress...</div>", # Clear previous errors gr.update(visible=False) gr.update(visible=False), gr.update(visible=False) # Hide download button ) else: yield (1 unmodified line gr.update(visible=True), # Keep placeholder visible gr.update(visible=False), # Hide screenshot "<div style='text-align: center; padding: 20px; color: var(--body-text-color); opacity: 0.7;'>Analysis in progress...</div>", # Clear previous errors gr.update(visible=False) gr.update(visible=False), gr.update(visible=False) # Hide download button ) else: print(f"[CONSOLE] Received final result with {len(dataframe_result)} rows")9 unmodified lines gr.update(visible=False), # Hide placeholder gr.update(value=final_image, visible=True, label="🎯 Annotated Screenshot (Analysis Complete)") if final_image else gr.update(visible=False), "<div style='text-align: center; padding: 20px; color: var(--body-text-color); opacity: 0.7;'>Processing results...</div>", # Clear previous errors gr.update(visible=False) gr.update(visible=False), gr.update(visible=False) # Hide download button ) break 20 unmodified lines .replace("<", "_x13x_") \ .replace(">", "_x14x_") \ .replace("|", "_x15x_")
save_url = save_url + "__" + str(uuid.uuid4()).replace("-", "_")
save_dict = { save_url: final_result }4 unmodified lines annotated_image_path = final_image if final_image else original_image print(f"[CONSOLE] Using image for dataset upload: {dataset_image_path} (original: {original_image}, final: {final_image})") print(f"[CONSOLE] Using annotated image for display: {annotated_image_path} (original: {original_image}, final: {final_image})")
if dataset_image_path and os.path.exists(dataset_image_path) and annotated_image_path and os.path.exists(annotated_image_path): try: # Load the original image using PIL16 unmodified lines print(f"[CONSOLE] Warning: Image path not found or invalid: {dataset_image_path}") image_df = pd.DataFrame([{"id": save_url, "image": None, "annotated_image": None}])
dataset_upload.update_dataset_with_new_splits(save_dict) dataset_upload.update_dataset_with_new_images(image_df, scheduler=scheduler, dataset_dir=dataset_dir, jsonl_path=jsonl_path) # dataset_upload.update_dataset_with_new_splits(save_dict) # dataset_upload.update_dataset_with_new_images(image_df, scheduler=scheduler, dataset_dir=dataset_dir, jsonl_path=jsonl_path)
# Prepare CSV for download csv_file_path = save_results_to_csv(final_result, url)
# Show final results with annotated screenshot yield ( final_status, gr.update(visible=False), # Hide placeholder gr.update(value=final_image, visible=True, label="🎯 Annotated Screenshot (Analysis Complete)") if final_image else gr.update(visible=False), results_html, gr.update(value=final_result, visible=True) gr.update(value=final_result, visible=True), gr.update(value=csv_file_path, visible=True) if csv_file_path else gr.update(visible=False) )
# Clean up temporary files after successful display10 unmodified lines gr.update(visible=True), # Show placeholder again gr.update(visible=False, label="Website Screenshot"), # Hide screenshot and reset label "<div style='color: #ef4444; text-align: center; background-color: var(--block-background-fill); padding: 15px; border-radius: 8px; border: 1px solid #ef4444; opacity: 0.9;'>Analysis failed. Please check your Gemini API key and try again.</div>", gr.update(visible=False) gr.update(visible=False), gr.update(visible=False) # Hide download button ) except Exception as e:7 unmodified lines gr.update(visible=True), # Show placeholder again gr.update(visible=False, label="Website Screenshot"), # Hide screenshot and reset label f"<div style='color: #ef4444; text-align: center; background-color: var(--block-background-fill); padding: 15px; border-radius: 8px; border: 1px solid #ef4444; opacity: 0.9;'>{error_msg}</div>", gr.update(visible=False) gr.update(visible=False), gr.update(visible=False) # Hide download button ) if e.__class__ == gr.exceptions.Error: raise e3 unmodified lines analyze_url_btn.click( fn=handle_url_analysis, inputs=[url_input, gemini_api_key], outputs=[status_text, screenshot_placeholder, screenshot_display, results_display, results_dataframe], outputs=[status_text, screenshot_placeholder, screenshot_display, results_display, results_dataframe, download_btn], show_progress="full" ) 24 unmodified lines
from py_files.utils import decrypt_system_prompts
if os.path.exists("./system_prompt.txt") and os.path.exists("./system_prompt_thinking.txt"): print(f"[CONSOLE] System prompts already decrypted, skipping decryption step") else: print(f"[CONSOLE] Decrypting system prompts...") if not decrypt_system_prompts(): print(f"[CONSOLE] Failed to decrypt system prompts, exiting...") exit(1) if not decrypt_system_prompts(): print(f"[CONSOLE] Failed to decrypt system prompts, exiting...") exit(1)
print(f"[CONSOLE] ===== STARTING GRADIO APPLICATION =====") print(f"[CONSOLE] Creating Gradio interface...")app.py+94-22