clementBE commited on
Commit
f421028
·
verified ·
1 Parent(s): c9f4ed9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -89
app.py CHANGED
@@ -1,95 +1,30 @@
1
- import pandas as pd
2
  import gradio as gr
3
- from gpt4all import GPT4All
4
- import plotly.express as px
5
 
6
- # Initialize GPT4All model (adjust model path if needed)
7
- model = GPT4All("ggml-gpt4all-l13b-snoozy.bin")
8
 
9
- def load_file(file):
10
- # Read XLSX file
11
  df = pd.read_excel(file.name)
12
- cols = df.columns.tolist()
13
- return df, cols, cols # return dataframe + columns (for dropdowns)
 
 
14
 
15
- def analyze_data(df, cols_selected):
16
- if not cols_selected:
17
- return "No columns selected.", None, None
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
- with gr.Blocks() as demo:
47
- gr.Markdown("# Advanced XLSX Analyzer with AI & Metadata Visualization")
48
-
49
- # Upload
50
- file_input = gr.File(label="Upload XLSX file", file_types=['.xlsx'])
51
-
52
- # Placeholders for loaded data and columns
53
- state_df = gr.State()
54
- state_cols = gr.State()
55
-
56
- # Dropdown to select columns dynamically
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()