PD03 commited on
Commit
ef15351
·
verified ·
1 Parent(s): 8be1581

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -7
app.py CHANGED
@@ -1,20 +1,24 @@
 
 
1
  import gradio as gr
2
  import pandas as pd
3
  import torch
4
  from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
5
 
6
- # 1) Load data
7
  df = pd.read_csv('synthetic_profit.csv')
8
 
9
- # 2) Use the publicly available TAPEX base WikiSQL model
10
  MODEL_ID = "microsoft/tapex-base-finetuned-wikisql"
11
 
12
- # 3) Ensure backend is available
13
  device = 0 if torch.cuda.is_available() else -1
14
 
 
15
  tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
16
  model = AutoModelForSeq2SeqLM.from_pretrained(MODEL_ID)
17
 
 
18
  table_qa = pipeline(
19
  "table-question-answering",
20
  model=model,
@@ -23,23 +27,29 @@ table_qa = pipeline(
23
  device=device,
24
  )
25
 
26
- # 4) QA function
27
  def answer_profitability(question: str) -> str:
28
- table = df.to_dict(orient="records")
 
 
29
  try:
30
  out = table_qa(table=table, query=question)
31
  return out.get("answer", "No answer found.")
32
  except Exception as e:
33
  return f"Error: {e}"
34
 
35
- # 5) Gradio UI
36
  iface = gr.Interface(
37
  fn=answer_profitability,
38
  inputs=gr.Textbox(lines=2, placeholder="Ask a question about profitability…"),
39
  outputs="text",
40
  title="SAP Profitability Q&A (TAPEX-Base)",
41
- description="Free-form questions on synthetic profitability data using microsoft/tapex-base-finetuned-wikisql."
 
 
 
42
  )
43
 
 
44
  if __name__ == "__main__":
45
  iface.launch(server_name="0.0.0.0", server_port=7860)
 
1
+ # app.py
2
+
3
  import gradio as gr
4
  import pandas as pd
5
  import torch
6
  from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
7
 
8
+ # 1) Load your synthetic profitability dataset
9
  df = pd.read_csv('synthetic_profit.csv')
10
 
11
+ # 2) Choose the publicly available TAPEX WikiSQL model
12
  MODEL_ID = "microsoft/tapex-base-finetuned-wikisql"
13
 
14
+ # 3) Set device: GPU if available, else CPU
15
  device = 0 if torch.cuda.is_available() else -1
16
 
17
+ # 4) Load tokenizer and model
18
  tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
19
  model = AutoModelForSeq2SeqLM.from_pretrained(MODEL_ID)
20
 
21
+ # 5) Build the table-question-answering pipeline
22
  table_qa = pipeline(
23
  "table-question-answering",
24
  model=model,
 
27
  device=device,
28
  )
29
 
30
+ # 6) Define the QA function, casting all cells to strings to avoid float issues
31
  def answer_profitability(question: str) -> str:
32
+ # Cast entire DataFrame to string
33
+ df_str = df.astype(str)
34
+ table = df_str.to_dict(orient="records")
35
  try:
36
  out = table_qa(table=table, query=question)
37
  return out.get("answer", "No answer found.")
38
  except Exception as e:
39
  return f"Error: {e}"
40
 
41
+ # 7) Define Gradio interface
42
  iface = gr.Interface(
43
  fn=answer_profitability,
44
  inputs=gr.Textbox(lines=2, placeholder="Ask a question about profitability…"),
45
  outputs="text",
46
  title="SAP Profitability Q&A (TAPEX-Base)",
47
+ description=(
48
+ "Free-form questions on the synthetic profitability dataset, "
49
+ "powered end-to-end by microsoft/tapex-base-finetuned-wikisql."
50
+ )
51
  )
52
 
53
+ # 8) Launch the app
54
  if __name__ == "__main__":
55
  iface.launch(server_name="0.0.0.0", server_port=7860)