File size: 6,633 Bytes
b4ea9c6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
230ff5f
b4ea9c6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
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("o3-2025-04-16", 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