File size: 4,696 Bytes
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
import os
import gradio as gr
import tempfile
from dotenv import find_dotenv, load_dotenv

# Load environment variables
load_dotenv(find_dotenv())

# 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:
        if file is not None:
            uploaded_file_path = file
            filename = os.path.basename(file)
            return f"βœ… File uploaded successfully: {filename}"
        else:
            uploaded_file_path = None
            return "❌ No file uploaded"
    except Exception as e:
        uploaded_file_path = None
        return f"❌ Upload error: {str(e)}"

def simple_file_analysis(user_question):
    """
    Simple file analysis without complex schemas - for debugging
    """
    global uploaded_file_path
    
    try:
        if not uploaded_file_path or not os.path.exists(uploaded_file_path):
            return "❌ No file uploaded or file not found. Please upload a file first."
        
        if not user_question or user_question.strip() == "":
            user_question = "Provide basic analysis of this file."
        
        # Basic file info without complex processing
        file_size = os.path.getsize(uploaded_file_path)
        filename = os.path.basename(uploaded_file_path)
        
        # Read first few lines
        try:
            with open(uploaded_file_path, 'r', encoding='utf-8') as f:
                first_lines = []
                for i, line in enumerate(f):
                    if i >= 5:
                        break
                    first_lines.append(line.rstrip())
        except Exception as e:
            first_lines = [f"Error reading file: {str(e)}"]
        
        result = f"""
πŸ“Š Basic File Analysis Results:

πŸ“ File Information:
- Name: {filename}
- Size: {file_size} bytes
- Path: {uploaded_file_path}

❓ Your Question: {user_question}

πŸ“ First 5 lines:
{chr(10).join(first_lines)}

βœ… This is a debug version to test Gradio deployment.
If you see this, the basic Gradio functionality works.
"""
        return result
        
    except Exception as e:
        return f"❌ Error in analysis: {str(e)}"

# Create a minimal Gradio interface for debugging
with gr.Blocks(title="DataForge Debug - Basic File Analysis") as demo:
    gr.Markdown("# πŸ” DataForge Debug Version")
    gr.Markdown("""
    **This is a debug version to test Gradio deployment issues.**
    
    This version removes complex Pydantic models and LangGraph processing 
    to isolate the schema generation issue in Hugging Face Spaces.
    """)
    
    with gr.Row():
        with gr.Column(scale=1):
            gr.Markdown("### πŸ“€ File Upload")
            file_upload = gr.File(
                label="Upload File for Basic 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=3,
                value=""
            )
            
            analyze_btn = gr.Button("πŸ” Run Basic Analysis", variant="primary")
        
        with gr.Column(scale=2):
            analysis_output = gr.Textbox(
                label="πŸ“Š Basic Analysis Results",
                lines=20,
                placeholder="Upload a file and click 'Run Basic 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=simple_file_analysis,
        inputs=[user_question],
        outputs=[analysis_output]
    )
    
    gr.Markdown("---")
    gr.Markdown("""
    ## 🐞 Debug Information
    
    **Purpose**: This debug version helps identify if the issue is related to:
    - βœ… Basic Gradio functionality (this should work)
    - ❌ Complex Pydantic models used in the full version
    - ❌ LangGraph state management
    - ❌ Version conflicts between local and HF Spaces
    
    **Next Steps**: 
    1. If this version works on HF Spaces, the issue is in complex schema handling
    2. If this fails too, it's a basic Gradio/environment issue
    """)

if __name__ == "__main__":
    print("Starting DataForge Debug application...")
    demo.launch()