Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,95 +1,30 @@
|
|
| 1 |
-
import pandas as pd
|
| 2 |
import gradio as gr
|
| 3 |
-
|
| 4 |
-
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
|
| 8 |
|
| 9 |
-
def
|
| 10 |
-
# Read
|
| 11 |
df = pd.read_excel(file.name)
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
# Filter dataframe by selected columns
|
| 20 |
-
df_subset = df[cols_selected].head(10)
|
| 21 |
-
preview = df_subset.to_string()
|
| 22 |
-
|
| 23 |
-
# Prepare prompt for AI
|
| 24 |
-
prompt = f"Analyze the following dataset snippet:\n{preview}\nProvide a concise summary of the data."
|
| 25 |
-
ai_response = model.generate(prompt)
|
| 26 |
-
|
| 27 |
-
# Metadata summary: number of rows, missing values per selected column
|
| 28 |
-
metadata = {
|
| 29 |
-
'Row count': len(df),
|
| 30 |
-
'Selected columns': cols_selected,
|
| 31 |
-
'Missing values': df[cols_selected].isnull().sum().to_dict()
|
| 32 |
-
}
|
| 33 |
-
|
| 34 |
-
# Create bar chart of missing values
|
| 35 |
-
missing_counts = df[cols_selected].isnull().sum()
|
| 36 |
-
fig = px.bar(
|
| 37 |
-
x=missing_counts.index,
|
| 38 |
-
y=missing_counts.values,
|
| 39 |
-
labels={'x': 'Columns', 'y': 'Missing Values'},
|
| 40 |
-
title="Missing Values per Selected Column"
|
| 41 |
-
)
|
| 42 |
-
fig.update_layout(xaxis_tickangle=-45)
|
| 43 |
-
|
| 44 |
-
return ai_response, metadata, fig
|
| 45 |
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
cols_dropdown = gr.CheckboxGroup(label="Select Columns to Analyze")
|
| 58 |
-
|
| 59 |
-
# Button to load columns after upload
|
| 60 |
-
load_button = gr.Button("Load Columns")
|
| 61 |
-
|
| 62 |
-
# Textbox for AI analysis output
|
| 63 |
-
ai_output = gr.Textbox(label="AI Analysis Output", lines=10)
|
| 64 |
-
|
| 65 |
-
# Metadata display (JSON)
|
| 66 |
-
meta_output = gr.JSON(label="Metadata Summary")
|
| 67 |
-
|
| 68 |
-
# Plot output for missing values
|
| 69 |
-
plot_output = gr.Plot(label="Missing Values Chart")
|
| 70 |
-
|
| 71 |
-
def load_columns(file):
|
| 72 |
-
df, cols1, cols2 = load_file(file)
|
| 73 |
-
return df, cols1, cols2
|
| 74 |
-
|
| 75 |
-
def update_dropdown(df, cols):
|
| 76 |
-
# Update dropdown choices
|
| 77 |
-
return gr.update(choices=cols, value=cols) # default all selected
|
| 78 |
-
|
| 79 |
-
def run_analysis(df, selected_cols):
|
| 80 |
-
return analyze_data(df, selected_cols)
|
| 81 |
-
|
| 82 |
-
# Upload triggers load file and update state
|
| 83 |
-
file_input.upload(load_columns, inputs=file_input, outputs=[state_df, state_cols, cols_dropdown])
|
| 84 |
-
|
| 85 |
-
# Load columns button updates dropdown choices and selects all by default
|
| 86 |
-
load_button.click(update_dropdown, inputs=[state_df, state_cols], outputs=cols_dropdown)
|
| 87 |
-
|
| 88 |
-
# Analyze on column selection change
|
| 89 |
-
cols_dropdown.change(run_analysis, inputs=[state_df, cols_dropdown], outputs=[ai_output, meta_output, plot_output])
|
| 90 |
-
|
| 91 |
-
# Also analyze when clicking load_button (with default columns selected)
|
| 92 |
-
load_button.click(run_analysis, inputs=[state_df, cols_dropdown], outputs=[ai_output, meta_output, plot_output])
|
| 93 |
-
|
| 94 |
-
if __name__ == "__main__":
|
| 95 |
-
demo.launch()
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
from transformers import pipeline
|
| 4 |
|
| 5 |
+
# Load a small hosted model (no download needed)
|
| 6 |
+
generator = pipeline("text-generation", model="mrm8488/t5-base-finetuned-e2m")
|
| 7 |
|
| 8 |
+
def analyze_xlsx(file, prompt):
|
| 9 |
+
# Read the first few rows from the XLSX
|
| 10 |
df = pd.read_excel(file.name)
|
| 11 |
+
preview = df.head(5).to_string(index=False)
|
| 12 |
+
|
| 13 |
+
# Combine prompt and table data
|
| 14 |
+
full_prompt = prompt + "\n\n" + preview
|
| 15 |
|
| 16 |
+
# Generate AI response
|
| 17 |
+
result = generator(full_prompt, max_new_tokens=100, do_sample=True, temperature=0.7)
|
| 18 |
+
return result[0]['generated_text']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
+
# Gradio interface
|
| 21 |
+
gr.Interface(
|
| 22 |
+
fn=analyze_xlsx,
|
| 23 |
+
inputs=[
|
| 24 |
+
gr.File(file_types=[".xlsx"], label="Upload XLSX file"),
|
| 25 |
+
gr.Textbox(lines=2, placeholder="Enter your prompt (e.g. Summarize this data)", label="Your Prompt")
|
| 26 |
+
],
|
| 27 |
+
outputs=gr.Textbox(label="AI Response"),
|
| 28 |
+
title="📊 XLSX AI Analyzer with Transformers",
|
| 29 |
+
description="Upload an Excel file and ask questions or request a summary. No API key or local model required.",
|
| 30 |
+
).launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|