rosemariafontana commited on
Commit
8bbfc12
Β·
verified Β·
1 Parent(s): 4959cde

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +78 -0
app.py ADDED
@@ -0,0 +1,78 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import pandas as pd
3
+
4
+ from transformers import pipeline
5
+
6
+
7
+ # Chatbot model
8
+ model = pipeline("document-question-answering", model="importa/layoutlm-document-qa")
9
+
10
+
11
+ def parse_ticket_image(image, question):
12
+ """Basically just runs through these questions for the document"""
13
+ # Define questions you want to ask the model
14
+
15
+ questions = [
16
+ {"question": "What is the ticket number?", "context": image},
17
+ {"question": "What is the date?", "context": image},
18
+ {"question": "What is the time?", "context": image},
19
+ {"question": "What is the gross weight?", "context": image},
20
+ {"question": "What is the tare weight?", "context": image},
21
+ {"question": "What is the net weight?", "context": image},
22
+ {"question": "What is the moisture (moist) percentage?", "context": image},
23
+ {"question": "What is the damage percentage?", "context": image},
24
+ {"question": "What is the gross units?", "context": image},
25
+ {"question": "What is the dock units?", "context": image},
26
+ {"question": "What is the comment?", "context": image},
27
+ {"question": "What is the assembly number?", "context": image},
28
+ ]
29
+
30
+ # Use the model to answer each question
31
+ results = [doc_qa(q["question"], q["context"]) for q in questions]
32
+
33
+ # Extract answers from the results
34
+ ticket_number = results[0][0]['answer']
35
+ date = results[1][0]['answer']
36
+ time = results[2][0]['answer']
37
+ gross_weight = results[3][0]['answer']
38
+ tare_weight = results[4][0]['answer']
39
+ net_weight = results[5][0]['answer']
40
+ moisture = results[6][0]['answer']
41
+ damage = results[7][0]['answer']
42
+ gross_units = results[8][0]['answer']
43
+ dock_units = results[9][0]['answer']
44
+ comment = results[10][0]['answer']
45
+ assembly_number = results[11][0]['answer']
46
+
47
+ # Create a structured format (like a table) using pandas
48
+ data = {
49
+ "Ticket Number": [ticket_number],
50
+ "Assembly Number": [assembly_number],
51
+ "Date": [date],
52
+ "Time": [time],
53
+ "Gross Weight": [gross_weight],
54
+ "Tare Weight": [tare_weight],
55
+ "Net Weight": [net_weight],
56
+ "Moisture": [moisture],
57
+ "Damage": [damage],
58
+ "Gross Units": [gross_units],
59
+ "Dock Units": [dock_units],
60
+ "Comment": [comment],
61
+ }
62
+ df = pd.DataFrame(data)
63
+
64
+ return df
65
+
66
+
67
+ """
68
+ For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
69
+ """
70
+ demo = gr.Interface(
71
+ fn=parse_ticket_image,
72
+ inputs=[gr.Image(label= "Upload your Grain Scale Ticket", type="pil")],
73
+ outputs=[gr.Dataframe(headers=["Field", "Value"], label="Extracted Grain Scale Data"],
74
+ )
75
+
76
+
77
+ if __name__ == "__main__":
78
+ demo.launch()