MashiroLn commited on
Commit
b44ae07
·
verified ·
1 Parent(s): eb96659

Upload folder using huggingface_hub

Browse files
Files changed (4) hide show
  1. app.py +29 -0
  2. apps/pdf_cropper.py +58 -0
  3. apps/text_tools.py +16 -0
  4. requirements.txt +4 -0
app.py ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from apps import pdf_cropper, text_tools
3
+
4
+ # --- 主程序入口 ---
5
+ # 这里是“应用集市”的容器。
6
+ # 每次添加新工具,只需要 import 进来,并在 tab_list 中注册即可。
7
+
8
+ def create_main_interface():
9
+ with gr.Blocks(title="我的科研工具箱", theme=gr.themes.Soft()) as main_app:
10
+ gr.Markdown("# 🛠️ 科研效率工具箱")
11
+
12
+ # 使用 Tab 布局来切换不同的工具
13
+ with gr.Tabs():
14
+
15
+ # --- 工具 1: PDF 智能裁边 ---
16
+ with gr.TabItem("📄 PDF 裁边"):
17
+ pdf_cropper.create_ui()
18
+
19
+ # --- 工具 2: 文本分析 (示例) ---
20
+ with gr.TabItem("📝 文本统计"):
21
+ text_tools.create_ui()
22
+
23
+ # --- 可以在这里继续添加更多 Tab ---
24
+
25
+ return main_app
26
+
27
+ if __name__ == "__main__":
28
+ app = create_main_interface()
29
+ app.launch(inbrowser=True)
apps/pdf_cropper.py ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from PIL import Image, ImageChops
3
+ import img2pdf
4
+ import io
5
+
6
+ # --- 核心逻辑 (复用之前的代码) ---
7
+ def trim_whitespace(im, fuzz_level=20):
8
+ bg = Image.new(im.mode, im.size, (255, 255, 255))
9
+ diff = ImageChops.difference(im, bg)
10
+ diff = ImageChops.add(diff, diff, 1, -fuzz_level)
11
+ bbox = diff.getbbox()
12
+ if bbox:
13
+ return im.crop(bbox)
14
+ return im
15
+
16
+ def process_pipeline(files, quality, fuzz_level, progress=gr.Progress()):
17
+ if not files: return None
18
+ pdf_components = []
19
+ for filepath in progress.tqdm(files, desc="处理中"):
20
+ try:
21
+ with Image.open(filepath) as img:
22
+ if img.mode in ('RGBA', 'LA') or (img.mode == 'P' and 'transparency' in img.info):
23
+ bg = Image.new('RGB', img.size, (255, 255, 255))
24
+ if img.mode!= 'RGBA': img = img.convert('RGBA')
25
+ bg.paste(img, mask=img.split()[3])
26
+ img = bg
27
+ else:
28
+ img = img.convert('RGB')
29
+
30
+ trimmed = trim_whitespace(img, fuzz_level)
31
+ byte_arr = io.BytesIO()
32
+ trimmed.save(byte_arr, format='JPEG', quality=int(quality))
33
+ pdf_components.append(byte_arr.getvalue())
34
+ except Exception: continue
35
+
36
+ if not pdf_components: return None
37
+ output_filename = "output_document.pdf"
38
+ with open(output_filename, "wb") as f:
39
+ f.write(img2pdf.convert(pdf_components))
40
+ return output_filename
41
+
42
+ # --- 模块化 UI 接口 ---
43
+ def create_ui():
44
+ """
45
+ 每个工具模块都需要暴露这个函数。
46
+ 注意:不要在这里创建 gr.Blocks(),直接写 Row/Column 即可,
47
+ 因为它们会被嵌入到主程序的 Tab 中。
48
+ """
49
+ with gr.Row():
50
+ with gr.Column():
51
+ file_input = gr.File(file_count="multiple", file_types=["image"], label="上传图片")
52
+ quality = gr.Slider(10, 100, 90, label="质量")
53
+ fuzz = gr.Slider(0, 100, 30, label="容差")
54
+ btn = gr.Button("开始处理", variant="primary")
55
+ with gr.Column():
56
+ output = gr.File(label="下载 PDF")
57
+
58
+ btn.click(process_pipeline, [file_input, quality, fuzz], output)
apps/text_tools.py ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+
3
+ def analyze_text(text):
4
+ return {
5
+ "字符数": len(text),
6
+ "单词数 (空格分隔)": len(text.split()),
7
+ "行数": len(text.splitlines())
8
+ }
9
+
10
+ def create_ui():
11
+ with gr.Row():
12
+ inp = gr.Textbox(lines=5, label="输入文本")
13
+ out = gr.JSON(label="统计结果")
14
+
15
+ btn = gr.Button("分析")
16
+ btn.click(analyze_text, inp, out)
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ gradio
2
+ Pillow
3
+ img2pdf
4
+ huggingface_hub