Spaces:
Running
Running
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 |