Spaces:
Sleeping
Sleeping
Update app.py
Browse files15 unmodified lines161718192021222324252660 unmodified lines8788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012115 unmodified lines# 2. Define the core data and plotting logicdef analyze_and_plot(uploaded_file, user_question):if uploaded_file is None:return "⚠️ Please upload a CSV file first.", Nonetry:# Clear any previous figures to avoid overlapping graphsplt.close('all')# Read the uploaded CSV file using Pandasdf = pd.read_csv(uploaded_file.name)60 unmodified lineschart_path = "output_chart.png"except Exception as e:text_response += f"\n\n*(Note: There was a background error rendering the chart: {str(e)})*"return text_response, chart_pathexcept Exception as e:return f"❌ An error occurred processing the file: {str(e)}", None# 3. Design the custom layout using Gradio Blockswith gr.Blocks(theme=gr.themes.Soft()) as demo:gr.Markdown("# 📊 Visual Cloud AI Data Analyst")gr.Markdown("Upload a CSV file, ask a question, or request an advanced statistical regression modeling.")with gr.Row():with gr.Column(scale=1):file_input = gr.File(label="Step 1: Upload CSV Dataset", file_types=[".csv"])question_input = gr.Textbox(label="Step 2: Ask a question or request a model",placeholder="e.g., 'Run a linear regression between ColumnA and ColumnB and show statistics'...")submit_btn = gr.Button("Analyze & Generate Chart", variant="primary")with gr.Column(scale=1):output_text = gr.Markdown(label="AI Written Insights")output_image = gr.Image(label="Generated Data Visualization", type="filepath")# Connect the inputs to the function when clicking the buttonsubmit_btn.click(fn=analyze_and_plot,inputs=[file_input, question_input],outputs=[output_text, output_image])# 4. Launch the public cloud server15 unmodified lines1617181920212223242526272829303160 unmodified lines929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413515 unmodified lines# 2. Define the core data and plotting logicdef analyze_and_plot(uploaded_file, user_question):if uploaded_file is None:# Return error message, hide chart, disable download buttonreturn "⚠️ Please upload a CSV file first.", None, gr.update(visible=False)try:# Clear any previous figures and delete old reports to keep clean statesplt.close('all')if os.path.exists("analysis_report.txt"):os.remove("analysis_report.txt")if os.path.exists("output_chart.png"):os.remove("output_chart.png")# Read the uploaded CSV file using Pandasdf = pd.read_csv(uploaded_file.name)60 unmodified lineschart_path = "output_chart.png"except Exception as e:text_response += f"\n\n*(Note: There was a background error rendering the chart: {str(e)})*"# Save the finalized text analysis into a downloadable file on the serverwith open("analysis_report.txt", "w", encoding="utf-8") as f:f.write(text_response)# Return text, chart path, and make the download button visible with the report file attachedreturn text_response, chart_path, gr.update(value="analysis_report.txt", visible=True)except Exception as e:return f"❌ An error occurred processing the file: {str(e)}", None, gr.update(visible=False)# 3. Design the custom layout using Gradio Blockswith gr.Blocks(theme=gr.themes.Soft()) as demo:gr.Markdown("# 📊 Visual Cloud AI Data Analyst")gr.Markdown("Upload a CSV file, ask a question, and download your final text reports easily.")with gr.Row():with gr.Column(scale=1):file_input = gr.File(label="Step 1: Upload CSV Dataset", file_types=[".csv"])question_input = gr.Textbox(label="Step 2: Ask a question or request a model",placeholder="e.g., 'Run a linear regression between ColumnA and ColumnB'...")submit_btn = gr.Button("Analyze & Generate Chart", variant="primary")with gr.Column(scale=1):output_text = gr.Markdown(label="AI Written Insights")# The download button is hidden until an analysis runs successfullydownload_btn = gr.DownloadButton("📥 Download Text Report", visible=False)output_image = gr.Image(label="Generated Data Visualization", type="filepath")# Connect inputs to the execution function and distribute the 3 outputssubmit_btn.click(fn=analyze_and_plot,inputs=[file_input, question_input],outputs=[output_text, output_image, download_btn])# 4. Launch the public cloud server
app.py+23-9