Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,49 @@
|
|
1 |
import gradio as gr
|
|
|
2 |
|
3 |
-
|
4 |
-
|
5 |
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
import easyocr
|
3 |
|
4 |
+
# 创建一个 EasyOCR 阅读器实例
|
5 |
+
reader = easyocr.Reader(['en']) # 选择语言,这里选择英文
|
6 |
|
7 |
+
def toggle_visibility(input_type):
|
8 |
+
"""根据输入类型控制文本框和文件上传控件的可见性"""
|
9 |
+
user_input_visible = input_type == "Text Input"
|
10 |
+
file_upload_visible = input_type == "File Upload"
|
11 |
+
return gr.update(visible=user_input_visible), gr.update(visible=file_upload_visible)
|
12 |
+
|
13 |
+
def process_input(input_type, user_input, uploaded_file):
|
14 |
+
print('ooooocr')
|
15 |
+
if input_type == "File Upload" and uploaded_file is not None:
|
16 |
+
# 读取上传的文件
|
17 |
+
with open(uploaded_file.name, "rb") as f:
|
18 |
+
image = f.read()
|
19 |
+
results = reader.readtext(image)
|
20 |
+
# 提取识别的文本
|
21 |
+
extracted_text = ' '.join([text[1] for text in results])
|
22 |
+
print("提取的文本:")
|
23 |
+
print(extracted_text)
|
24 |
+
return extracted_text
|
25 |
+
elif input_type == "Text Input":
|
26 |
+
return user_input
|
27 |
+
|
28 |
+
demo = gr.Blocks()
|
29 |
+
|
30 |
+
with demo:
|
31 |
+
gr.Markdown("# Step 1: Generate the keys")
|
32 |
+
b_gen_key_and_install = gr.Button("Generate the keys and send public part to server")
|
33 |
+
evaluation_key = gr.Textbox(label="Evaluation key (truncated):", max_lines=4, interactive=False)
|
34 |
+
user_id = gr.Textbox(label="", max_lines=4, interactive=False, visible=False)
|
35 |
+
|
36 |
+
gr.Markdown("# Step 2: Choose Input Method")
|
37 |
+
input_type = gr.Radio(choices=["Text Input", "File Upload"], label="Select Input Method")
|
38 |
+
user_input = gr.Textbox(label="Enter Text", placeholder="Type here...", visible=False) # Initially hidden
|
39 |
+
file_upload = gr.File(label="Upload File", file_types=[".jpg", ".png"], visible=False) # Initially hidden
|
40 |
+
|
41 |
+
# 使用change事件来调用toggle_visibility函数
|
42 |
+
input_type.change(toggle_visibility, inputs=input_type, outputs=[user_input, file_upload])
|
43 |
+
|
44 |
+
submit_button = gr.Button("Submit")
|
45 |
+
output_text = gr.Textbox(label="Extracted Text")
|
46 |
+
|
47 |
+
submit_button.click(process_input, inputs=[input_type, user_input, file_upload], outputs=output_text)
|
48 |
+
|
49 |
+
demo.launch()
|