tc-ha commited on
Commit
51dfab7
1 Parent(s): 472c73d

add requirement

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