PD03 commited on
Commit
53c503a
·
verified ·
1 Parent(s): 4790b2c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -47
app.py CHANGED
@@ -3,10 +3,10 @@ import gradio as gr
3
  import pandas as pd
4
  from transformers import pipeline
5
 
6
- # 1) Load your data
7
  df = pd.read_csv("synthetic_profit.csv")
8
 
9
- # 2) Prepare the TAPAS fallback
10
  tapas = pipeline(
11
  "table-question-answering",
12
  model="google/tapas-base-finetuned-wtq",
@@ -15,82 +15,66 @@ tapas = pipeline(
15
  )
16
  table = df.astype(str).to_dict(orient="records")
17
 
18
- # 3) Helpers for parsing
19
- OPERATIONS = {
20
- "total": "sum",
21
- "sum": "sum",
22
- "average": "mean",
23
- "mean": "mean"
24
- }
25
- COLUMNS = {
26
- "revenue": "Revenue",
27
- "cost": "Cost",
28
- "profit": "Profit",
29
- "margin": "ProfitMargin",
30
- "profit margin":"ProfitMargin"
31
- }
32
 
33
  def parse_and_compute(question: str):
34
  q = question.lower()
35
- # 1) detect op
 
36
  op = next((OPERATIONS[k] for k in OPERATIONS if k in q), None)
37
  # 2) detect column
38
- col = next((COLUMNS[k] for k in COLUMNS if k in q), None)
39
- # 3) detect product (assumes "Product X")
40
- m = re.search(r"product\s*([A-Za-z0-9]+)", q)
41
- prod = f"Product {m.group(1)}" if m else None
42
- # 4) detect region from known values
43
- region = next((r for r in df["Region"].unique() if r.lower() in q), None)
44
- # 5) detect year
45
- y = re.search(r"\b(20\d{2})\b", q)
46
- year = int(y.group(1)) if y else None
47
- # 6) detect quarter
48
- qtr = next((fq for fq in df["FiscalQuarter"].unique() if fq.lower() in q), None)
49
 
 
50
  if None in (op, col, prod, region, year, qtr):
51
- return None # fallback
52
 
53
- # filter
54
  sub = df[
55
  (df["Product"] == prod) &
56
  (df["Region"] == region) &
57
  (df["FiscalYear"] == year) &
58
- (df["FiscalQuarter"] == qtr)
59
  ]
60
-
61
- # compute
62
  try:
63
  val = getattr(sub[col], op)()
64
- except Exception:
65
  return None
66
 
67
  return f"{op.capitalize()} {col} for {prod} in {region}, {qtr} {year}: {val:.2f}"
68
 
69
- # 4) Main answer fn
70
  def answer(question: str) -> str:
71
- res = parse_and_compute(question)
72
- if res is not None:
73
- return res
74
 
75
- # fallback to TAPAS
76
  try:
77
- out = tapas(table=table, query=question)
78
- return out.get("answer", "No answer found.")
79
  except Exception as e:
80
  return f"❌ Error: {e}"
81
 
82
- # 5) Gradio UI
83
  iface = gr.Interface(
84
  fn=answer,
85
  inputs=gr.Textbox(lines=2, placeholder="e.g. What is the total revenue for Product A in EMEA in Q1 2024?"),
86
  outputs=gr.Textbox(lines=3),
87
  title="SAP Profitability Q&A",
88
- description=(
89
- "Supports any basic “total”/“average” question by parsing and computing via Pandas. \n"
90
- "Falls back to TAPAS for anything else."
91
- ),
92
  allow_flagging="never",
93
  )
94
 
95
- if __name__ == "__main__":
96
  iface.launch(server_name="0.0.0.0", server_port=7860)
 
3
  import pandas as pd
4
  from transformers import pipeline
5
 
6
+ # Load data
7
  df = pd.read_csv("synthetic_profit.csv")
8
 
9
+ # Prepare TAPAS fallback
10
  tapas = pipeline(
11
  "table-question-answering",
12
  model="google/tapas-base-finetuned-wtq",
 
15
  )
16
  table = df.astype(str).to_dict(orient="records")
17
 
18
+ # Helpers
19
+ OPERATIONS = {"total": "sum", "sum": "sum", "average": "mean", "mean": "mean"}
20
+ COLUMNS = {"revenue": "Revenue", "cost": "Cost", "profit": "Profit", "margin":"ProfitMargin","profit margin":"ProfitMargin"}
 
 
 
 
 
 
 
 
 
 
 
21
 
22
  def parse_and_compute(question: str):
23
  q = question.lower()
24
+
25
+ # 1) detect operation
26
  op = next((OPERATIONS[k] for k in OPERATIONS if k in q), None)
27
  # 2) detect column
28
+ col = next((COLUMNS[k] for k in COLUMNS if k in q), None)
29
+ # 3) detect product by scanning your actual values
30
+ prod = next((p for p in df["Product"].unique() if p.lower() in q), None)
31
+ # 4) region
32
+ region = next((r for r in df["Region"].unique() if r.lower() in q), None)
33
+ # 5) year
34
+ yr_match = re.search(r"\b(20\d{2})\b", q)
35
+ year = int(yr_match.group(1)) if yr_match else None
36
+ # 6) quarter
37
+ qtr = next((x for x in df["FiscalQuarter"].unique() if x.lower() in q), None)
 
38
 
39
+ # if any piece missing, we fallback
40
  if None in (op, col, prod, region, year, qtr):
41
+ return None
42
 
43
+ # filter & compute
44
  sub = df[
45
  (df["Product"] == prod) &
46
  (df["Region"] == region) &
47
  (df["FiscalYear"] == year) &
48
+ (df["FiscalQuarter"]== qtr)
49
  ]
 
 
50
  try:
51
  val = getattr(sub[col], op)()
52
+ except:
53
  return None
54
 
55
  return f"{op.capitalize()} {col} for {prod} in {region}, {qtr} {year}: {val:.2f}"
56
 
 
57
  def answer(question: str) -> str:
58
+ out = parse_and_compute(question)
59
+ if out is not None:
60
+ return out
61
 
62
+ # fallback
63
  try:
64
+ res = tapas(table=table, query=question)
65
+ return res.get("answer", "No answer found.")
66
  except Exception as e:
67
  return f"❌ Error: {e}"
68
 
69
+ # Gradio...
70
  iface = gr.Interface(
71
  fn=answer,
72
  inputs=gr.Textbox(lines=2, placeholder="e.g. What is the total revenue for Product A in EMEA in Q1 2024?"),
73
  outputs=gr.Textbox(lines=3),
74
  title="SAP Profitability Q&A",
75
+ description="Basic total/average queries via Pandas+fallback to TAPAS",
 
 
 
76
  allow_flagging="never",
77
  )
78
 
79
+ if __name__=="__main__":
80
  iface.launch(server_name="0.0.0.0", server_port=7860)