Enoch commited on
Commit
e7c6dbc
·
1 Parent(s): 813eee5

Added my Python app

Browse files
Files changed (1) hide show
  1. app.py +268 -0
app.py ADDED
@@ -0,0 +1,268 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from openai import OpenAI
3
+ import gradio as gr
4
+ # 删除原有的 openai.api_key 设置
5
+ # 替换为新的客户端初始化
6
+
7
+ client = OpenAI(
8
+ api_key=os.getenv("DASHSCOPE_API_KEY"),
9
+ base_url="https://dashscope.aliyuncs.com/compatible-mode/v1"
10
+ )
11
+
12
+ # 修改 call_openai_api 函数
13
+ def call_openai_api(prompt, temperature=0.7):
14
+ chat_completion = client.chat.completions.create(
15
+ messages=[
16
+ {
17
+ "role": "system",
18
+ "content": "你是一个专业的专利代理人,擅长撰写专利文档。",
19
+ },
20
+ {
21
+ "role": "user",
22
+ "content": prompt,
23
+ },
24
+ ],
25
+ model="qwen-plus",
26
+ temperature=temperature,
27
+ max_tokens=3000,
28
+ n=1
29
+ )
30
+ return chat_completion.choices[0].message.content.strip()
31
+
32
+ # 定义每个部分的Agent Prompt模版
33
+ # 在实际使用时,需根据您想要的生成要求对这些模版进行详细调整
34
+ BACKGROUND_PROMPT = """
35
+ 你是一名资深的专利代理人,现在需要根据以下输入信息来撰写一份专利申请技术交底书的第一部分内容。
36
+ 请根据用户提供的背景材料和参考文献信息,详细说明本发明所处的技术领域背景和已有技术方案。
37
+
38
+ 要求:
39
+ 1. 内容必须清晰、详实,包括宏观技术背景和具体现有技术方案。
40
+ 2. 若有参考文献(专利、期刊、书籍)请在文中标明出处。
41
+ 3. 内容可读性强,让读者无需查阅外部文献即可理解。
42
+
43
+ 用户提供的信息:
44
+ {user_input}
45
+
46
+ 请按照如下格式输出:
47
+ 一、详细介绍技术背景,并描述已有的与本发明最相近似的实现方案。
48
+ 1.背景技术
49
+ 2.现有技术方案详细介绍
50
+ """
51
+
52
+ SHORTCOMING_PROMPT = """
53
+ 请根据以下背景与已有技术方案描述,以及用户给出的需求信息,撰写第二部分内容:
54
+ 二、现有技术的缺点是什么?
55
+ 要求:
56
+ - 针对上部分已有技术方案,指出目前存在的不足、问题点或局限性。
57
+ - 可根据用户输入信息进行适当扩展。
58
+
59
+ 现有信息(第一部分输出):
60
+ {previous_content}
61
+
62
+ 用户提供的需求:
63
+ {user_input}
64
+ """
65
+
66
+ PROBLEM_PROMPT = """
67
+ 请根据以下信息编写第三部分内容:
68
+ 三、本发明决的技术问题是:
69
+ 要求:
70
+ - 针对第二部分所列出的缺点和不足,描述本发明试图解决的具体技术问题。
71
+ - 内容应与本发明拟解决的问题相呼应。
72
+
73
+ 已有输出:
74
+ 第一部分:
75
+ {background_content}
76
+
77
+ 第二部分:
78
+ {shortcoming_content}
79
+
80
+ 用户提供的需求:
81
+ {user_input}
82
+ """
83
+
84
+ SOLUTION_PROMPT = """
85
+ 请根据以下信息撰写第四部分内容:
86
+ 四、本发明技术方案的详细阐述:
87
+ 要求:
88
+ - 详细描述本发明的技术方案,包括各功能模块/步骤的技术实现方式、结构图(可文字描述)和原理说明。
89
+ - 确保是完整且清晰的技术方案描述。
90
+
91
+ 已有输出:
92
+ 第一部分:
93
+ {background_content}
94
+ 第二部分:
95
+ {shortcoming_content}
96
+ 第三部分:
97
+ {problem_content}
98
+
99
+ 用户提供的需求:
100
+ {user_input}
101
+ """
102
+
103
+ KEYPOINT_PROMPT = """
104
+ 请根据以下信息撰写第五部分内容:
105
+ 五、本发明的关键点和欲保护点是:
106
+ 要求:
107
+ - 从第四部分的技术方案中提炼出关键创新点,以简洁的列点形式呈现。
108
+ - 这些点应是有利于申请权利要求保护的技术核心。
109
+
110
+ 已有输出:
111
+ 第四部分:
112
+ {solution_content}
113
+
114
+ 用户提供的需求:
115
+ {user_input}
116
+ """
117
+
118
+ ADVANTAGE_PROMPT = """
119
+ 请根据以下信息撰写第六部分内容:
120
+ 六、与第二条所述最好的现有技术相比,本发明的优点:
121
+ 要求:
122
+ - 简要介绍本发明相对现有技术的有益效果和优势。
123
+ - 结合技术方案来描述,做到有理有据。
124
+
125
+ 已有输出:
126
+ 第二部分(现有技术的缺点):
127
+ {shortcoming_content}
128
+ 第三部分(本发明解决的技术问题):
129
+ {problem_content}
130
+ 第四部分(本发明技术方案的详细阐述):
131
+ {solution_content}
132
+
133
+ 用户提供的需求:
134
+ {user_input}
135
+ """
136
+
137
+ ALTERNATIVE_PROMPT = """
138
+ 请根据以下信息撰写第七部分内容:
139
+ 七、针对第四部分中的技术方案,是否还有别的替代方案?
140
+ 要求:
141
+ - 阐述可能的替代技术方案或实现方式,以拓宽专利保护范围。
142
+ - 可以是部分元件/步骤的替换,也可以是整体方案的替代。
143
+
144
+ 已有输出:
145
+ 第四部分:
146
+ {solution_content}
147
+
148
+ 用户提供的需求:
149
+ {user_input}
150
+ """
151
+
152
+ def generate_patent_document(
153
+ bg_input,
154
+ shortcoming_input,
155
+ problem_input,
156
+ solution_input,
157
+ keypoint_input,
158
+ advantage_input,
159
+ alternative_input,
160
+ progress=gr.Progress()
161
+ ):
162
+ # 1. 背景技术
163
+ progress(0, desc="正在生成背景技术部分...")
164
+ bg_prompt = BACKGROUND_PROMPT.format(user_input=bg_input)
165
+ background_content = call_openai_api(bg_prompt)
166
+
167
+ # 2. 现有技术缺点
168
+ progress(0.15, desc="正在生成现有技术缺点部分...")
169
+ short_prompt = SHORTCOMING_PROMPT.format(
170
+ previous_content=background_content,
171
+ user_input=shortcoming_input
172
+ )
173
+ shortcoming_content = call_openai_api(short_prompt)
174
+
175
+ # 3. 本发明解决的技术问题
176
+ progress(0.3, desc="正在生成技术问题部分...")
177
+ problem_prompt_full = PROBLEM_PROMPT.format(
178
+ background_content=background_content,
179
+ shortcoming_content=shortcoming_content,
180
+ user_input=problem_input
181
+ )
182
+ problem_content = call_openai_api(problem_prompt_full)
183
+
184
+ # 4. 本发明技术方案
185
+ progress(0.45, desc="正在生成技术方案部分...")
186
+ solution_prompt_full = SOLUTION_PROMPT.format(
187
+ background_content=background_content,
188
+ shortcoming_content=shortcoming_content,
189
+ problem_content=problem_content,
190
+ user_input=solution_input
191
+ )
192
+ solution_content = call_openai_api(solution_prompt_full)
193
+
194
+ # 5. 关键点
195
+ progress(0.6, desc="正在生成关键点部分...")
196
+ keypoint_prompt_full = KEYPOINT_PROMPT.format(
197
+ solution_content=solution_content,
198
+ user_input=keypoint_input
199
+ )
200
+ keypoint_content = call_openai_api(keypoint_prompt_full)
201
+
202
+ # 6. 优点
203
+ progress(0.75, desc="正在生成优点部分...")
204
+ advantage_prompt_full = ADVANTAGE_PROMPT.format(
205
+ shortcoming_content=shortcoming_content,
206
+ problem_content=problem_content,
207
+ solution_content=solution_content,
208
+ user_input=advantage_input
209
+ )
210
+ advantage_content = call_openai_api(advantage_prompt_full)
211
+
212
+ # 7. 替代方案
213
+ progress(0.9, desc="正在生成替代方案部分...")
214
+ alternative_prompt_full = ALTERNATIVE_PROMPT.format(
215
+ solution_content=solution_content,
216
+ user_input=alternative_input
217
+ )
218
+ alternative_content = call_openai_api(alternative_prompt_full)
219
+
220
+ progress(1.0, desc="生成完成!")
221
+
222
+ # 整合所有内容
223
+ final_document = f"{background_content}\n\n{shortcoming_content}\n\n{problem_content}\n\n{solution_content}\n\n{keypoint_content}\n\n{advantage_content}\n\n{alternative_content}"
224
+ return final_document
225
+
226
+ # 使用Gradio构建前端
227
+ def gradio_interface(
228
+ bg_input,
229
+ shortcoming_input,
230
+ problem_input,
231
+ solution_input,
232
+ keypoint_input,
233
+ advantage_input,
234
+ alternative_input,
235
+ progress=gr.Progress()
236
+ ):
237
+ doc = generate_patent_document(
238
+ bg_input,
239
+ shortcoming_input,
240
+ problem_input,
241
+ solution_input,
242
+ keypoint_input,
243
+ advantage_input,
244
+ alternative_input,
245
+ progress=progress
246
+ )
247
+ return doc
248
+
249
+ with gr.Blocks() as demo:
250
+ gr.Markdown("## 专利交底书生成系统 (基于大模型的Agent生成)")
251
+ bg = gr.Textbox(label="一、背景技术与现有技术方案输入")
252
+ sh = gr.Textbox(label="二、现有技术缺点输入")
253
+ pr = gr.Textbox(label="三、本发明解决的技术问题输入")
254
+ so = gr.Textbox(label="四、本发明技术方案输入")
255
+ kp = gr.Textbox(label="五、关键点输入")
256
+ adv = gr.Textbox(label="六、本发明优点输入")
257
+ alt = gr.Textbox(label="七、替代方案输入")
258
+
259
+ generate_button = gr.Button("开始生成")
260
+ output = gr.Textbox(label="生成的专利交底书文本", lines=30)
261
+
262
+ generate_button.click(
263
+ fn=gradio_interface,
264
+ inputs=[bg, sh, pr, so, kp, adv, alt],
265
+ outputs=[output]
266
+ )
267
+
268
+ demo.launch()