ITSAIDI commited on
Commit
60c1b07
1 Parent(s): bb4adf7
Files changed (2) hide show
  1. App.py +14 -32
  2. utilitis.py +48 -4
App.py CHANGED
@@ -1,14 +1,11 @@
1
  import streamlit as st
2
- from utilitis import Draw, Add_Results, Change_Image
3
  from PIL import Image
4
  import time
5
 
6
- def check_if_changed(original_values, updated_values):
7
- for key, value in original_values.items():
8
- if updated_values[key] != value:
9
- return True
10
- return False
11
 
 
12
  st.title("Bienvenue à Textra Web App")
13
  st.markdown("### Drag and Drop votre facture ici:")
14
  st.write("(PNG, JPG, JPEG)")
@@ -26,35 +23,20 @@ if uploaded_file is not None:
26
  # Process the image and retrieve results
27
  image, Results = process_image(uploaded_file)
28
  Change_Image(image,image_initiale)
 
 
 
29
  st.sidebar.title('Results')
30
- st.write(Results)
31
- # Define text inputs with initial values
32
- text_fourni = st.sidebar.text_input("Fournisseur", value=Results["Fournisseur"])
33
- text_InvDate = st.sidebar.text_input("Date Facture", value=Results["Date Facture"])
34
- text_InvNum = st.sidebar.text_input("Numéro de facture", value=Results["Numéro de facture"])
35
- text_TT = st.sidebar.text_input("Total HT", value=Results["Total HT"])
36
- text_TTC = st.sidebar.text_input("TTC", value=Results["Total TTC"])
37
- text_TVA = st.sidebar.text_input("TVA", value=Results["TVA"])
38
-
39
- st.write("Predicted Text:", text_fourni, text_InvDate, text_InvNum, text_TT, text_TVA, text_TTC)
40
-
41
- New_results = {
42
- "Fournisseur": text_fourni,
43
- "Date Facture": text_InvDate,
44
- "Numéro de facture": text_InvNum,
45
- "Total HT": text_TT,
46
- "TVA": text_TVA,
47
- "Total TTC": text_TTC
48
- }
49
-
50
  # Check if any input has been changed
51
  if check_if_changed(Results,New_results):
52
- st.write(check_if_changed(Results,New_results))
53
- # Add a button to save changes
54
- if st.sidebar.button("Sauvegarder"):
55
- # Get updated values from text inputs
56
- updated_results = New_results
57
- success_message = st.sidebar.empty()
58
  success_message.success("Les résultats ont été sauvegardés avec succès !")
59
  time.sleep(1)
60
  success_message.empty()
 
 
 
 
 
1
  import streamlit as st
2
+ from utilitis import Draw,Change_Image,check_if_changed,Update
3
  from PIL import Image
4
  import time
5
 
6
+
 
 
 
 
7
 
8
+ st.set_page_config(layout='wide')
9
  st.title("Bienvenue à Textra Web App")
10
  st.markdown("### Drag and Drop votre facture ici:")
11
  st.write("(PNG, JPG, JPEG)")
 
23
  # Process the image and retrieve results
24
  image, Results = process_image(uploaded_file)
25
  Change_Image(image,image_initiale)
26
+ # Some Initializations
27
+ sauvgarder_button = st.sidebar.empty()
28
+ success_message = st.sidebar.empty()
29
  st.sidebar.title('Results')
30
+ # Get Track of User Modeifications :
31
+ New_results = Update(Results)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
  # Check if any input has been changed
33
  if check_if_changed(Results,New_results):
34
+ st.write(check_if_changed(Results,New_results))
35
+ if sauvgarder_button.button("Sauvegarder"):
 
 
 
 
36
  success_message.success("Les résultats ont été sauvegardés avec succès !")
37
  time.sleep(1)
38
  success_message.empty()
39
+ st.write(New_results)
40
+
41
+
42
+
utilitis.py CHANGED
@@ -65,7 +65,8 @@ def Encode(image):
65
  encoding = processor(image, words, boxes=boxes,return_offsets_mapping=True,truncation=True, max_length=512, padding="max_length", return_tensors="pt")
66
  offset_mapping = encoding.pop('offset_mapping')
67
  return encoding, offset_mapping,words
68
-
 
69
  def unnormalize_box(bbox, width, height):
70
  return [
71
  width * (bbox[0] / 1000),
@@ -73,7 +74,8 @@ def unnormalize_box(bbox, width, height):
73
  width * (bbox[2] / 1000),
74
  height * (bbox[3] / 1000),
75
  ]
76
-
 
77
  def Run_model(image):
78
  encoding,offset_mapping,words = Encode(image)
79
  device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
@@ -94,7 +96,8 @@ def Run_model(image):
94
  true_boxes = [unnormalize_box(box, width, height) for idx, box in enumerate(token_boxes) if not is_subword[idx]]
95
  return true_predictions,true_boxes,words
96
 
97
-
 
98
  def Get_Json(true_predictions,words):
99
  Results = {}
100
  i = 0
@@ -107,7 +110,8 @@ def Get_Json(true_predictions,words):
107
  Results = {key_mapping.get(key, key): value for key, value in Results.items()}
108
  return Results
109
 
110
-
 
111
  def Draw(image):
112
  true_predictions, true_boxes,words = Run_model(image)
113
  draw = ImageDraw.Draw(image)
@@ -152,11 +156,51 @@ def Draw(image):
152
 
153
  return image,Results
154
 
 
 
155
 
156
  def Add_Results(data):
157
  # Render the table
158
  for key, value in data.items():
159
  data[key] = st.sidebar.text_input(key, value)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
160
 
161
  #############################################################################
162
  #############################################################################
 
65
  encoding = processor(image, words, boxes=boxes,return_offsets_mapping=True,truncation=True, max_length=512, padding="max_length", return_tensors="pt")
66
  offset_mapping = encoding.pop('offset_mapping')
67
  return encoding, offset_mapping,words
68
+ #############################################################################
69
+ #############################################################################
70
  def unnormalize_box(bbox, width, height):
71
  return [
72
  width * (bbox[0] / 1000),
 
74
  width * (bbox[2] / 1000),
75
  height * (bbox[3] / 1000),
76
  ]
77
+ #############################################################################
78
+ #############################################################################
79
  def Run_model(image):
80
  encoding,offset_mapping,words = Encode(image)
81
  device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
 
96
  true_boxes = [unnormalize_box(box, width, height) for idx, box in enumerate(token_boxes) if not is_subword[idx]]
97
  return true_predictions,true_boxes,words
98
 
99
+ #############################################################################
100
+ #############################################################################
101
  def Get_Json(true_predictions,words):
102
  Results = {}
103
  i = 0
 
110
  Results = {key_mapping.get(key, key): value for key, value in Results.items()}
111
  return Results
112
 
113
+ #############################################################################
114
+ #############################################################################
115
  def Draw(image):
116
  true_predictions, true_boxes,words = Run_model(image)
117
  draw = ImageDraw.Draw(image)
 
156
 
157
  return image,Results
158
 
159
+ #############################################################################
160
+ #############################################################################
161
 
162
  def Add_Results(data):
163
  # Render the table
164
  for key, value in data.items():
165
  data[key] = st.sidebar.text_input(key, value)
166
+ #############################################################################
167
+ #############################################################################
168
+
169
+ def check_if_changed(original_values, updated_values):
170
+ for key, value in original_values.items():
171
+ if updated_values[key] != value:
172
+ return True
173
+ return False
174
+ #############################################################################
175
+ #############################################################################
176
+
177
+ def Update(Results):
178
+ New_results = {}
179
+
180
+ if "Fournisseur" in Results.keys():
181
+ text_fourni = st.sidebar.text_input("Fournisseur", value=Results["Fournisseur"])
182
+ New_results["Fournisseur"] = text_fourni
183
+
184
+ if "Date Facture" in Results.keys():
185
+ text_InvDate = st.sidebar.text_input("Date Facture", value=Results["Date Facture"])
186
+ New_results["Date Facture"] = text_InvDate
187
+
188
+ if "Numéro de facture" in Results.keys():
189
+ text_InvNum = st.sidebar.text_input("Numéro de facture", value=Results["Numéro de facture"])
190
+ New_results["Numéro de facture"] = text_InvNum
191
+
192
+ if "Total HT" in Results.keys():
193
+ text_TT = st.sidebar.text_input("Total HT", value=Results["Total HT"])
194
+ New_results["Total HT"] = text_TT
195
+
196
+ if "TVA" in Results.keys():
197
+ text_TVA = st.sidebar.text_input("TVA", value=Results["TVA"])
198
+ New_results["TVA"] = text_TVA
199
+
200
+ if "Total TTC" in Results.keys():
201
+ text_TTC = st.sidebar.text_input("TTC", value=Results["Total TTC"])
202
+ New_results["Total TTC"] = text_TTC
203
+ return New_results
204
 
205
  #############################################################################
206
  #############################################################################