DataForge / app_with_debug.py
ai-puppy
Revert "update better model"
b7e87c1
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