tc-ha commited on
Commit
5c9bf40
1 Parent(s): 51dfab7

add requirement

Browse files
Files changed (2) hide show
  1. app.py +90 -90
  2. requirements.txt +9 -9
app.py CHANGED
@@ -1,122 +1,122 @@
1
- import streamlit as st
2
 
3
- x = st.slider('Select a value')
4
- st.write(x, 'squared is', x * x)
5
 
6
- # import streamlit as st
7
- # import torch
8
- # from PIL import Image
9
- # import json
10
- # from tqdm import tqdm
11
-
12
- # from transformers import AutoModelForQuestionAnswering, LayoutLMv2Processor, AutoTokenizer
13
-
14
- # class Config():
15
- # def __init__(self):
16
- # self.data_dir = "/opt/ml/input/data/"
17
- # self.model = "layoutlmv2"
18
- # self.device = "cpu"
19
- # self.checkpoint = "microsoft/layoutlmv2-base-uncased"
20
- # self.use_ocr_library = False
21
- # self.debug = False
22
- # self.batch_data = 1
23
- # self.num_proc = 1
24
- # self.shuffle = True
25
 
26
- # self.lr = 5e-6
27
- # self.seed = 42
28
- # self.batch = 1
29
- # self.max_len = 512
30
- # self.epochs = 1000
31
 
32
- # self.fuzzy = False
33
- # self.model_name = ''
34
 
35
- # config = Config()
36
 
37
- # def predict_start_first(outputs):
38
- # start_logits = outputs.start_logits
39
- # end_logits = outputs.end_logits
40
 
41
- # predicted_start_idx_list = []
42
- # predicted_end_idx_list = []
43
 
44
- # start_position = start_logits.argmax(1)
45
 
46
- # for i in range(len(start_logits)):
47
 
48
- # start = start_position[i]
49
- # predicted_start_idx_list.append(start)
50
- # max_score = -float('inf')
51
- # predicted_end_idx = 0
52
 
53
- # for end in range(start, len(end_logits[i])):
54
- # score = end_logits[i][end]
55
- # if score > max_score:
56
- # max_score = score
57
- # predicted_end_idx = end
58
 
59
- # predicted_end_idx_list.append(predicted_end_idx)
60
 
61
- # return predicted_start_idx_list, predicted_end_idx_list
62
 
63
- # # Define function to make predictions
64
- # def predict(config, model, image, question):
65
 
66
- # processor = LayoutLMv2Processor.from_pretrained("microsoft/layoutlmv2-base-uncased")
67
- # encoding = processor(image, question, return_tensors="pt")
68
 
69
- # # model
70
- # with torch.no_grad():
71
- # output = model(
72
- # input_ids=encoding['input_ids'],
73
- # attention_mask=encoding['attention_mask'],
74
- # token_type_ids=encoding['token_type_ids'],
75
- # bbox=encoding['bbox'], image=encoding['image']
76
- # )
77
 
78
- # predicted_start_idx, predicted_end_idx = predict_start_first(output)
79
 
80
- # answer = processor.tokenizer.decode(encoding['input_ids'][0, predicted_start_idx[0]:predicted_end_idx[0]+1])
81
 
82
- # return answer
83
 
84
- # def main(config):
85
 
86
- # # Load deep learning model
87
- # checkpoint = ''
88
- # model = AutoModelForQuestionAnswering.from_pretrained('microsoft/layoutlmv2-base-uncased').to(config.device)
89
- # # model.load_state_dict(torch.load("model"))
90
 
91
- # # Create Streamlit app
92
- # st.title('Deep Learning Pipeline')
93
- # st.write('Upload an image and ask a question to get a prediction')
94
 
95
- # # Create file uploader and text input widgets
96
- # uploaded_file = st.file_uploader("Choose an image", type=['jpg', 'jpeg', 'png'])
97
- # question = st.text_input('Ask a question')
98
 
99
- # # If file is uploaded, show the image
100
- # if uploaded_file is not None:
101
- # image = Image.open(uploaded_file).convert("RGB")
102
- # st.image(image, caption='Uploaded Image', use_column_width=True)
103
 
104
- # # If question is asked and file is uploaded, make a prediction
105
- # if st.button('Get Prediction') and uploaded_file is not None and question != '':
106
- # # Preprocess the image and question as needed
107
- # # ...
108
 
109
- # # Make the prediction
110
- # with st.spinner('Predicting...'):
111
- # output = predict(config, model, image, question)
112
 
113
- # # Show the output
114
- # st.write('Output:', output)
115
 
116
 
117
- # if __name__ == '__main__':
118
- # config = Config()
119
- # main(config)
120
 
121
 
122
 
 
1
+ # import streamlit as st
2
 
3
+ # x = st.slider('Select a value')
4
+ # st.write(x, 'squared is', x * x)
5
 
6
+ import streamlit as st
7
+ import torch
8
+ from PIL import Image
9
+ import json
10
+ from tqdm import tqdm
11
+
12
+ from transformers import AutoModelForQuestionAnswering, LayoutLMv2Processor, AutoTokenizer
13
+
14
+ class Config():
15
+ def __init__(self):
16
+ self.data_dir = "/opt/ml/input/data/"
17
+ self.model = "layoutlmv2"
18
+ self.device = "cpu"
19
+ self.checkpoint = "microsoft/layoutlmv2-base-uncased"
20
+ self.use_ocr_library = False
21
+ self.debug = False
22
+ self.batch_data = 1
23
+ self.num_proc = 1
24
+ self.shuffle = True
25
 
26
+ self.lr = 5e-6
27
+ self.seed = 42
28
+ self.batch = 1
29
+ self.max_len = 512
30
+ self.epochs = 1000
31
 
32
+ self.fuzzy = False
33
+ self.model_name = ''
34
 
35
+ config = Config()
36
 
37
+ def predict_start_first(outputs):
38
+ start_logits = outputs.start_logits
39
+ end_logits = outputs.end_logits
40
 
41
+ predicted_start_idx_list = []
42
+ predicted_end_idx_list = []
43
 
44
+ start_position = start_logits.argmax(1)
45
 
46
+ for i in range(len(start_logits)):
47
 
48
+ start = start_position[i]
49
+ predicted_start_idx_list.append(start)
50
+ max_score = -float('inf')
51
+ predicted_end_idx = 0
52
 
53
+ for end in range(start, len(end_logits[i])):
54
+ score = end_logits[i][end]
55
+ if score > max_score:
56
+ max_score = score
57
+ predicted_end_idx = end
58
 
59
+ predicted_end_idx_list.append(predicted_end_idx)
60
 
61
+ return predicted_start_idx_list, predicted_end_idx_list
62
 
63
+ # Define function to make predictions
64
+ def predict(config, model, image, question):
65
 
66
+ processor = LayoutLMv2Processor.from_pretrained("microsoft/layoutlmv2-base-uncased")
67
+ encoding = processor(image, question, return_tensors="pt")
68
 
69
+ # model
70
+ with torch.no_grad():
71
+ output = model(
72
+ input_ids=encoding['input_ids'],
73
+ attention_mask=encoding['attention_mask'],
74
+ token_type_ids=encoding['token_type_ids'],
75
+ bbox=encoding['bbox'], image=encoding['image']
76
+ )
77
 
78
+ predicted_start_idx, predicted_end_idx = predict_start_first(output)
79
 
80
+ answer = processor.tokenizer.decode(encoding['input_ids'][0, predicted_start_idx[0]:predicted_end_idx[0]+1])
81
 
82
+ return answer
83
 
84
+ def main(config):
85
 
86
+ # Load deep learning model
87
+ checkpoint = ''
88
+ model = AutoModelForQuestionAnswering.from_pretrained('microsoft/layoutlmv2-base-uncased').to(config.device)
89
+ # model.load_state_dict(torch.load("model"))
90
 
91
+ # Create Streamlit app
92
+ st.title('Deep Learning Pipeline')
93
+ st.write('Upload an image and ask a question to get a prediction')
94
 
95
+ # Create file uploader and text input widgets
96
+ uploaded_file = st.file_uploader("Choose an image", type=['jpg', 'jpeg', 'png'])
97
+ question = st.text_input('Ask a question')
98
 
99
+ # If file is uploaded, show the image
100
+ if uploaded_file is not None:
101
+ image = Image.open(uploaded_file).convert("RGB")
102
+ st.image(image, caption='Uploaded Image', use_column_width=True)
103
 
104
+ # If question is asked and file is uploaded, make a prediction
105
+ if st.button('Get Prediction') and uploaded_file is not None and question != '':
106
+ # Preprocess the image and question as needed
107
+ # ...
108
 
109
+ # Make the prediction
110
+ with st.spinner('Predicting...'):
111
+ output = predict(config, model, image, question)
112
 
113
+ # Show the output
114
+ st.write('Output:', output)
115
 
116
 
117
+ if __name__ == '__main__':
118
+ config = Config()
119
+ main(config)
120
 
121
 
122
 
requirements.txt CHANGED
@@ -1,9 +1,9 @@
1
- # datasets
2
- # editdistance
3
- # numpy
4
- # pandas
5
- # Pillow
6
- # torch
7
- # tqdm
8
- # transformers
9
- # thefuzz
 
1
+ datasets
2
+ editdistance
3
+ numpy
4
+ pandas
5
+ Pillow
6
+ torch
7
+ tqdm
8
+ transformers
9
+ thefuzz