Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -16,11 +16,16 @@ client = genai.Client(api_key=api_key)
|
|
| 16 |
# 2. Define the core data and plotting logic
|
| 17 |
def analyze_and_plot(uploaded_file, user_question):
|
| 18 |
if uploaded_file is None:
|
| 19 |
-
|
|
|
|
| 20 |
|
| 21 |
try:
|
| 22 |
-
# Clear any previous figures to
|
| 23 |
plt.close('all')
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
# Read the uploaded CSV file using Pandas
|
| 26 |
df = pd.read_csv(uploaded_file.name)
|
|
@@ -87,35 +92,44 @@ def analyze_and_plot(uploaded_file, user_question):
|
|
| 87 |
chart_path = "output_chart.png"
|
| 88 |
except Exception as e:
|
| 89 |
text_response += f"\n\n*(Note: There was a background error rendering the chart: {str(e)})*"
|
| 90 |
-
|
| 91 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
|
| 93 |
except Exception as e:
|
| 94 |
-
return f"❌ An error occurred processing the file: {str(e)}", None
|
| 95 |
|
| 96 |
# 3. Design the custom layout using Gradio Blocks
|
| 97 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 98 |
gr.Markdown("# 📊 Visual Cloud AI Data Analyst")
|
| 99 |
-
gr.Markdown("Upload a CSV file, ask a question,
|
| 100 |
|
| 101 |
with gr.Row():
|
| 102 |
with gr.Column(scale=1):
|
| 103 |
file_input = gr.File(label="Step 1: Upload CSV Dataset", file_types=[".csv"])
|
| 104 |
question_input = gr.Textbox(
|
| 105 |
label="Step 2: Ask a question or request a model",
|
| 106 |
-
placeholder="e.g., 'Run a linear regression between ColumnA and ColumnB
|
| 107 |
)
|
| 108 |
submit_btn = gr.Button("Analyze & Generate Chart", variant="primary")
|
| 109 |
|
| 110 |
with gr.Column(scale=1):
|
| 111 |
output_text = gr.Markdown(label="AI Written Insights")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
output_image = gr.Image(label="Generated Data Visualization", type="filepath")
|
| 113 |
|
| 114 |
-
# Connect
|
| 115 |
submit_btn.click(
|
| 116 |
fn=analyze_and_plot,
|
| 117 |
inputs=[file_input, question_input],
|
| 118 |
-
outputs=[output_text, output_image]
|
| 119 |
)
|
| 120 |
|
| 121 |
# 4. Launch the public cloud server
|
|
|
|
| 16 |
# 2. Define the core data and plotting logic
|
| 17 |
def analyze_and_plot(uploaded_file, user_question):
|
| 18 |
if uploaded_file is None:
|
| 19 |
+
# Return error message, hide chart, disable download button
|
| 20 |
+
return "⚠️ Please upload a CSV file first.", None, gr.update(visible=False)
|
| 21 |
|
| 22 |
try:
|
| 23 |
+
# Clear any previous figures and delete old reports to keep clean states
|
| 24 |
plt.close('all')
|
| 25 |
+
if os.path.exists("analysis_report.txt"):
|
| 26 |
+
os.remove("analysis_report.txt")
|
| 27 |
+
if os.path.exists("output_chart.png"):
|
| 28 |
+
os.remove("output_chart.png")
|
| 29 |
|
| 30 |
# Read the uploaded CSV file using Pandas
|
| 31 |
df = pd.read_csv(uploaded_file.name)
|
|
|
|
| 92 |
chart_path = "output_chart.png"
|
| 93 |
except Exception as e:
|
| 94 |
text_response += f"\n\n*(Note: There was a background error rendering the chart: {str(e)})*"
|
| 95 |
+
|
| 96 |
+
# Save the finalized text analysis into a downloadable file on the server
|
| 97 |
+
with open("analysis_report.txt", "w", encoding="utf-8") as f:
|
| 98 |
+
f.write(text_response)
|
| 99 |
+
|
| 100 |
+
# Return text, chart path, and make the download button visible with the report file attached
|
| 101 |
+
return text_response, chart_path, gr.update(value="analysis_report.txt", visible=True)
|
| 102 |
|
| 103 |
except Exception as e:
|
| 104 |
+
return f"❌ An error occurred processing the file: {str(e)}", None, gr.update(visible=False)
|
| 105 |
|
| 106 |
# 3. Design the custom layout using Gradio Blocks
|
| 107 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 108 |
gr.Markdown("# 📊 Visual Cloud AI Data Analyst")
|
| 109 |
+
gr.Markdown("Upload a CSV file, ask a question, and download your final text reports easily.")
|
| 110 |
|
| 111 |
with gr.Row():
|
| 112 |
with gr.Column(scale=1):
|
| 113 |
file_input = gr.File(label="Step 1: Upload CSV Dataset", file_types=[".csv"])
|
| 114 |
question_input = gr.Textbox(
|
| 115 |
label="Step 2: Ask a question or request a model",
|
| 116 |
+
placeholder="e.g., 'Run a linear regression between ColumnA and ColumnB'..."
|
| 117 |
)
|
| 118 |
submit_btn = gr.Button("Analyze & Generate Chart", variant="primary")
|
| 119 |
|
| 120 |
with gr.Column(scale=1):
|
| 121 |
output_text = gr.Markdown(label="AI Written Insights")
|
| 122 |
+
|
| 123 |
+
# The download button is hidden until an analysis runs successfully
|
| 124 |
+
download_btn = gr.DownloadButton("📥 Download Text Report", visible=False)
|
| 125 |
+
|
| 126 |
output_image = gr.Image(label="Generated Data Visualization", type="filepath")
|
| 127 |
|
| 128 |
+
# Connect inputs to the execution function and distribute the 3 outputs
|
| 129 |
submit_btn.click(
|
| 130 |
fn=analyze_and_plot,
|
| 131 |
inputs=[file_input, question_input],
|
| 132 |
+
outputs=[output_text, output_image, download_btn]
|
| 133 |
)
|
| 134 |
|
| 135 |
# 4. Launch the public cloud server
|