Spaces:
Sleeping
Sleeping
Update app.py
Browse files15 unmodified lines16171819192021222223242526272829303160 unmodified lines92939490919596979899100101102103941041051061071089910911011111211311411510611611711811912012112212312412512612711412812913013111813213313413515 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 button return "⚠️ Please upload a CSV file first.", None return "⚠️ Please upload a CSV file first.", None, gr.update(visible=False) try: # Clear any previous figures to avoid overlapping graphs # Clear any previous figures and delete old reports to keep clean states plt.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 Pandas df = pd.read_csv(uploaded_file.name)60 unmodified lines chart_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_path # Save the finalized text analysis into a downloadable file on the server with 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 attached return 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 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, or request an advanced statistical regression modeling.") 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 and show statistics'..." 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 successfully download_btn = gr.DownloadButton("📥 Download Text Report", visible=False) output_image = gr.Image(label="Generated Data Visualization", type="filepath") # Connect the inputs to the function when clicking the button # Connect inputs to the execution function and distribute the 3 outputs submit_btn.click( fn=analyze_and_plot, inputs=[file_input, question_input], outputs=[output_text, output_image] outputs=[output_text, output_image, download_btn] )
# 4. Launch the public cloud serverapp.py+23-9