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