File size: 10,290 Bytes
c6d26b8 6c60394 c6d26b8 6c60394 c6d26b8 6c60394 9e4aac4 c6d26b8 6c60394 c6d26b8 5f1dbf2 c6d26b8 6c60394 c6d26b8 6c60394 c6d26b8 6c60394 c6d26b8 6c60394 c6d26b8 6c60394 c6d26b8 aa0d25a 581b5dc aa0d25a 581b5dc 5f1dbf2 581b5dc aa0d25a 581b5dc aa0d25a 581b5dc aa0d25a 581b5dc aa0d25a 581b5dc 5f1dbf2 9e4aac4 5f1dbf2 aa0d25a 5f1dbf2 aa0d25a 5f1dbf2 aa0d25a 5f1dbf2 c6d26b8 e59da9c c6d26b8 e59da9c c6d26b8 c552435 146a1c2 c552435 146a1c2 581b5dc c6d26b8 5f1dbf2 c6d26b8 e59da9c 5f1dbf2 c6d26b8 c552435 c6d26b8 6c60394 c6d26b8 5f1dbf2 c6d26b8 5f1dbf2 c6d26b8 e59da9c c6d26b8 5f1dbf2 c6d26b8 aa0d25a c6d26b8 2bd6ff5 c6d26b8 962af02 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 |
import os
import pyperclip
import gradio as gr
import nltk
import pytesseract
import google.generativeai as genai
from nltk.tokenize import sent_tokenize
from transformers import *
import torch
from tqdm import tqdm # Import tqdm
import time
# Download necessary data for nltk
nltk.download('punkt')
OCR_TR_DESCRIPTION = '''# OCR Translate and Summary GeminiPro
<div id="content_align">OCR system based on Tesseract</div>'''
# Getting the list of available languages for Tesseract
choices = os.popen('tesseract --list-langs').read().split('\n')[1:-1]
# tesseract语言列表转pytesseract语言
def ocr_lang(lang_list):
lang_str = ""
lang_len = len(lang_list)
if lang_len == 1:
return lang_list[0]
else:
for i in range(lang_len):
lang_list.insert(lang_len - i, "+")
lang_str = "".join(lang_list[:-1])
return lang_str
# ocr tesseract
def ocr_tesseract(img, languages):
ocr_str = pytesseract.image_to_string(img, lang=ocr_lang(languages))
return ocr_str
# 清除
def clear_content():
return None
import pyperclip
# 复制到剪贴板
def cp_text(input_text):
try:
pyperclip.copy(input_text)
except Exception as e:
print("Error occurred while copying to clipboard")
print(e)
# 清除剪贴板
def cp_clear():
pyperclip.clear()
# Split the text into 2000 character chunks
def process_text_input_text(input_text):
# Split the text into 2000 character chunks
chunks = [input_text[i:i+2000] for i in range(0, len(input_text), 2000)]
return chunks
def process_and_translate(api_key, input_text, src_lang, tgt_lang):
# Process the input text into chunks
chunks = process_text_input_text(input_text)
# Translate each chunk and collect the results
translated_chunks = []
for chunk in chunks:
if chunk is None or chunk == "":
translated_chunks.append("System prompt: There is no content to translate!")
else:
prompt = f"This is an {src_lang} to {tgt_lang} translation, please provide the {tgt_lang} translation for this paragraph. Do not provide any explanations or text apart from the translation.\n{src_lang}: "
#prompt = f"This is an {src_lang} to {tgt_lang} translation, please provide the {tgt_lang} translation for this sentence. Do not provide any explanations or text apart from the translation.\n{src_lang}: "
genai.configure(api_key=api_key)
generation_config = {
"candidateCount": 1,
"maxOutputTokens": 2048,
"temperature": 0.3,
"topP": 1
}
safety_settings = [
{
"category": "HARM_CATEGORY_HARASSMENT",
"threshold": "BLOCK_NONE",
},
{
"category": "HARM_CATEGORY_HATE_SPEECH",
"threshold": "BLOCK_NONE",
},
{
"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
"threshold": "BLOCK_NONE",
},
{
"category": "HARM_CATEGORY_DANGEROUS_CONTENT",
"threshold": "BLOCK_NONE",
},
]
model = genai.GenerativeModel(model_name='gemini-pro')
response = model.generate_content([prompt, chunk],
#generation_config=generation_config,
safety_settings=safety_settings
)
translated_chunks.append(response.text)
# Join the translated chunks back together into a single string
response = '\n\n'.join(translated_chunks)
return response
def process_and_summary(api_key, input_text, src_lang, tgt_lang):
# Process the input text into chunks
chunks = process_text_input_text(input_text)
time.sleep(30)
# Translate each chunk and collect the results
translated_chunks = []
for chunk in chunks:
if chunk is None or chunk == "":
translated_chunks.append("System prompt: There is no content to translate!")
else:
prompt = f"This is an {src_lang} to {tgt_lang} summarization and knowledge key points, please provide the {tgt_lang} summarization and list the {tgt_lang} knowledge key points for this sentence. Do not provide any explanations or text apart from the summarization.\n{src_lang}: "
genai.configure(api_key=api_key)
generation_config = {
"candidateCount": 1,
"maxOutputTokens": 2048,
"temperature": 0.3,
"topP": 1
}
safety_settings = [
{
"category": "HARM_CATEGORY_HARASSMENT",
"threshold": "BLOCK_NONE",
},
{
"category": "HARM_CATEGORY_HATE_SPEECH",
"threshold": "BLOCK_NONE",
},
{
"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
"threshold": "BLOCK_NONE",
},
{
"category": "HARM_CATEGORY_DANGEROUS_CONTENT",
"threshold": "BLOCK_NONE",
},
]
model = genai.GenerativeModel(model_name='gemini-pro')
response = model.generate_content([prompt, chunk],
#generation_config=generation_config,
safety_settings=safety_settings
)
translated_chunks.append(response.text)
# Join the translated chunks back together into a single string
response = '\n\n*Next Paragraph*\n\n'.join(translated_chunks)
return response
# prompt = f"Display language is {tgt_lang}, do not display original text, As a Knowledge Video Content Analysis Expert, specialize in analyzing knowledge videos, identifying and clearly explaining key points in {tgt_lang}, ensuring accurate, easy-to-understand summaries suitable for diverse audiences, analyze, list key points, and explain detailedly below text: "
def main():
with gr.Blocks(css='style.css') as ocr_tr:
gr.Markdown(OCR_TR_DESCRIPTION)
# -------------- OCR 文字提取 --------------
with gr.Box():
with gr.Row():
gr.Markdown("### Step 01: Text Extraction")
with gr.Row():
with gr.Column():
with gr.Row():
inputs_img = gr.Image(image_mode="RGB", source="upload", type="pil", label="image")
with gr.Row():
inputs_lang = gr.CheckboxGroup(choices=["chi_sim", "eng"],
type="value",
value=['eng'],
label='language')
with gr.Row():
clear_img_btn = gr.Button('Clear')
ocr_btn = gr.Button(value='OCR Extraction', variant="primary")
with gr.Row():
# Use Markdown to display clickable URL
gr.Markdown("[Click here to get API key](https://makersuite.google.com/u/1/app/apikey)")
with gr.Row():
# Create a text input box for users to enter their API key
inputs_api_key = gr.Textbox(label="Please enter your API key here", type="password")
with gr.Column():
with gr.Row():
outputs_text = gr.Textbox(label="Extract content", lines=20)
src_lang = gr.inputs.Dropdown(choices=["Chinese (Simplified)", "Chinese (Traditional)", "English", "Japanese", "Korean"],
default="English", label='source language')
tgt_lang = gr.inputs.Dropdown(choices=["Chinese (Simplified)", "Chinese (Traditional)", "English", "Japanese", "Korean"],
default="Chinese (Traditional)", label='target language')
with gr.Row():
clear_text_btn = gr.Button('Clear')
translate_btn = gr.Button(value='Translate', variant="primary")
summary_btn = gr.Button(value='Summary', variant="primary")
with gr.Row():
pass
# -------------- 翻译 --------------
with gr.Box():
with gr.Row():
gr.Markdown("### Step 02: Process")
with gr.Row():
outputs_tr_text = gr.Textbox(label="Process Content", lines=20)
with gr.Row():
cp_clear_btn = gr.Button(value='Clear Clipboard')
cp_btn = gr.Button(value='Copy to clipboard', variant="primary")
# ---------------------- OCR Tesseract ----------------------
ocr_btn.click(fn=ocr_tesseract, inputs=[inputs_img, inputs_lang], outputs=[
outputs_text,])
clear_img_btn.click(fn=clear_content, inputs=[], outputs=[inputs_img])
# ---------------------- 翻译 ----------------------
translate_btn.click(fn=process_and_translate, inputs=[inputs_api_key, outputs_text, src_lang, tgt_lang], outputs=[outputs_tr_text])
summary_btn.click(fn=process_and_summary, inputs=[inputs_api_key, outputs_text, src_lang, tgt_lang], outputs=[outputs_tr_text])
clear_text_btn.click(fn=clear_content, inputs=[], outputs=[outputs_text])
# ---------------------- 复制到剪贴板 ----------------------
cp_btn.click(fn=cp_text, inputs=[outputs_tr_text], outputs=[])
cp_clear_btn.click(fn=cp_clear, inputs=[], outputs=[])
ocr_tr.launch(inbrowser=True)
if __name__ == '__main__':
main() |