import os import gradio as gr import asyncio import tempfile import traceback from dotenv import find_dotenv, load_dotenv # Load environment variables load_dotenv(find_dotenv()) print("🔍 DEBUG: Starting DataForge with debug information") print(f"🔍 DEBUG: Python version: {os.sys.version}") print(f"🔍 DEBUG: Working directory: {os.getcwd()}") # Import with debug info print("🔍 DEBUG: Importing langchain...") try: from langchain.chat_models import init_chat_model print("✅ DEBUG: langchain imported successfully") except Exception as e: print(f"❌ DEBUG: langchain import failed: {e}") traceback.print_exc() print("🔍 DEBUG: Importing agent...") try: from agent import FileInjectedPyodideSandbox, create_pyodide_eval_fn, create_codeact print("✅ DEBUG: agent imported successfully") except Exception as e: print(f"❌ DEBUG: agent import failed: {e}") traceback.print_exc() print("🔍 DEBUG: Importing graph...") try: from graph import analyze_file_with_guidance_sync, guided_analysis_graph print("✅ DEBUG: graph imported successfully") except Exception as e: print(f"❌ DEBUG: graph import failed: {e}") traceback.print_exc() # Initialize model with debug info print("🔍 DEBUG: Initializing model...") try: codeact_model = init_chat_model("gpt-4.1-2025-04-14", model_provider="openai") print("✅ DEBUG: Model initialized successfully") except Exception as e: print(f"❌ DEBUG: Model initialization failed: {e}") traceback.print_exc() # Store uploaded file path globally uploaded_file_path = None def handle_file_upload(file): """Handle file upload and store the path globally""" global uploaded_file_path try: print(f"🔍 DEBUG: File upload called with: {file}") if file is not None: uploaded_file_path = file filename = os.path.basename(file) print(f"✅ DEBUG: File uploaded: {filename}") return f"✅ File uploaded successfully: {filename}" else: uploaded_file_path = None print("❌ DEBUG: No file provided") return "❌ No file uploaded" except Exception as e: uploaded_file_path = None print(f"❌ DEBUG: Upload error: {e}") traceback.print_exc() return f"❌ Upload error: {str(e)}" def analyze_file_with_question(user_question): """ Analyze the uploaded file using the guided approach with debug info """ global uploaded_file_path try: print(f"🔍 DEBUG: Analysis called with question: {user_question}") print(f"🔍 DEBUG: Uploaded file path: {uploaded_file_path}") if not uploaded_file_path or not os.path.exists(uploaded_file_path): print("❌ DEBUG: No file available for analysis") return "❌ No file uploaded or file not found. Please upload a file first." if not user_question or user_question.strip() == "": user_question = "Provide a comprehensive analysis of this file including security, performance, and data insights." print("🔍 DEBUG: Calling analyze_file_with_guidance_sync...") result = analyze_file_with_guidance_sync(uploaded_file_path, user_question) print(f"✅ DEBUG: Analysis completed, result length: {len(result) if result else 0}") return result except Exception as e: print(f"❌ DEBUG: Analysis error: {e}") traceback.print_exc() return f"❌ Error in guided analysis: {str(e)}\n\nFull traceback:\n{traceback.format_exc()}" print("🔍 DEBUG: Creating Gradio interface...") # Create the Gradio interface with debug wrapper try: with gr.Blocks(title="DataForge - AI-Powered File Analysis (Debug Mode)") as demo: gr.Markdown("# 🔍 DataForge - AI-Powered File Analysis (Debug Mode)") gr.Markdown(""" **This is a debug version to help identify Hugging Face Spaces deployment issues.** Upload any file and ask specific questions for targeted AI analysis. """) with gr.Row(): with gr.Column(scale=1): gr.Markdown("### 📤 File Upload") file_upload = gr.File( label="Upload File for Analysis", type="filepath" ) upload_status = gr.Textbox( label="Upload Status", value="No file uploaded", interactive=False ) gr.Markdown("### ❓ Ask Your Question") user_question = gr.Textbox( label="Your Question about the File", placeholder="What would you like to know about this file?", lines=4, value="" ) analyze_btn = gr.Button("🔍 Run Analysis (Debug)", variant="primary", size="lg") with gr.Column(scale=2): analysis_output = gr.Textbox( label="📊 Analysis Results (Debug Mode)", lines=25, max_lines=35, placeholder="Upload a file, type your question, and click 'Run Analysis' to see results...", interactive=False ) # Event handlers file_upload.change( fn=handle_file_upload, inputs=[file_upload], outputs=[upload_status] ) analyze_btn.click( fn=analyze_file_with_question, inputs=[user_question], outputs=[analysis_output] ) gr.Markdown("---") gr.Markdown("## 🐞 Debug Information") gr.Markdown(""" This debug version includes: - Detailed import and initialization logging - Exception tracebacks - Step-by-step execution tracking - Version and environment information Check the console/logs for detailed debug output. """) print("✅ DEBUG: Gradio interface created successfully") except Exception as e: print(f"❌ DEBUG: Gradio interface creation failed: {e}") traceback.print_exc() raise if __name__ == "__main__": print("🔍 DEBUG: Starting application launch...") try: demo.launch() print("✅ DEBUG: Application launched successfully") except Exception as e: print(f"❌ DEBUG: Application launch failed: {e}") traceback.print_exc() raise