Spaces:
Runtime error
Runtime error
Create app.py
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170import gradio as grimport pandas as pdimport matplotlib.pyplot as pltimport seaborn as snsfrom PIL import Imageimport iofrom transformers import pipeline, set_seed
# Load local summarization model (after training)try: summarizer = pipeline("text-generation", model="trained_summary_model", tokenizer="trained_summary_model")except Exception as e: summarizer = None print("Warning: Local summarization model not loaded:", e)
# --- Local summarizer ---def summarize_with_local_model(prompt): if summarizer is None: return "Local summarizer not available. Please train the model first." try: set_seed(42) result = summarizer(prompt, max_length=100, do_sample=True, temperature=0.7)[0]["generated_text"] return result[len(prompt):].strip() # Remove prompt from generated output except Exception as e: return f"Summary failed: {str(e)}"
# --- Chart generator ---def generate_static_chart(df, chart_type, x, y=None): try: plt.figure(figsize=(10, 6)) if chart_type == "bar": if y: sns.barplot(data=df, x=x, y=y) else: df[x].value_counts().plot(kind='bar') elif chart_type == "pie": df[x].value_counts().plot.pie(autopct='%1.1f%%') elif chart_type == "line" and y: sns.lineplot(data=df, x=x, y=y) elif chart_type == "scatter" and y: sns.scatterplot(data=df, x=x, y=y) elif chart_type == "hist": sns.histplot(df[x], bins=10, kde=True) elif chart_type == "correlation heatmap": corr = df.select_dtypes(include='number').corr() sns.heatmap(corr, annot=True, cmap='coolwarm', fmt=".2f") else: return None
plt.title(f"{chart_type.capitalize()} of {x}" + (f" vs {y}" if y else "")) if y: plt.legend([y]) plt.tight_layout() buf = io.BytesIO() plt.savefig(buf, format='png') buf.seek(0) return Image.open(buf) except Exception as e: print("Chart error:", e) return None
# --- Main logic ---def visualize(csv_file, chart_type, x_col, y_col): if csv_file is None: raise gr.Error("Please upload a CSV file.") if not chart_type: raise gr.Error("Please select a chart type.")
try: df = pd.read_csv(csv_file.name) except Exception as e: return f"Error reading CSV: {e}", None, "Error loading CSV."
if x_col not in df.columns or (y_col and y_col not in df.columns): return ( f"Invalid column names. Available columns: {', '.join(df.columns)}", None, df.to_markdown() )
numeric_required = ["line", "scatter", "hist", "correlation heatmap"] if chart_type in numeric_required: if not pd.api.types.is_numeric_dtype(df[x_col]) or (y_col and not pd.api.types.is_numeric_dtype(df[y_col])): return ( f"The chart type '{chart_type}' requires numeric columns. Please select numeric columns or choose a different chart.", None, df.to_markdown() )
# Prompt sample = df[[x_col] + ([y_col] if y_col else [])].head(10).to_csv(index=False) prompt = f"Analyze this {chart_type} chart with sample data:\n{sample}\nInsights:" summary = summarize_with_local_model(prompt)
chart = generate_static_chart(df, chart_type, x_col, y_col) if chart is None: return "Chart generation failed. Try different columns.", None, df.to_markdown()
return summary, chart, df.to_markdown()
# --- Column selector helper ---def get_columns(csv_file): try: df = pd.read_csv(csv_file.name) cols = list(df.columns) has_numeric = not df.select_dtypes(include='number').empty chart_choices = ["bar", "pie"] + (["line", "scatter", "hist", "correlation heatmap"] if has_numeric else []) return ( gr.update(choices=cols, visible=True), gr.update(choices=cols, visible=True), ", ".join(cols), gr.update(choices=chart_choices) ) except Exception as e: return ( gr.update(visible=False), gr.update(visible=False), f"Error: {e}", gr.update(choices=[]) )
def reset_outputs(): return "", None, "", gr.update(visible=False), gr.update(visible=False), gr.update(choices=[])
# --- Gradio Interface ---demo = gr.Blocks()
with demo: gr.Markdown("## π Smart Data Visualizer with Local AI Summary") gr.Markdown("Upload a CSV and select columns to visualize. Local AI generates summaries.")
with gr.Row(): csv_file = gr.File(label="Upload CSV", file_types=[".csv"]) chart_type = gr.Dropdown([], label="Chart Type")
with gr.Row(): x_input = gr.Dropdown(label="X-axis Column", visible=False) y_input = gr.Dropdown(label="Y-axis Column (optional)", visible=False)
available_cols = gr.Markdown("")
with gr.Row(): summary_output = gr.Textbox(label="π AI Summary") chart_output = gr.Image(type="pil", label="π Chart")
with gr.Accordion("π Full CSV Preview", open=False): csv_preview = gr.Markdown("")
run_btn = gr.Button("Generate Visualization")
run_btn.click( fn=visualize, inputs=[csv_file, chart_type, x_input, y_input], outputs=[summary_output, chart_output, csv_preview] )
csv_file.change( fn=get_columns, inputs=csv_file, outputs=[x_input, y_input, available_cols, chart_type] )
csv_file.clear( fn=reset_outputs, inputs=None, outputs=[summary_output, chart_output, csv_preview, x_input, y_input, chart_type] )
if __name__ == "__main__": demo.launch()app.pyAdded+170