""" Entry point for DescribePDF Hugging Face Space. """ import gradio as gr import os import sys # Asegurarnos de que el directorio actual esté en el path sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) # Importar solo las partes necesarias from describepdf import config, core # Definir tema theme = gr.themes.Soft( primary_hue="red", secondary_hue="rose", spacing_size="lg", ) # Variables globales para configuración predeterminada SUGGESTED_VLMS = [ "qwen/qwen2.5-vl-72b-instruct", "google/gemini-2.5-pro-preview-03-25", "openai/chatgpt-4o-latest" ] SUGGESTED_LLMS = [ "google/gemini-2.5-flash-preview", "openai/chatgpt-4o-latest", "anthropic/claude-3.5-sonnet" ] SUGGESTED_LANGUAGES = [ "English", "Spanish", "French", "German", "Chinese", "Japanese", "Italian", "Portuguese", "Russian", "Korean" ] def generate( pdf_file_obj, ui_api_key, ui_vlm_model, ui_lang, ui_use_md, ui_use_sum, ui_sum_model, progress=gr.Progress() ): """Wrapper function to call the core conversion process""" if pdf_file_obj is None: return "Please upload a PDF file.", gr.update(value=None, visible=False), None # Load environment config env_config = config.get_config() # Prepare configuration for this run api_key = ui_api_key.strip() if ui_api_key.strip() else env_config.get("openrouter_api_key") current_run_config = { "provider": "openrouter", "openrouter_api_key": api_key, "vlm_model": ui_vlm_model, "output_language": ui_lang, "use_markitdown": ui_use_md, "use_summary": ui_use_sum, "summary_llm_model": ui_sum_model if ui_sum_model else env_config.get("or_summary_model") } # Validate API key if not current_run_config.get("openrouter_api_key"): error_msg = "Error: OpenRouter API Key is missing. Provide it in the UI." return error_msg, gr.update(value=None, visible=False), None # Create progress callback for Gradio def progress_callback(progress_value, status): clamped_progress = max(0.0, min(1.0, progress_value)) progress(clamped_progress, desc=status) # Run the conversion status_message, result_markdown = core.convert_pdf_to_markdown( pdf_file_obj.name, current_run_config, progress_callback ) # Handle the download file if result_markdown: try: import tempfile import secrets # Get base filename from the uploaded PDF base_name = os.path.splitext(os.path.basename(pdf_file_obj.name))[0] download_filename = f"{base_name}_description.md" # Create a temporary file random_suffix = secrets.token_hex(4) temp_dir = tempfile.gettempdir() download_filepath = os.path.join(temp_dir, f"{base_name}_{random_suffix}.md") # Write markdown result to the temporary file with open(download_filepath, "w", encoding="utf-8") as md_file: md_file.write(result_markdown) download_button_update = gr.update(value=download_filepath, visible=True, label=f"Download '{download_filename}'") except Exception as e: status_message += f" (Error creating download file: {str(e)})" download_button_update = gr.update(value=None, visible=False) else: download_button_update = gr.update(value=None, visible=False) return status_message, download_button_update, result_markdown # Crear interfaz de usuario manualmente en lugar de usar la función create_ui() with gr.Blocks(title="DescribePDF", theme=theme) as app: gr.Markdown("