Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,166 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
import os
|
4 |
+
from PIL import Image
|
5 |
+
import tempfile
|
6 |
+
import subprocess
|
7 |
+
import sys
|
8 |
+
|
9 |
+
# 安装MonkeyOCR
|
10 |
+
def install_monkeyocr():
|
11 |
+
"""安装MonkeyOCR依赖"""
|
12 |
+
try:
|
13 |
+
subprocess.check_call([
|
14 |
+
sys.executable, "-m", "pip", "install",
|
15 |
+
"git+https://github.com/Yuliang-Liu/MonkeyOCR.git"
|
16 |
+
])
|
17 |
+
return True
|
18 |
+
except:
|
19 |
+
return False
|
20 |
+
|
21 |
+
# 在启动时安装
|
22 |
+
if not install_monkeyocr():
|
23 |
+
print("Failed to install MonkeyOCR")
|
24 |
+
|
25 |
+
try:
|
26 |
+
from monkeyocr import MonkeyOCR
|
27 |
+
except ImportError:
|
28 |
+
print("MonkeyOCR not available, using placeholder")
|
29 |
+
|
30 |
+
class MonkeyOCRDemo:
|
31 |
+
def __init__(self):
|
32 |
+
self.model = None
|
33 |
+
self.load_model()
|
34 |
+
|
35 |
+
def load_model(self):
|
36 |
+
"""加载MonkeyOCR模型"""
|
37 |
+
try:
|
38 |
+
# 这里需要根据实际的MonkeyOCR API调整
|
39 |
+
self.model = MonkeyOCR.from_pretrained("echo840/MonkeyOCR")
|
40 |
+
print("MonkeyOCR模型加载成功")
|
41 |
+
except Exception as e:
|
42 |
+
print(f"模型加载失败: {e}")
|
43 |
+
self.model = None
|
44 |
+
|
45 |
+
def parse_document(self, file_input, prompt="解析这个文档"):
|
46 |
+
"""解析文档"""
|
47 |
+
if self.model is None:
|
48 |
+
return "模型未加载,请稍后再试", "模型加载失败"
|
49 |
+
|
50 |
+
try:
|
51 |
+
# 处理上传的文件
|
52 |
+
if file_input is None:
|
53 |
+
return "请上传一个文件", "错误:没有文件"
|
54 |
+
|
55 |
+
# 根据文件类型处理
|
56 |
+
file_path = file_input.name if hasattr(file_input, 'name') else file_input
|
57 |
+
|
58 |
+
# 调用MonkeyOCR进行解析
|
59 |
+
# 这里需要根据实际的API调整
|
60 |
+
result = self.model.parse(file_path)
|
61 |
+
|
62 |
+
# 返回解析结果
|
63 |
+
markdown_result = result.get('markdown', '解析失败')
|
64 |
+
status = "解析成功"
|
65 |
+
|
66 |
+
return markdown_result, status
|
67 |
+
|
68 |
+
except Exception as e:
|
69 |
+
error_msg = f"解析过程中出现错误: {str(e)}"
|
70 |
+
return error_msg, "错误"
|
71 |
+
|
72 |
+
# 创建Demo实例
|
73 |
+
demo_instance = MonkeyOCRDemo()
|
74 |
+
|
75 |
+
def parse_with_monkeyocr(file_input, prompt="解析文档内容"):
|
76 |
+
"""Gradio包装函数"""
|
77 |
+
return demo_instance.parse_document(file_input, prompt)
|
78 |
+
|
79 |
+
# 创建Gradio界面
|
80 |
+
def create_interface():
|
81 |
+
with gr.Blocks(title="MonkeyOCR Document Parser", theme=gr.themes.Soft()) as demo:
|
82 |
+
gr.Markdown("""
|
83 |
+
# 🐵 MonkeyOCR 文档解析器
|
84 |
+
|
85 |
+
MonkeyOCR采用Structure-Recognition-Relation (SRR)三元组范式,能够高效解析中英文文档。
|
86 |
+
|
87 |
+
**支持格式**: PDF, PNG, JPG, JPEG
|
88 |
+
**功能**: 文本识别、公式解析、表格提取、结构化输出
|
89 |
+
""")
|
90 |
+
|
91 |
+
with gr.Row():
|
92 |
+
with gr.Column(scale=1):
|
93 |
+
file_input = gr.File(
|
94 |
+
label="上传文档",
|
95 |
+
file_types=[".pdf", ".png", ".jpg", ".jpeg"],
|
96 |
+
type="filepath"
|
97 |
+
)
|
98 |
+
|
99 |
+
prompt_input = gr.Textbox(
|
100 |
+
label="提示词(可选)",
|
101 |
+
placeholder="请描述您希望如何解析这个文档...",
|
102 |
+
value="解析这个文档的所有内容,包括文本、表格和公式",
|
103 |
+
lines=2
|
104 |
+
)
|
105 |
+
|
106 |
+
parse_btn = gr.Button("🚀 开始解析", variant="primary", size="lg")
|
107 |
+
|
108 |
+
gr.Markdown("""
|
109 |
+
### 使用说明
|
110 |
+
1. 上传PDF文档或图片
|
111 |
+
2. 输入解析提示词(可选)
|
112 |
+
3. 点击"开始解析"按钮
|
113 |
+
4. 等待解析完成,查看结果
|
114 |
+
""")
|
115 |
+
|
116 |
+
with gr.Column(scale=2):
|
117 |
+
status_output = gr.Textbox(
|
118 |
+
label="解析状态",
|
119 |
+
value="等待上传文件...",
|
120 |
+
interactive=False
|
121 |
+
)
|
122 |
+
|
123 |
+
result_output = gr.Textbox(
|
124 |
+
label="解析结果(Markdown格式)",
|
125 |
+
lines=20,
|
126 |
+
max_lines=30,
|
127 |
+
show_copy_button=True
|
128 |
+
)
|
129 |
+
|
130 |
+
# 示例文件
|
131 |
+
gr.Examples(
|
132 |
+
examples=[
|
133 |
+
["./examples/sample.pdf", "解析这个学术论文"],
|
134 |
+
["./examples/table.png", "提取表格数据"],
|
135 |
+
["./examples/formula.jpg", "识别数学公式"],
|
136 |
+
],
|
137 |
+
inputs=[file_input, prompt_input],
|
138 |
+
outputs=[result_output, status_output],
|
139 |
+
fn=parse_with_monkeyocr,
|
140 |
+
cache_examples=False
|
141 |
+
)
|
142 |
+
|
143 |
+
# 绑定事件
|
144 |
+
parse_btn.click(
|
145 |
+
fn=parse_with_monkeyocr,
|
146 |
+
inputs=[file_input, prompt_input],
|
147 |
+
outputs=[result_output, status_output]
|
148 |
+
)
|
149 |
+
|
150 |
+
# 文件上传时的处理
|
151 |
+
file_input.change(
|
152 |
+
fn=lambda x: "文件已上传,请点击解析按钮" if x else "等待上传文件...",
|
153 |
+
inputs=[file_input],
|
154 |
+
outputs=[status_output]
|
155 |
+
)
|
156 |
+
|
157 |
+
return demo
|
158 |
+
|
159 |
+
# 启动应用
|
160 |
+
if __name__ == "__main__":
|
161 |
+
demo = create_interface()
|
162 |
+
demo.launch(
|
163 |
+
server_name="0.0.0.0",
|
164 |
+
server_port=7860,
|
165 |
+
share=False
|
166 |
+
)
|