Spaces:
Running
Running
Enoch
commited on
Commit
·
9a5a32f
1
Parent(s):
e341113
app update
Browse files
app.py
CHANGED
@@ -1,39 +1,47 @@
|
|
1 |
import os
|
2 |
from openai import OpenAI
|
3 |
import gradio as gr
|
4 |
-
|
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 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
|
|
|
|
|
|
|
|
34 |
BACKGROUND_PROMPT = """
|
35 |
你是一名资深的专利代理人,现在需要根据以下输入信息来撰写一份专利申请技术交底书的第一部分内容。
|
36 |
-
|
37 |
|
38 |
要求:
|
39 |
1. 内容必须清晰、详实,包括宏观技术背景和具体现有技术方案。
|
@@ -67,7 +75,7 @@ PROBLEM_PROMPT = """
|
|
67 |
请根据以下信息编写第三部分内容:
|
68 |
三、本发明决的技术问题是:
|
69 |
要求:
|
70 |
-
-
|
71 |
- 内容应与本发明拟解决的问题相呼应。
|
72 |
|
73 |
已有输出:
|
@@ -82,7 +90,7 @@ PROBLEM_PROMPT = """
|
|
82 |
"""
|
83 |
|
84 |
SOLUTION_PROMPT = """
|
85 |
-
|
86 |
四、本发明技术方案的详细阐述:
|
87 |
要求:
|
88 |
- 详细描述本发明的技术方案,包括各功能模块/步骤的技术实现方式、结构图(可文字描述)和原理说明。
|
@@ -117,10 +125,10 @@ KEYPOINT_PROMPT = """
|
|
117 |
|
118 |
ADVANTAGE_PROMPT = """
|
119 |
请根据以下信息撰写第六部分内容:
|
120 |
-
|
121 |
-
|
122 |
- 简要介绍本发明相对现有技术的有益效果和优势。
|
123 |
-
-
|
124 |
|
125 |
已有输出:
|
126 |
第二部分(现有技术的缺点):
|
@@ -148,7 +156,6 @@ ALTERNATIVE_PROMPT = """
|
|
148 |
用户提供的需求:
|
149 |
{user_input}
|
150 |
"""
|
151 |
-
|
152 |
def generate_patent_document(
|
153 |
bg_input,
|
154 |
shortcoming_input,
|
@@ -159,12 +166,10 @@ def generate_patent_document(
|
|
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,
|
@@ -172,7 +177,6 @@ def generate_patent_document(
|
|
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,
|
@@ -181,7 +185,6 @@ def generate_patent_document(
|
|
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,
|
@@ -191,7 +194,6 @@ def generate_patent_document(
|
|
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,
|
@@ -199,7 +201,6 @@ def generate_patent_document(
|
|
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,
|
@@ -209,7 +210,6 @@ def generate_patent_document(
|
|
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,
|
@@ -219,11 +219,17 @@ def generate_patent_document(
|
|
219 |
|
220 |
progress(1.0, desc="生成完成!")
|
221 |
|
222 |
-
|
223 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
224 |
return final_document
|
225 |
|
226 |
-
# 使用Gradio构建前端
|
227 |
def gradio_interface(
|
228 |
bg_input,
|
229 |
shortcoming_input,
|
@@ -234,6 +240,7 @@ def gradio_interface(
|
|
234 |
alternative_input,
|
235 |
progress=gr.Progress()
|
236 |
):
|
|
|
237 |
doc = generate_patent_document(
|
238 |
bg_input,
|
239 |
shortcoming_input,
|
@@ -244,25 +251,119 @@ def gradio_interface(
|
|
244 |
alternative_input,
|
245 |
progress=progress
|
246 |
)
|
247 |
-
return doc
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
248 |
|
249 |
with gr.Blocks() as demo:
|
250 |
gr.Markdown("## 专利交底书生成系统 (基于大模型的Agent生成)")
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
|
260 |
-
|
261 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
262 |
generate_button.click(
|
263 |
-
fn=gradio_interface,
|
264 |
inputs=[bg, sh, pr, so, kp, adv, alt],
|
265 |
-
outputs=[
|
266 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
267 |
|
268 |
-
demo.launch()
|
|
|
1 |
import os
|
2 |
from openai import OpenAI
|
3 |
import gradio as gr
|
4 |
+
from dotenv import load_dotenv
|
5 |
+
import tempfile
|
6 |
+
import atexit
|
7 |
|
8 |
+
# 加载环境变量
|
9 |
+
load_dotenv()
|
10 |
+
|
11 |
+
# 初始化OpenAI客户端
|
12 |
client = OpenAI(
|
13 |
api_key=os.getenv("DASHSCOPE_API_KEY"),
|
14 |
base_url="https://dashscope.aliyuncs.com/compatible-mode/v1"
|
15 |
)
|
16 |
|
|
|
17 |
def call_openai_api(prompt, temperature=0.7):
|
18 |
+
"""调用OpenAI API生成内容"""
|
19 |
+
try:
|
20 |
+
chat_completion = client.chat.completions.create(
|
21 |
+
messages=[
|
22 |
+
{
|
23 |
+
"role": "system",
|
24 |
+
"content": "你是一个专业的专利代理人,擅长撰写专利文档。",
|
25 |
+
},
|
26 |
+
{
|
27 |
+
"role": "user",
|
28 |
+
"content": prompt,
|
29 |
+
},
|
30 |
+
],
|
31 |
+
model="qwen2.5-1.5b-instruct",
|
32 |
+
temperature=temperature,
|
33 |
+
max_tokens=3000,
|
34 |
+
n=1
|
35 |
+
)
|
36 |
+
return chat_completion.choices[0].message.content.strip()
|
37 |
+
except Exception as e:
|
38 |
+
print(f"API调用出错:{str(e)}")
|
39 |
+
return f"生成失败:{str(e)}"
|
40 |
+
|
41 |
+
# 定义每个部分的Agent Prompt模板
|
42 |
BACKGROUND_PROMPT = """
|
43 |
你是一名资深的专利代理人,现在需要根据以下输入信息来撰写一份专利申请技术交底书的第一部分内容。
|
44 |
+
请根据用户提供的背景材料和参考文献信息,详说明本发明所处的技术领域背景和已有技术方案。
|
45 |
|
46 |
要求:
|
47 |
1. 内容必须清晰、详实,包括宏观技术背景和具体现有技术方案。
|
|
|
75 |
请根据以下信息编写第三部分内容:
|
76 |
三、本发明决的技术问题是:
|
77 |
要求:
|
78 |
+
- 对第二分所列出的缺点和不足,描述本发明试图解决的具体技术问题。
|
79 |
- 内容应与本发明拟解决的问题相呼应。
|
80 |
|
81 |
已有输出:
|
|
|
90 |
"""
|
91 |
|
92 |
SOLUTION_PROMPT = """
|
93 |
+
请根以下信息撰写第四部分内容:
|
94 |
四、本发明技术方案的详细阐述:
|
95 |
要求:
|
96 |
- 详细描述本发明的技术方案,包括各功能模块/步骤的技术实现方式、结构图(可文字描述)和原理说明。
|
|
|
125 |
|
126 |
ADVANTAGE_PROMPT = """
|
127 |
请根据以下信息撰写第六部分内容:
|
128 |
+
六、与第二条所述最好的现有技术相比,本发明的优:
|
129 |
+
求:
|
130 |
- 简要介绍本发明相对现有技术的有益效果和优势。
|
131 |
+
- 结技术方案来描述,做到有理有据。
|
132 |
|
133 |
已有输出:
|
134 |
第二部分(现有技术的缺点):
|
|
|
156 |
用户提供的需求:
|
157 |
{user_input}
|
158 |
"""
|
|
|
159 |
def generate_patent_document(
|
160 |
bg_input,
|
161 |
shortcoming_input,
|
|
|
166 |
alternative_input,
|
167 |
progress=gr.Progress()
|
168 |
):
|
|
|
169 |
progress(0, desc="正在生成背景技术部分...")
|
170 |
bg_prompt = BACKGROUND_PROMPT.format(user_input=bg_input)
|
171 |
background_content = call_openai_api(bg_prompt)
|
172 |
|
|
|
173 |
progress(0.15, desc="正在生成现有技术缺点部分...")
|
174 |
short_prompt = SHORTCOMING_PROMPT.format(
|
175 |
previous_content=background_content,
|
|
|
177 |
)
|
178 |
shortcoming_content = call_openai_api(short_prompt)
|
179 |
|
|
|
180 |
progress(0.3, desc="正在生成技术问题部分...")
|
181 |
problem_prompt_full = PROBLEM_PROMPT.format(
|
182 |
background_content=background_content,
|
|
|
185 |
)
|
186 |
problem_content = call_openai_api(problem_prompt_full)
|
187 |
|
|
|
188 |
progress(0.45, desc="正在生成技术方案部分...")
|
189 |
solution_prompt_full = SOLUTION_PROMPT.format(
|
190 |
background_content=background_content,
|
|
|
194 |
)
|
195 |
solution_content = call_openai_api(solution_prompt_full)
|
196 |
|
|
|
197 |
progress(0.6, desc="正在生成关键点部分...")
|
198 |
keypoint_prompt_full = KEYPOINT_PROMPT.format(
|
199 |
solution_content=solution_content,
|
|
|
201 |
)
|
202 |
keypoint_content = call_openai_api(keypoint_prompt_full)
|
203 |
|
|
|
204 |
progress(0.75, desc="正在生成优点部分...")
|
205 |
advantage_prompt_full = ADVANTAGE_PROMPT.format(
|
206 |
shortcoming_content=shortcoming_content,
|
|
|
210 |
)
|
211 |
advantage_content = call_openai_api(advantage_prompt_full)
|
212 |
|
|
|
213 |
progress(0.9, desc="正在生成替代方案部分...")
|
214 |
alternative_prompt_full = ALTERNATIVE_PROMPT.format(
|
215 |
solution_content=solution_content,
|
|
|
219 |
|
220 |
progress(1.0, desc="生成完成!")
|
221 |
|
222 |
+
final_document = (
|
223 |
+
f"{background_content}\n\n"
|
224 |
+
f"{shortcoming_content}\n\n"
|
225 |
+
f"{problem_content}\n\n"
|
226 |
+
f"{solution_content}\n\n"
|
227 |
+
f"{keypoint_content}\n\n"
|
228 |
+
f"{advantage_content}\n\n"
|
229 |
+
f"{alternative_content}"
|
230 |
+
)
|
231 |
return final_document
|
232 |
|
|
|
233 |
def gradio_interface(
|
234 |
bg_input,
|
235 |
shortcoming_input,
|
|
|
240 |
alternative_input,
|
241 |
progress=gr.Progress()
|
242 |
):
|
243 |
+
progress_output = gr.Textbox() # 用于显示进度的临时输出
|
244 |
doc = generate_patent_document(
|
245 |
bg_input,
|
246 |
shortcoming_input,
|
|
|
251 |
alternative_input,
|
252 |
progress=progress
|
253 |
)
|
254 |
+
return progress_output, doc
|
255 |
+
|
256 |
+
# ... existing code ...
|
257 |
+
|
258 |
+
def generate_filename(text):
|
259 |
+
"""根据专利交底书内容生成合适的文件名"""
|
260 |
+
try:
|
261 |
+
prompt = """
|
262 |
+
请根据以下专利交底书内容,生成一个简短的文件名(不超过50个字符)。
|
263 |
+
文件名应该能反映出发明的核心内容和技术领域。
|
264 |
+
只需要返回文件名,不需要其他解释。
|
265 |
+
|
266 |
+
专利交底书内容:
|
267 |
+
{text}
|
268 |
+
""".format(text=text[:1000]) # 只取前1000个字符用于生成文件名
|
269 |
+
|
270 |
+
filename = call_openai_api(prompt).strip()
|
271 |
+
# 确保文件名合法
|
272 |
+
filename = "".join(c for c in filename if c not in r'\/:*?"<>|')
|
273 |
+
return filename + ".txt"
|
274 |
+
except Exception as e:
|
275 |
+
print(f"生成文件名失败:{str(e)}")
|
276 |
+
return "专利交底书.txt" # 默认文件名
|
277 |
+
|
278 |
+
def download_text(text):
|
279 |
+
"""将文本保存为临时文件并返回文件路径"""
|
280 |
+
filename = generate_filename(text)
|
281 |
+
# 创建临时文件,使用生成的文件名
|
282 |
+
temp_dir = tempfile.gettempdir()
|
283 |
+
file_path = os.path.join(temp_dir, filename)
|
284 |
+
|
285 |
+
with open(file_path, mode="w", encoding="utf-8") as f:
|
286 |
+
f.write(text)
|
287 |
+
return file_path
|
288 |
+
|
289 |
+
# ... existing code ...
|
290 |
+
def clear_all():
|
291 |
+
"""清空所有输入和输出"""
|
292 |
+
try:
|
293 |
+
# 清空下载文件组件前,先将其值设为None
|
294 |
+
return [""] * 9 + [None] # 7个输入框 + 2个输出框 + 1个下载文件框(None)
|
295 |
+
except Exception as e:
|
296 |
+
print(f"清空过程中出错:{str(e)}")
|
297 |
+
return [""] * 9 + [None]
|
298 |
+
|
299 |
+
def cleanup_temp_files():
|
300 |
+
"""清理临时文件"""
|
301 |
+
temp_dir = tempfile.gettempdir()
|
302 |
+
for file in os.listdir(temp_dir):
|
303 |
+
if file.endswith(".txt"):
|
304 |
+
try:
|
305 |
+
file_path = os.path.join(temp_dir, file)
|
306 |
+
if os.path.exists(file_path):
|
307 |
+
os.remove(file_path)
|
308 |
+
except Exception as e:
|
309 |
+
print(f"删除临时文件失败:{str(e)}")
|
310 |
+
continue
|
311 |
+
|
312 |
|
313 |
with gr.Blocks() as demo:
|
314 |
gr.Markdown("## 专利交底书生成系统 (基于大模型的Agent生成)")
|
315 |
+
|
316 |
+
with gr.Column():
|
317 |
+
bg = gr.Textbox(label="一、背景技术与现有技术方案输入")
|
318 |
+
sh = gr.Textbox(label="二、现有技术缺点输入")
|
319 |
+
pr = gr.Textbox(label="三、本发明解决的技术问题输入")
|
320 |
+
so = gr.Textbox(label="四、本发明技术方案输入")
|
321 |
+
kp = gr.Textbox(label="五、关键点输入")
|
322 |
+
adv = gr.Textbox(label="六、本发明优点输入")
|
323 |
+
alt = gr.Textbox(label="七、替代方案输入")
|
324 |
+
|
325 |
+
with gr.Row():
|
326 |
+
generate_button = gr.Button("开始生成", variant="primary")
|
327 |
+
clear_button = gr.Button("清空所有", variant="secondary")
|
328 |
+
|
329 |
+
progress_output = gr.Textbox(label="生成进度", lines=2)
|
330 |
+
final_output = gr.Markdown(label="生成的专利交底书文本")
|
331 |
+
|
332 |
+
with gr.Row():
|
333 |
+
download_button = gr.Button("创建下载文件", variant="secondary")
|
334 |
+
download_file = gr.File(label="点击下载文件", visible=True)
|
335 |
+
|
336 |
+
# 设置下载按钮
|
337 |
+
download_button.click(
|
338 |
+
fn=download_text,
|
339 |
+
inputs=[final_output],
|
340 |
+
outputs=[download_file]
|
341 |
+
)
|
342 |
+
|
343 |
+
# 设置生成按钮
|
344 |
generate_button.click(
|
345 |
+
fn=gradio_interface,
|
346 |
inputs=[bg, sh, pr, so, kp, adv, alt],
|
347 |
+
outputs=[progress_output, final_output]
|
348 |
)
|
349 |
+
|
350 |
+
# 设置清空按钮
|
351 |
+
clear_button.click(
|
352 |
+
fn=clear_all,
|
353 |
+
inputs=[],
|
354 |
+
outputs=[bg, sh, pr, so, kp, adv, alt, progress_output, final_output, download_file]
|
355 |
+
)
|
356 |
+
|
357 |
+
# 在程序退出时清理临时文件
|
358 |
+
def cleanup_temp_files():
|
359 |
+
temp_dir = tempfile.gettempdir()
|
360 |
+
for file in os.listdir(temp_dir):
|
361 |
+
if file.endswith(".txt"):
|
362 |
+
try:
|
363 |
+
os.remove(os.path.join(temp_dir, file))
|
364 |
+
except:
|
365 |
+
pass
|
366 |
+
|
367 |
+
atexit.register(cleanup_temp_files)
|
368 |
|
369 |
+
demo.launch()
|