SoybeanMilk commited on
Commit
c552435
1 Parent(s): ce9f569

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -12
app.py CHANGED
@@ -18,11 +18,6 @@ OCR_TR_DESCRIPTION = '''# OCR Translate v0.2
18
  # Getting the list of available languages for Tesseract
19
  choices = os.popen('tesseract --list-langs').read().split('\n')[1:-1]
20
 
21
- # Replace YOUR_API_KEY with your API key from https://cloud.google.com/docs/authentication/api-keys
22
- genai.configure(api_key="AIzaSyC_o3nrbe2fVUifhUg0Zh2oVsweSIdlEbg")
23
- model = genai.GenerativeModel('gemini-pro')
24
-
25
-
26
  # tesseract语言列表转pytesseract语言
27
  def ocr_lang(lang_list):
28
  lang_str = ""
@@ -63,13 +58,13 @@ def cp_clear():
63
  pyperclip.clear()
64
 
65
  # Add a translation function
66
- def translate(input_text, inputs_transStyle):
 
 
67
  if input_text is None or input_text == "":
68
  return "System prompt: There is no content to translate!"
69
 
70
- # 选择翻译模型
71
- prompt = f"Please reformat the following article to have clear paragraph breaks and correct punctuation, and then translate it into {inputs_transStyle}. In the translation, do not display the original text, fictional content, or any repeated content. Ensure that the original meaning and context are preserved as much as possible."
72
- # Reorder for ease of reading and translate into {inputs_transStyle}"
73
  response = model.generate_content([prompt, input_text])
74
  return response.text
75
 
@@ -98,15 +93,19 @@ def main():
98
  clear_img_btn = gr.Button('Clear')
99
  ocr_btn = gr.Button(value='OCR Extraction', variant="primary")
100
 
 
 
 
101
  with gr.Column():
102
  with gr.Row():
103
  outputs_text = gr.Textbox(label="Extract content", lines=20)
104
  inputs_transStyle = gr.inputs.Dropdown(choices=["Chinese (Simplified)", "Chinese (Traditional)", "English", "Japanese", "Korean"],
105
- default="Chinese (Simplified)", label='translation mode')
106
  with gr.Row():
107
  clear_text_btn = gr.Button('Clear')
108
  translate_btn = gr.Button(value='Translate', variant="primary")
109
 
 
110
  with gr.Row():
111
  pass
112
 
@@ -129,14 +128,14 @@ def main():
129
  clear_img_btn.click(fn=clear_content, inputs=[], outputs=[inputs_img])
130
 
131
  # ---------------------- 翻译 ----------------------
132
- translate_btn.click(fn=translate, inputs=[outputs_text, inputs_transStyle], outputs=[outputs_tr_text])
133
  clear_text_btn.click(fn=clear_content, inputs=[], outputs=[outputs_text])
134
 
135
  # ---------------------- 复制到剪贴板 ----------------------
136
  cp_btn.click(fn=cp_text, inputs=[outputs_tr_text], outputs=[])
137
  cp_clear_btn.click(fn=cp_clear, inputs=[], outputs=[])
138
 
139
- ocr_tr.launch(inbrowser=True)
140
 
141
  if __name__ == '__main__':
142
  main()
 
18
  # Getting the list of available languages for Tesseract
19
  choices = os.popen('tesseract --list-langs').read().split('\n')[1:-1]
20
 
 
 
 
 
 
21
  # tesseract语言列表转pytesseract语言
22
  def ocr_lang(lang_list):
23
  lang_str = ""
 
58
  pyperclip.clear()
59
 
60
  # Add a translation function
61
+ def translate(api_key, input_text, inputs_transStyle):
62
+ genai.configure(api_key=api_key)
63
+ model = genai.GenerativeModel('gemini-pro')
64
  if input_text is None or input_text == "":
65
  return "System prompt: There is no content to translate!"
66
 
67
+ prompt = f"Please reformat the following article to have clear paragraph breaks and correct punctuation. Then, translate it into {inputs_transStyle}, ensuring that the original meaning and context are preserved as much as possible. In the translation, ensure that no content is repeated."
 
 
68
  response = model.generate_content([prompt, input_text])
69
  return response.text
70
 
 
93
  clear_img_btn = gr.Button('Clear')
94
  ocr_btn = gr.Button(value='OCR Extraction', variant="primary")
95
 
96
+ with gr.Row():
97
+ inputs_api_key = gr.inputs.Textbox(label='API: https://makersuite.google.com/u/1/app/apikey', type='password')
98
+
99
  with gr.Column():
100
  with gr.Row():
101
  outputs_text = gr.Textbox(label="Extract content", lines=20)
102
  inputs_transStyle = gr.inputs.Dropdown(choices=["Chinese (Simplified)", "Chinese (Traditional)", "English", "Japanese", "Korean"],
103
+ default="Chinese (Traditional)", label='translation mode')
104
  with gr.Row():
105
  clear_text_btn = gr.Button('Clear')
106
  translate_btn = gr.Button(value='Translate', variant="primary")
107
 
108
+
109
  with gr.Row():
110
  pass
111
 
 
128
  clear_img_btn.click(fn=clear_content, inputs=[], outputs=[inputs_img])
129
 
130
  # ---------------------- 翻译 ----------------------
131
+ translate_btn.click(fn=translate, inputs=[inputs_api_key, outputs_text, inputs_transStyle], outputs=[outputs_tr_text])
132
  clear_text_btn.click(fn=clear_content, inputs=[], outputs=[outputs_text])
133
 
134
  # ---------------------- 复制到剪贴板 ----------------------
135
  cp_btn.click(fn=cp_text, inputs=[outputs_tr_text], outputs=[])
136
  cp_clear_btn.click(fn=cp_clear, inputs=[], outputs=[])
137
 
138
+ ocr_tr.launch(inbrowser=True, share=True)
139
 
140
  if __name__ == '__main__':
141
  main()