DYDYLAN commited on
Commit
92da610
·
verified ·
1 Parent(s): 93e904f

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +107 -0
app.py ADDED
@@ -0,0 +1,107 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import pipeline
3
+ from diffusers import StableDiffusionPipeline
4
+ import torch
5
+
6
+
7
+ device = "cuda" if torch.cuda.is_available() else "cpu"
8
+
9
+ vision_pipe = pipeline(
10
+ "image-to-text",
11
+ model="nlpconnect/vit-gpt2-image-captioning",
12
+ device=0 if device == "cuda" else -1
13
+ )
14
+
15
+ text_pipe = pipeline(
16
+ "text2text-generation",
17
+ model="google/flan-t5-base",
18
+ max_length=256,
19
+ device=0 if device == "cuda" else -1
20
+ )
21
+
22
+ sd_model_id = "runwayml/stable-diffusion-v1-5"
23
+ sd_pipe = StableDiffusionPipeline.from_pretrained(
24
+ sd_model_id,
25
+ torch_dtype=torch.float16 if device == "cuda" else torch.float32
26
+ )
27
+ sd_pipe = sd_pipe.to(device)
28
+
29
+ def analyze_and_enhance(image, style, language):
30
+ if image is None:
31
+ return None, "请先上传图表。", "", None, "", ""
32
+
33
+ raw_caption = vision_pipe(image)[0]["generated_text"]
34
+
35
+ prompt_academic = (
36
+ "Rewrite the following description in a formal academic style, "
37
+ "suitable for a scientific paper. Be concise but precise.\n\n"
38
+ f"Description: {raw_caption}"
39
+ )
40
+ academic_desc = text_pipe(prompt_academic)[0]["generated_text"]
41
+
42
+ caption_prompt = (
43
+ "Write a 1-2 sentence figure caption for an academic paper, "
44
+ "based on this description:\n\n"
45
+ f"{academic_desc}\n\nCaption:"
46
+ )
47
+ caption_text = text_pipe(caption_prompt)[0]["generated_text"]
48
+
49
+ summary_prompt = (
50
+ "Write a short 3-4 sentence paragraph that explains the key trend "
51
+ "and message of this figure for the results section of a paper.\n\n"
52
+ f"{academic_desc}\n\nParagraph:"
53
+ )
54
+ summary_text = text_pipe(summary_prompt)[0]["generated_text"]
55
+
56
+ if language == "中文":
57
+ caption_text = text_pipe(
58
+ f"Translate this figure caption into Chinese:\n{caption_text}"
59
+ )[0]["generated_text"]
60
+ summary_text = text_pipe(
61
+ f"Translate this paragraph into Chinese:\n{summary_text}"
62
+ )[0]["generated_text"]
63
+ academic_desc_out = text_pipe(
64
+ f"Translate this academic description into Chinese:\n{academic_desc}"
65
+ )[0]["generated_text"]
66
+ else:
67
+ academic_desc_out = academic_desc
68
+
69
+ sd_prompt = (
70
+ f"{style} infographic style illustration of: {academic_desc} "
71
+ "minimal, clean, flat colors, suitable for a scientific slide."
72
+ )
73
+
74
+ with torch.autocast(device if device == "cuda" else "cpu"):
75
+ enhanced_image = sd_pipe(sd_prompt, num_inference_steps=25, guidance_scale=7.5).images[0]
76
+
77
+ return image, raw_caption, academic_desc_out, enhanced_image, caption_text, summary_text
78
+
79
+ with gr.Blocks() as demo:
80
+ gr.Markdown("# ChartSmith – AI 论文图表生成助手")
81
+
82
+ with gr.Row():
83
+ with gr.Column():
84
+ img_in = gr.Image(type="pil", label="上传你的学术图表(截图也可以)")
85
+ style = gr.Dropdown(
86
+ ["Formal academic", "Infographic", "Magazine-style"],
87
+ value="Formal academic",
88
+ label="重绘风格"
89
+ )
90
+ language = gr.Radio(["English", "中文"], value="English", label="输出语言")
91
+ btn = gr.Button("分析并美化图表")
92
+ with gr.Column():
93
+ orig_img = gr.Image(label="原始图表")
94
+ raw_caption_box = gr.Textbox(label="Step 2: 初步自动描述(Vision-LLM)")
95
+ academic_box = gr.Textbox(label="Step 3: 学术化解释(Academic Explanation)")
96
+ enhanced_img = gr.Image(label="Step 4: 美化 / 重绘后的图示")
97
+ caption_box = gr.Textbox(label="Step 5: 自动生成图注(Caption)")
98
+ summary_box = gr.Textbox(label="Step 5: 图表相关简短摘要 / 讨论")
99
+
100
+ btn.click(
101
+ analyze_and_enhance,
102
+ inputs=[img_in, style, language],
103
+ outputs=[orig_img, raw_caption_box, academic_box, enhanced_img, caption_box, summary_box]
104
+ )
105
+
106
+ if __name__ == "__main__":
107
+ demo.launch()