Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 4 |
+
|
| 5 |
+
tokenizer = AutoTokenizer.from_pretrained("Salesforce/codegen2-1B")
|
| 6 |
+
model = AutoModelForCausalLM.from_pretrained("sarah111/codegen_1b_tatqa", trust_remote_code=True)
|
| 7 |
+
|
| 8 |
+
import gradio as gr
|
| 9 |
+
|
| 10 |
+
def tableQA(Question, table):
|
| 11 |
+
answer = 0
|
| 12 |
+
program = ""
|
| 13 |
+
try :
|
| 14 |
+
table = table.to_csv(header=True, index=False).strip('\n').split('\n')
|
| 15 |
+
table = '\n'.join(table)
|
| 16 |
+
#print(table)
|
| 17 |
+
entree = 'Table :\n{0}\n\nQuestion : {1}\n\nProgram :'.format(table, Question)
|
| 18 |
+
print(entree)
|
| 19 |
+
model.to(device)
|
| 20 |
+
input_ids = tokenizer(entree, return_tensors="pt").input_ids.to(device)
|
| 21 |
+
|
| 22 |
+
gen_tokens = model.generate(input_ids.to(device),do_sample=True,temperature=0.9,max_length=400)
|
| 23 |
+
output = tokenizer.batch_decode(gen_tokens)[0]
|
| 24 |
+
program = output.split("Program :````Python\n",1)[1].split("<|endoftext|>",1)[0].split("answer=")[1]
|
| 25 |
+
print(program)
|
| 26 |
+
program = program.replace(",", "")
|
| 27 |
+
answer = eval(program)
|
| 28 |
+
print(answer)
|
| 29 |
+
|
| 30 |
+
except:
|
| 31 |
+
print('exception')
|
| 32 |
+
|
| 33 |
+
return program, answer
|
| 34 |
+
|
| 35 |
+
demo = gr.Interface(
|
| 36 |
+
fn = tableQA,
|
| 37 |
+
inputs = [
|
| 38 |
+
"text",
|
| 39 |
+
gr.Dataframe(
|
| 40 |
+
headers=["", "2019", "2018"],
|
| 41 |
+
datatype=["str", "str", "str"],
|
| 42 |
+
label="Table",
|
| 43 |
+
),
|
| 44 |
+
],
|
| 45 |
+
outputs=[gr.Textbox(label="Derivation"),gr.Textbox(label="Result")],
|
| 46 |
+
title ="Outil dβaide aux financiers",
|
| 47 |
+
description = "Ce prototype met en Γ©vidence une situation rΓ©elle oΓΉ le systΓ¨me de question rΓ©ponse est mis en place pour permettre Γ des financiers de poser des questions nΓ©cessitant un raisonnement arithmΓ©tique et portant sur une table issue dβun rapport financier.",
|
| 48 |
+
examples=ex,
|
| 49 |
+
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
demo.launch(share=True)
|