alecinvan commited on
Commit
d54084e
1 Parent(s): c1a8463

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +99 -32
app.py CHANGED
@@ -1,43 +1,64 @@
1
- # OCR Translate v0.2
2
- # 创建人:曾逸夫
3
- # 创建时间:2022-07-19
 
4
 
5
  import os
6
 
7
  os.system("sudo apt-get install xclip")
8
 
 
9
  import gradio as gr
10
  import nltk
11
  import pyclip
12
  import pytesseract
13
  from nltk.tokenize import sent_tokenize
14
  from transformers import MarianMTModel, MarianTokenizer
 
 
15
 
16
  nltk.download('punkt')
17
 
18
- OCR_TR_DESCRIPTION = '''# OCR Translate v0.2
19
- <div id="content_align">OCR translation system based on Tesseract</div>'''
20
 
21
- # 图片路径
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  img_dir = "./data"
23
 
24
- # 获取tesseract语言列表
25
  choices = os.popen('tesseract --list-langs').read().split('\n')[1:-1]
26
 
27
 
28
- # 翻译模型选择
 
29
  def model_choice(src="en", trg="zh"):
30
  # https://huggingface.co/Helsinki-NLP/opus-mt-zh-en
31
  # https://huggingface.co/Helsinki-NLP/opus-mt-en-zh
32
- model_name = f"Helsinki-NLP/opus-mt-{src}-{trg}" # 模型名称
33
 
34
- tokenizer = MarianTokenizer.from_pretrained(model_name) # 分词器
35
- model = MarianMTModel.from_pretrained(model_name) # 模型
36
 
37
  return tokenizer, model
38
 
39
 
40
- # tesseract语言列表转pytesseract语言
41
  def ocr_lang(lang_list):
42
  lang_str = ""
43
  lang_len = len(lang_list)
@@ -51,18 +72,27 @@ def ocr_lang(lang_list):
51
  return lang_str
52
 
53
 
54
- # ocr tesseract
 
 
 
 
 
 
 
 
55
  def ocr_tesseract(img, languages):
56
- ocr_str = pytesseract.image_to_string(img, lang=ocr_lang(languages))
 
57
  return ocr_str
58
 
59
 
60
- # 清除
61
  def clear_content():
62
  return None
63
 
64
 
65
- # 复制到剪贴板
66
  def cp_text(input_text):
67
  # sudo apt-get install xclip
68
  try:
@@ -105,21 +135,36 @@ def translate(input_text, inputs_transStyle):
105
  return translate_text[2:]
106
 
107
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
108
  def main():
109
 
110
  with gr.Blocks(css='style.css') as ocr_tr:
111
  gr.Markdown(OCR_TR_DESCRIPTION)
112
 
113
  # -------------- OCR 文字提取 --------------
114
- with gr.Box():
115
 
116
  with gr.Row():
117
- gr.Markdown("### Step 01: Text Extraction")
118
 
119
  with gr.Row():
120
  with gr.Column():
121
  with gr.Row():
122
- inputs_img = gr.Image(image_mode="RGB", source="upload", type="pil", label="image")
123
  with gr.Row():
124
  inputs_lang = gr.CheckboxGroup(choices=["chi_sim", "eng"],
125
  type="value",
@@ -127,20 +172,31 @@ def main():
127
  label='language')
128
 
129
  with gr.Row():
130
- clear_img_btn = gr.Button('Clear')
131
- ocr_btn = gr.Button(value='OCR Extraction', variant="primary")
132
 
133
  with gr.Column():
134
  with gr.Row():
135
- outputs_text = gr.Textbox(label="Extract content", lines=20)
136
  with gr.Row():
137
  inputs_transStyle = gr.Radio(choices=["zh-en", "en-zh"],
138
  type="value",
139
  value="zh-en",
140
- label='translation mode')
141
  with gr.Row():
142
- clear_text_btn = gr.Button('Clear')
143
- translate_btn = gr.Button(value='Translate', variant="primary")
 
 
 
 
 
 
 
 
 
 
 
144
 
145
  with gr.Row():
146
  example_list = [["./data/test.png", ["eng"]], ["./data/test02.png", ["eng"]],
@@ -148,33 +204,44 @@ def main():
148
  gr.Examples(example_list, [inputs_img, inputs_lang], outputs_text, ocr_tesseract, cache_examples=False)
149
 
150
  # -------------- 翻译 --------------
151
- with gr.Box():
152
 
153
  with gr.Row():
154
- gr.Markdown("### Step 02: Translation")
155
 
156
  with gr.Row():
157
  outputs_tr_text = gr.Textbox(label="Translate Content", lines=20)
158
 
159
  with gr.Row():
160
- cp_clear_btn = gr.Button(value='Clear Clipboard')
161
- cp_btn = gr.Button(value='Copy to clipboard', variant="primary")
162
 
163
  # ---------------------- OCR Tesseract ----------------------
164
  ocr_btn.click(fn=ocr_tesseract, inputs=[inputs_img, inputs_lang], outputs=[
165
  outputs_text,])
166
  clear_img_btn.click(fn=clear_content, inputs=[], outputs=[inputs_img])
167
 
168
- # ---------------------- 翻译 ----------------------
 
 
 
 
 
 
 
 
169
  translate_btn.click(fn=translate, inputs=[outputs_text, inputs_transStyle], outputs=[outputs_tr_text])
170
  clear_text_btn.click(fn=clear_content, inputs=[], outputs=[outputs_text])
171
 
172
- # ---------------------- 复制到剪贴板 ----------------------
173
  cp_btn.click(fn=cp_text, inputs=[outputs_tr_text], outputs=[])
174
  cp_clear_btn.click(fn=cp_clear, inputs=[], outputs=[])
175
 
176
- ocr_tr.launch(inbrowser=True)
177
 
178
 
179
  if __name__ == '__main__':
180
  main()
 
 
 
 
1
+ # AI Meeting note parser
2
+ # Author:Alec Li
3
+ # Date:2024-01-26
4
+ # Location: Richmond Hospital Canada
5
 
6
  import os
7
 
8
  os.system("sudo apt-get install xclip")
9
 
10
+
11
  import gradio as gr
12
  import nltk
13
  import pyclip
14
  import pytesseract
15
  from nltk.tokenize import sent_tokenize
16
  from transformers import MarianMTModel, MarianTokenizer
17
+ import openai
18
+
19
 
20
  nltk.download('punkt')
21
 
 
 
22
 
23
+ OCR_TR_DESCRIPTION = '''
24
+ <div id="content_align">
25
+ <span style="color:darkred;font-size:32px;font-weight:bold">
26
+ 模多多会议记录总结神器
27
+ </span>
28
+ </div>
29
+
30
+ <div id="content_align">
31
+ <span style="color:blue;font-size:16px;font-weight:bold">
32
+ 会议记录拍照 -> 转文字 -> 翻译 -> 提炼会议纪要 -> 识别待办事项 -> 分配任务
33
+ </div>
34
+
35
+ <div id="content_align" style="margin-top: 10px;">
36
+ 作者: Dr. Alec Li
37
+ </div>
38
+ '''
39
+
40
+
41
+ # Image path
42
  img_dir = "./data"
43
 
44
+ # Get tesseract language list
45
  choices = os.popen('tesseract --list-langs').read().split('\n')[1:-1]
46
 
47
 
48
+
49
+ # Translation model selection
50
  def model_choice(src="en", trg="zh"):
51
  # https://huggingface.co/Helsinki-NLP/opus-mt-zh-en
52
  # https://huggingface.co/Helsinki-NLP/opus-mt-en-zh
53
+ model_name = f"Helsinki-NLP/opus-mt-{src}-{trg}" # Model name
54
 
55
+ tokenizer = MarianTokenizer.from_pretrained(model_name) # Tokenizer
56
+ model = MarianMTModel.from_pretrained(model_name) # model
57
 
58
  return tokenizer, model
59
 
60
 
61
+ # Convert tesseract language list to pytesseract language
62
  def ocr_lang(lang_list):
63
  lang_str = ""
64
  lang_len = len(lang_list)
 
72
  return lang_str
73
 
74
 
75
+ import pytesseract
76
+ import os
77
+
78
+ # Set Tesseract executable path in Colab virtal environment
79
+ pytesseract.pytesseract.tesseract_cmd = "/usr/bin/tesseract"
80
+
81
+ # Set up the Tesseract data directory
82
+ os.environ["TESSDATA_PREFIX"] = "/usr/share/tesseract-ocr/4.00/tessdata"
83
+
84
  def ocr_tesseract(img, languages):
85
+ custom_config = f'--oem 3 --psm 6 -l {ocr_lang(languages)}'
86
+ ocr_str = pytesseract.image_to_string(img, config=custom_config)
87
  return ocr_str
88
 
89
 
90
+ # Clear content
91
  def clear_content():
92
  return None
93
 
94
 
95
+ # copy to clipboard
96
  def cp_text(input_text):
97
  # sudo apt-get install xclip
98
  try:
 
135
  return translate_text[2:]
136
 
137
 
138
+ # 在 https://platform.openai.com/signup 注册并获取 API 密钥
139
+ openai.api_key = "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"
140
+
141
+ def generate_summary(text_input):
142
+ response = openai.ChatCompletion.create(
143
+ model="gpt-3.5-turbo",
144
+ messages=[
145
+ {"role": "system", "content": "You are a helpful assistant."},
146
+ {"role": "user", "content": text_input}
147
+ ]
148
+ )
149
+ summary = response["choices"][0]["message"]["content"].strip()
150
+ return summary
151
+
152
+
153
  def main():
154
 
155
  with gr.Blocks(css='style.css') as ocr_tr:
156
  gr.Markdown(OCR_TR_DESCRIPTION)
157
 
158
  # -------------- OCR 文字提取 --------------
159
+ with gr.Column():
160
 
161
  with gr.Row():
162
+ gr.Markdown("### Step 01: 文本抽取")
163
 
164
  with gr.Row():
165
  with gr.Column():
166
  with gr.Row():
167
+ inputs_img = gr.Image(image_mode="RGB", type="pil", label="image")
168
  with gr.Row():
169
  inputs_lang = gr.CheckboxGroup(choices=["chi_sim", "eng"],
170
  type="value",
 
172
  label='language')
173
 
174
  with gr.Row():
175
+ clear_img_btn = gr.Button('清除')
176
+ ocr_btn = gr.Button(value='图片文本抽取', variant="primary")
177
 
178
  with gr.Column():
179
  with gr.Row():
180
+ outputs_text = gr.Textbox(label="抽取的文本", lines=20)
181
  with gr.Row():
182
  inputs_transStyle = gr.Radio(choices=["zh-en", "en-zh"],
183
  type="value",
184
  value="zh-en",
185
+ label='翻译模式')
186
  with gr.Row():
187
+ clear_text_btn = gr.Button('清除')
188
+ translate_btn = gr.Button(value='翻译', variant="primary")
189
+
190
+ # Add a text box to display the generated summary
191
+ with gr.Row():
192
+ outputs_summary_text = gr.Textbox(label="生成的摘要", lines=20)
193
+
194
+
195
+ with gr.Row():
196
+ with gr.Row():
197
+ generate_summary_btn = gr.Button('生成摘要', variant="primary")
198
+ with gr.Row():
199
+ clear_summary_btn = gr.Button('清除摘要')
200
 
201
  with gr.Row():
202
  example_list = [["./data/test.png", ["eng"]], ["./data/test02.png", ["eng"]],
 
204
  gr.Examples(example_list, [inputs_img, inputs_lang], outputs_text, ocr_tesseract, cache_examples=False)
205
 
206
  # -------------- 翻译 --------------
207
+ with gr.Column():
208
 
209
  with gr.Row():
210
+ gr.Markdown("### Step 02: 翻译")
211
 
212
  with gr.Row():
213
  outputs_tr_text = gr.Textbox(label="Translate Content", lines=20)
214
 
215
  with gr.Row():
216
+ cp_clear_btn = gr.Button(value='清除剪贴板')
217
+ cp_btn = gr.Button(value='复制到剪贴板', variant="primary")
218
 
219
  # ---------------------- OCR Tesseract ----------------------
220
  ocr_btn.click(fn=ocr_tesseract, inputs=[inputs_img, inputs_lang], outputs=[
221
  outputs_text,])
222
  clear_img_btn.click(fn=clear_content, inputs=[], outputs=[inputs_img])
223
 
224
+
225
+ # ---------------------- Summarization ----------------------
226
+ # To update the click event of the button, use generate_summary directly
227
+ generate_summary_btn.click(fn=generate_summary, inputs=[outputs_text],
228
+ outputs=[outputs_summary_text])
229
+ clear_summary_btn.click(fn=clear_content, inputs=[], outputs=[outputs_summary_text])
230
+
231
+
232
+ # ---------------------- Translate ----------------------
233
  translate_btn.click(fn=translate, inputs=[outputs_text, inputs_transStyle], outputs=[outputs_tr_text])
234
  clear_text_btn.click(fn=clear_content, inputs=[], outputs=[outputs_text])
235
 
236
+ # ---------------------- Copy to clipboard ----------------------
237
  cp_btn.click(fn=cp_text, inputs=[outputs_tr_text], outputs=[])
238
  cp_clear_btn.click(fn=cp_clear, inputs=[], outputs=[])
239
 
240
+ ocr_tr.launch(inbrowser=True, share=True)
241
 
242
 
243
  if __name__ == '__main__':
244
  main()
245
+
246
+
247
+