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