Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import pipeline
|
3 |
+
|
4 |
+
# 创建用于图像到文本的 pipeline
|
5 |
+
ocr_pipe = pipeline("image-to-text", model="microsoft/trocr-base-handwritten")
|
6 |
+
|
7 |
+
# 创建用于语法纠正的 pipeline
|
8 |
+
grammar_correction_pipe = pipeline("text2text-generation", model="flexudy/t5-base-multi-sentence-doctor")
|
9 |
+
|
10 |
+
def process_image_and_correct_grammar(input_image):
|
11 |
+
# 使用 OCR 模型识别图像中的文本
|
12 |
+
ocr_results = ocr_pipe(input_image)
|
13 |
+
recognized_text = ocr_results[0]['generated_text']
|
14 |
+
|
15 |
+
# 使用语法纠正模型纠正文本
|
16 |
+
corrected_text_results = grammar_correction_pipe(recognized_text)
|
17 |
+
corrected_text = corrected_text_results[0]['generated_text']
|
18 |
+
|
19 |
+
# 返回原始文本和纠正后的文本
|
20 |
+
return {"Original Text": recognized_text, "Corrected Text": corrected_text}
|
21 |
+
|
22 |
+
# 创建 Gradio 接口
|
23 |
+
iface = gr.Interface(
|
24 |
+
fn=process_image_and_correct_grammar, # 指定处理函数
|
25 |
+
inputs=gr.inputs.Image(type='pil', label="Upload your image here or use your camera"), # 设置输入为图片
|
26 |
+
outputs=[gr.outputs.Textbox(label="Original Text"), gr.outputs.Textbox(label="Corrected Text")], # 设置输出为两个文本框
|
27 |
+
title="OCR and Grammar Correction", # 接口标题
|
28 |
+
description="Upload an image and the text in the image will be recognized and grammatically corrected." # 接口描述
|
29 |
+
)
|
30 |
+
|
31 |
+
# 启动 Gradio 应用
|
32 |
+
iface.launch()
|