clementBE commited on
Commit
7d37829
·
verified ·
1 Parent(s): 8a07003

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -18
app.py CHANGED
@@ -1,30 +1,61 @@
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()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
  import pandas as pd
3
+ import matplotlib.pyplot as plt
4
+ import io
5
  from transformers import pipeline
6
+ import plotly.express as px
7
 
8
+ # Load small hosted model (no key or local model needed)
9
  generator = pipeline("text-generation", model="mrm8488/t5-base-finetuned-e2m")
10
 
11
+ # Global variable to store uploaded dataframe
12
+ uploaded_df = {}
13
+
14
+ def read_xlsx(file):
15
  df = pd.read_excel(file.name)
16
+ uploaded_df['df'] = df
17
+ return df.columns.tolist(), "Preview:\n" + df.head(5).to_string(index=False)
18
 
19
+ def generate_ai_summary(prompt):
20
+ df = uploaded_df.get('df')
21
+ if df is None:
22
+ return "No data loaded."
23
+ preview = df.head(5).to_string(index=False)
24
  full_prompt = prompt + "\n\n" + preview
 
 
25
  result = generator(full_prompt, max_new_tokens=100, do_sample=True, temperature=0.7)
26
  return result[0]['generated_text']
27
 
28
+ def plot_columns(x_col, y_col):
29
+ df = uploaded_df.get('df')
30
+ if df is None or x_col not in df.columns or y_col not in df.columns:
31
+ return None
32
+ fig = px.bar(df.head(20), x=x_col, y=y_col, title=f"{y_col} by {x_col}")
33
+ return fig
34
+
35
+ # Gradio Interface
36
+ with gr.Blocks() as demo:
37
+ gr.Markdown("# 📈 XLSX Analyzer + AI Summary + Plot")
38
+
39
+ with gr.Row():
40
+ file_input = gr.File(file_types=[".xlsx"], label="Upload XLSX File")
41
+ col_output = gr.Textbox(label="Columns", interactive=False)
42
+
43
+ with gr.Row():
44
+ x_dropdown = gr.Dropdown(label="X Axis", choices=[])
45
+ y_dropdown = gr.Dropdown(label="Y Axis", choices=[])
46
+
47
+ plot_output = gr.Plot(label="Bar Plot (Top 20)")
48
+
49
+ with gr.Row():
50
+ prompt = gr.Textbox(label="Your Prompt", placeholder="Summarize this table or ask a question", lines=2)
51
+ ai_output = gr.Textbox(label="AI Summary")
52
+
53
+ file_input.change(read_xlsx, inputs=file_input, outputs=[x_dropdown, col_output])
54
+ file_input.change(read_xlsx, inputs=file_input, outputs=[y_dropdown, gr.Textbox(visible=False)])
55
+
56
+ x_dropdown.change(plot_columns, inputs=[x_dropdown, y_dropdown], outputs=plot_output)
57
+ y_dropdown.change(plot_columns, inputs=[x_dropdown, y_dropdown], outputs=plot_output)
58
+
59
+ prompt.submit(generate_ai_summary, inputs=prompt, outputs=ai_output)
60
+
61
+ demo.launch()