Spaces:
Sleeping
Sleeping
Enoch
commited on
Commit
·
2f80522
1
Parent(s):
6b68d70
增加了模型选择功能,点击日志
Browse files
app.py
CHANGED
@@ -10,31 +10,52 @@ load_dotenv()
|
|
10 |
|
11 |
# 初始化OpenAI客户端
|
12 |
client = OpenAI(
|
13 |
-
api_key=os.getenv("
|
14 |
-
base_url="https://api.shubiaobiao.cn/v1/"
|
15 |
)
|
16 |
|
17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
"""调用OpenAI API生成内容"""
|
19 |
try:
|
20 |
chat_completion = client.chat.completions.create(
|
21 |
messages=[
|
22 |
-
|
|
|
|
|
|
|
23 |
{
|
24 |
"role": "user",
|
25 |
-
"content":
|
26 |
},
|
27 |
],
|
28 |
-
model=
|
29 |
temperature=temperature,
|
30 |
-
max_tokens=
|
31 |
n=1
|
32 |
)
|
|
|
33 |
return chat_completion.choices[0].message.content.strip()
|
34 |
except Exception as e:
|
35 |
print(f"API调用出错:{str(e)}")
|
36 |
return f"生成失败:{str(e)}"
|
37 |
|
|
|
38 |
# 定义每个部分的Agent Prompt模板
|
39 |
BACKGROUND_PROMPT = """
|
40 |
你是一名资深的专利代理人,现在需要根据以下输入信息来撰写一份专利申请技术交底书的第一部分内容。
|
@@ -89,7 +110,6 @@ PROBLEM_PROMPT = """
|
|
89 |
用户提供的需求:
|
90 |
{user_input}
|
91 |
|
92 |
-
请按照如下格式输出:
|
93 |
三、本发明解决的技术问题是:
|
94 |
……
|
95 |
"""
|
@@ -186,106 +206,92 @@ def generate_patent_document(
|
|
186 |
keypoint_input,
|
187 |
advantage_input,
|
188 |
alternative_input,
|
189 |
-
|
190 |
):
|
|
|
191 |
try:
|
192 |
final_document = ""
|
193 |
|
194 |
# 生成背景技术部分
|
195 |
-
# progress(0
|
196 |
-
|
197 |
bg_prompt = BACKGROUND_PROMPT.format(user_input=bg_input)
|
198 |
-
background_content = call_openai_api(bg_prompt)
|
199 |
-
final_document +=
|
200 |
-
yield
|
201 |
-
|
202 |
-
|
203 |
# 生成现有技术缺点部分
|
204 |
# progress(0.15, desc="正在生成现有技术缺点部分...")
|
205 |
-
|
206 |
short_prompt = SHORTCOMING_PROMPT.format(
|
207 |
previous_content=background_content,
|
208 |
user_input=shortcoming_input
|
209 |
)
|
210 |
-
shortcoming_content = call_openai_api(short_prompt)
|
211 |
-
final_document +=
|
212 |
-
yield
|
213 |
-
|
214 |
-
|
215 |
# 生成技术问题部分
|
216 |
# progress(0.3, desc="正在生成技术问题部分...")
|
217 |
-
|
218 |
problem_prompt_full = PROBLEM_PROMPT.format(
|
219 |
background_content=background_content,
|
220 |
shortcoming_content=shortcoming_content,
|
221 |
user_input=problem_input
|
222 |
)
|
223 |
-
problem_content = call_openai_api(problem_prompt_full)
|
224 |
-
final_document +=
|
225 |
-
yield
|
226 |
-
|
227 |
-
|
228 |
# 生成技术方案部分
|
229 |
# progress(0.45, desc="正在生成技术方案部分...")
|
230 |
-
|
231 |
solution_prompt_full = SOLUTION_PROMPT.format(
|
232 |
background_content=background_content,
|
233 |
shortcoming_content=shortcoming_content,
|
234 |
problem_content=problem_content,
|
235 |
user_input=solution_input
|
236 |
)
|
237 |
-
solution_content = call_openai_api(solution_prompt_full)
|
238 |
-
final_document +=
|
239 |
-
yield
|
240 |
-
|
241 |
-
|
242 |
# 生成关键点部分
|
243 |
# progress(0.6, desc="正在生成关键点部分...")
|
244 |
-
|
245 |
keypoint_prompt_full = KEYPOINT_PROMPT.format(
|
246 |
solution_content=solution_content,
|
247 |
user_input=keypoint_input
|
248 |
)
|
249 |
-
keypoint_content = call_openai_api(keypoint_prompt_full)
|
250 |
-
final_document +=
|
251 |
-
yield
|
252 |
-
|
253 |
-
|
254 |
# 生成优点部分
|
255 |
# progress(0.75, desc="正在生成优点部分...")
|
256 |
-
|
257 |
advantage_prompt_full = ADVANTAGE_PROMPT.format(
|
258 |
shortcoming_content=shortcoming_content,
|
259 |
problem_content=problem_content,
|
260 |
solution_content=solution_content,
|
261 |
user_input=advantage_input
|
262 |
)
|
263 |
-
advantage_content = call_openai_api(advantage_prompt_full)
|
264 |
-
final_document +=
|
265 |
-
yield
|
266 |
-
|
267 |
-
|
268 |
# 生成替代方案部分
|
269 |
# progress(0.9, desc="正在生成替代方案部分...")
|
270 |
-
|
271 |
alternative_prompt_full = ALTERNATIVE_PROMPT.format(
|
272 |
solution_content=solution_content,
|
273 |
user_input=alternative_input
|
274 |
)
|
275 |
-
alternative_content = call_openai_api(alternative_prompt_full)
|
276 |
-
final_document +=
|
277 |
-
yield status, final_document
|
278 |
-
# progress(1.0, desc="已完成替代方案部分。")
|
279 |
-
|
280 |
-
# 最终润色(可选)
|
281 |
-
# progress(1.0, desc="正在进行最终润色...")
|
282 |
-
# status = "正在进行最终润色..."
|
283 |
-
# final_document = finalize_document(final_document) # 如果有润色步骤
|
284 |
-
# yield status, final_document
|
285 |
|
286 |
-
#
|
287 |
-
|
288 |
-
yield status, final_document
|
289 |
|
290 |
except Exception as e:
|
291 |
error_message = f"生成过程中发生错误:{str(e)}"
|
@@ -371,7 +377,17 @@ with gr.Blocks(theme=gr.themes.Soft(
|
|
371 |
secondary_hue="indigo",
|
372 |
neutral_hue="slate"
|
373 |
)) as demo:
|
374 |
-
gr.Markdown("## 📝 专利交底书生成系统\n
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
375 |
|
376 |
with gr.Accordion("📥 输入信息", open=True):
|
377 |
bg = gr.Textbox(
|
@@ -379,44 +395,92 @@ with gr.Blocks(theme=gr.themes.Soft(
|
|
379 |
placeholder="请输入背景技术和现有技术方案...",
|
380 |
lines=4
|
381 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
382 |
sh = gr.Textbox(
|
383 |
label="二、现有技术缺点输入",
|
384 |
placeholder="请输入现有技术的缺点...",
|
385 |
lines=4
|
386 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
387 |
pr = gr.Textbox(
|
388 |
label="三、本发明解决的技术问题输入",
|
389 |
placeholder="请输入本发明解决的技术问题...",
|
390 |
lines=4
|
391 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
392 |
so = gr.Textbox(
|
393 |
label="四、本发明技术方案输入",
|
394 |
placeholder="请输入本发明的技术方案...",
|
395 |
lines=4
|
396 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
397 |
kp = gr.Textbox(
|
398 |
label="五、关键点输入",
|
399 |
placeholder="请输入本发明的关键点...",
|
400 |
lines=3
|
401 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
402 |
adv = gr.Textbox(
|
403 |
label="六、本发明优点输入",
|
404 |
placeholder="请输入本发明的优点...",
|
405 |
lines=3
|
406 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
407 |
alt = gr.Textbox(
|
408 |
label="七、替代方案输入",
|
409 |
placeholder="请输入替代方案...",
|
410 |
lines=3
|
411 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
412 |
|
413 |
with gr.Row():
|
414 |
generate_button = gr.Button("🚀 开始生成", variant="primary")
|
415 |
clear_button = gr.Button("🧹 清空所有", variant="secondary")
|
416 |
|
417 |
progress_bar = gr.Progress()
|
418 |
-
status_box = gr.Textbox(label="生成状态", interactive=False
|
419 |
-
|
420 |
final_output = gr.Markdown(label="生成的专利交底书文本")
|
421 |
with gr.Row():
|
422 |
download_button = gr.Button("⬇️ 创建下载文件", variant="secondary")
|
@@ -429,10 +493,10 @@ with gr.Blocks(theme=gr.themes.Soft(
|
|
429 |
outputs=[download_file]
|
430 |
)
|
431 |
|
432 |
-
#
|
433 |
generate_button.click(
|
434 |
fn=generate_patent_document,
|
435 |
-
inputs=[bg, sh, pr, so, kp, adv, alt],
|
436 |
outputs=[status_box, final_output]
|
437 |
).then(
|
438 |
fn=lambda x: gr.update(visible=True),
|
@@ -440,10 +504,11 @@ with gr.Blocks(theme=gr.themes.Soft(
|
|
440 |
outputs=[download_file]
|
441 |
)
|
442 |
|
|
|
443 |
clear_button.click(
|
444 |
fn=clear_all,
|
445 |
inputs=[],
|
446 |
outputs=[bg, sh, pr, so, kp, adv, alt, final_output, download_file]
|
447 |
)
|
448 |
|
449 |
-
demo.launch()
|
|
|
10 |
|
11 |
# 初始化OpenAI客户端
|
12 |
client = OpenAI(
|
13 |
+
api_key=os.getenv("SHU"),
|
14 |
+
base_url="https://api.shubiaobiao.cn/v1/"
|
15 |
)
|
16 |
|
17 |
+
|
18 |
+
import datetime # 导入 datetime 模块
|
19 |
+
|
20 |
+
USER_COUNT_FILE = "click_log.txt" # 修改计数器文件名,更名为日志文件名
|
21 |
+
|
22 |
+
def increment_user_counter():
|
23 |
+
"""记录用户点击事件的时间戳到日志文件"""
|
24 |
+
timestamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") # 获取当前时间并格式化
|
25 |
+
log_entry = f"{timestamp}\n" # 创建日志条目,包含时间戳和换行符
|
26 |
+
try:
|
27 |
+
with open(USER_COUNT_FILE, "a") as f: # 以追加模式打开日志文件
|
28 |
+
f.write(log_entry) # 写入日志条目
|
29 |
+
print(f"用户点击记录已添加: {timestamp}") # 可选:在控制台打印日志信息
|
30 |
+
except Exception as e:
|
31 |
+
print(f"记录用户点击日志出错: {str(e)}")
|
32 |
+
|
33 |
+
def call_openai_api(prompt, temperature=0.7, model="deepseek-v3"):
|
34 |
"""调用OpenAI API生成内容"""
|
35 |
try:
|
36 |
chat_completion = client.chat.completions.create(
|
37 |
messages=[
|
38 |
+
{
|
39 |
+
"role": "system",
|
40 |
+
"content": "你是一个专业的专利代理人,正在完成一份专利文档的撰写。",
|
41 |
+
},
|
42 |
{
|
43 |
"role": "user",
|
44 |
+
"content": prompt,
|
45 |
},
|
46 |
],
|
47 |
+
model=model,
|
48 |
temperature=temperature,
|
49 |
+
max_tokens=8000,
|
50 |
n=1
|
51 |
)
|
52 |
+
|
53 |
return chat_completion.choices[0].message.content.strip()
|
54 |
except Exception as e:
|
55 |
print(f"API调用出错:{str(e)}")
|
56 |
return f"生成失败:{str(e)}"
|
57 |
|
58 |
+
|
59 |
# 定义每个部分的Agent Prompt模板
|
60 |
BACKGROUND_PROMPT = """
|
61 |
你是一名资深的专利代理人,现在需要根据以下输入信息来撰写一份专利申请技术交底书的第一部分内容。
|
|
|
110 |
用户提供的需求:
|
111 |
{user_input}
|
112 |
|
|
|
113 |
三、本发明解决的技术问题是:
|
114 |
……
|
115 |
"""
|
|
|
206 |
keypoint_input,
|
207 |
advantage_input,
|
208 |
alternative_input,
|
209 |
+
model_choice,
|
210 |
):
|
211 |
+
increment_user_counter() # 在函数开始时递增计数器
|
212 |
try:
|
213 |
final_document = ""
|
214 |
|
215 |
# 生成背景技术部分
|
216 |
+
# progress(0, desc="正在生成背景技术部分...")
|
217 |
+
yield "正在生成背景技术部分...", None
|
218 |
bg_prompt = BACKGROUND_PROMPT.format(user_input=bg_input)
|
219 |
+
background_content = call_openai_api(bg_prompt, model=model_choice)
|
220 |
+
final_document += f"{background_content}\n\n"
|
221 |
+
yield "正在生成背景技术部分...", final_document
|
222 |
+
|
|
|
223 |
# 生成现有技术缺点部分
|
224 |
# progress(0.15, desc="正在生成现有技术缺点部分...")
|
225 |
+
yield "正在生成现有技术缺点部分...", final_document
|
226 |
short_prompt = SHORTCOMING_PROMPT.format(
|
227 |
previous_content=background_content,
|
228 |
user_input=shortcoming_input
|
229 |
)
|
230 |
+
shortcoming_content = call_openai_api(short_prompt, model=model_choice)
|
231 |
+
final_document += f"{shortcoming_content}\n\n"
|
232 |
+
yield "正在生成现有技术缺点部分...", final_document
|
233 |
+
|
|
|
234 |
# 生成技术问题部分
|
235 |
# progress(0.3, desc="正在生成技术问题部分...")
|
236 |
+
yield "正在生成技术问题部分...", final_document
|
237 |
problem_prompt_full = PROBLEM_PROMPT.format(
|
238 |
background_content=background_content,
|
239 |
shortcoming_content=shortcoming_content,
|
240 |
user_input=problem_input
|
241 |
)
|
242 |
+
problem_content = call_openai_api(problem_prompt_full, model=model_choice)
|
243 |
+
final_document += f"{problem_content}\n\n"
|
244 |
+
yield "正在生成技术问题部分...", final_document
|
245 |
+
|
|
|
246 |
# 生成技术方案部分
|
247 |
# progress(0.45, desc="正在生成技术方案部分...")
|
248 |
+
yield "正在生成技术方案部分...", final_document
|
249 |
solution_prompt_full = SOLUTION_PROMPT.format(
|
250 |
background_content=background_content,
|
251 |
shortcoming_content=shortcoming_content,
|
252 |
problem_content=problem_content,
|
253 |
user_input=solution_input
|
254 |
)
|
255 |
+
solution_content = call_openai_api(solution_prompt_full, model=model_choice)
|
256 |
+
final_document += f"{solution_content}\n\n"
|
257 |
+
yield "正在生成技术方案部分...", final_document
|
258 |
+
|
|
|
259 |
# 生成关键点部分
|
260 |
# progress(0.6, desc="正在生成关键点部分...")
|
261 |
+
yield "正在生成关键点部分...", final_document
|
262 |
keypoint_prompt_full = KEYPOINT_PROMPT.format(
|
263 |
solution_content=solution_content,
|
264 |
user_input=keypoint_input
|
265 |
)
|
266 |
+
keypoint_content = call_openai_api(keypoint_prompt_full, model=model_choice)
|
267 |
+
final_document += f"{keypoint_content}\n\n"
|
268 |
+
yield "正在生成关键点部分...", final_document
|
269 |
+
|
|
|
270 |
# 生成优点部分
|
271 |
# progress(0.75, desc="正在生成优点部分...")
|
272 |
+
yield "正在生成优点部分...", final_document
|
273 |
advantage_prompt_full = ADVANTAGE_PROMPT.format(
|
274 |
shortcoming_content=shortcoming_content,
|
275 |
problem_content=problem_content,
|
276 |
solution_content=solution_content,
|
277 |
user_input=advantage_input
|
278 |
)
|
279 |
+
advantage_content = call_openai_api(advantage_prompt_full, model=model_choice)
|
280 |
+
final_document += f"{advantage_content}\n\n"
|
281 |
+
yield "正在生成优点部分...", final_document
|
282 |
+
|
|
|
283 |
# 生成替代方案部分
|
284 |
# progress(0.9, desc="正在生成替代方案部分...")
|
285 |
+
yield "正在生成替代方案部分...", final_document
|
286 |
alternative_prompt_full = ALTERNATIVE_PROMPT.format(
|
287 |
solution_content=solution_content,
|
288 |
user_input=alternative_input
|
289 |
)
|
290 |
+
alternative_content = call_openai_api(alternative_prompt_full, model=model_choice)
|
291 |
+
final_document += f"{alternative_content}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
292 |
|
293 |
+
# progress(1.0, desc="生成完成!")
|
294 |
+
yield "生成完成!", final_document
|
|
|
295 |
|
296 |
except Exception as e:
|
297 |
error_message = f"生成过程中发生错误:{str(e)}"
|
|
|
377 |
secondary_hue="indigo",
|
378 |
neutral_hue="slate"
|
379 |
)) as demo:
|
380 |
+
gr.Markdown("## 📝 专利交底书生成系统\n### **基于语言模型技术,为您自动生成专利文档。**")
|
381 |
+
gr.Markdown('- 请按顺序填写下列所有部分的信息,然后点击\"🚀 开始生成\"按钮。')
|
382 |
+
gr.Markdown("- 请提供分点的自然语言信息,得到更高质量的输出结果")
|
383 |
+
|
384 |
+
# 添加模型选择下拉菜单
|
385 |
+
model_dropdown = gr.Dropdown(
|
386 |
+
choices=["deepseek-v3", "gpt-4o"],
|
387 |
+
value="deepseek-v3",
|
388 |
+
label="选择模型",
|
389 |
+
info="请选择要使用的AI模型"
|
390 |
+
)
|
391 |
|
392 |
with gr.Accordion("📥 输入信息", open=True):
|
393 |
bg = gr.Textbox(
|
|
|
395 |
placeholder="请输入背景技术和现有技术方案...",
|
396 |
lines=4
|
397 |
)
|
398 |
+
# 添加背景技术示例
|
399 |
+
gr.Examples(
|
400 |
+
examples=[["在移动设备领域,智能手机的电池续航一直是一个重要问题。目前主流的充电技术包括有线快充和无线充电。现有技术中,无线充电通常采用电磁感应原理,需要将设备放置在特定的充电板上,且充电效率相对较低。"]],
|
401 |
+
inputs=[bg],
|
402 |
+
label="背景技术示例"
|
403 |
+
)
|
404 |
+
|
405 |
sh = gr.Textbox(
|
406 |
label="二、现有技术缺点输入",
|
407 |
placeholder="请输入现有技术的缺点...",
|
408 |
lines=4
|
409 |
)
|
410 |
+
# 添加现有技术缺点示例
|
411 |
+
gr.Examples(
|
412 |
+
examples=[["现有的无线充电技术存在充电距离短、效率低、发热严重等问题。同时,用户必须将设备精确放置在充电区域内,使用不够便捷。"]],
|
413 |
+
inputs=[sh],
|
414 |
+
label="现有技术缺点示例"
|
415 |
+
)
|
416 |
+
|
417 |
pr = gr.Textbox(
|
418 |
label="三、本发明解决的技术问题输入",
|
419 |
placeholder="请输入本发明解决的技术问题...",
|
420 |
lines=4
|
421 |
)
|
422 |
+
# 添加技术问题示例
|
423 |
+
gr.Examples(
|
424 |
+
examples=[["本发明旨在提供一种新型远距离无线充电技术,解决现有无线充电距离短、效率低的问题,实现更便捷的充电体验。"]],
|
425 |
+
inputs=[pr],
|
426 |
+
label="技术问题示例"
|
427 |
+
)
|
428 |
+
|
429 |
so = gr.Textbox(
|
430 |
label="四、本发明技术方案输入",
|
431 |
placeholder="请输入本发明的技术方案...",
|
432 |
lines=4
|
433 |
)
|
434 |
+
# 添加技术方案示例
|
435 |
+
gr.Examples(
|
436 |
+
examples=[["本发明提出一种基于定向电磁波的远距离无线充电系统,包含智能定向天线阵列和高效能量转换模块。系统通过毫米波技术实现精确对准和能量传输,可在3米范围内实现稳定充电。"]],
|
437 |
+
inputs=[so],
|
438 |
+
label="技术方案示例"
|
439 |
+
)
|
440 |
+
|
441 |
kp = gr.Textbox(
|
442 |
label="五、关键点输入",
|
443 |
placeholder="请输入本发明的关键点...",
|
444 |
lines=3
|
445 |
)
|
446 |
+
# 添加关键点示例
|
447 |
+
gr.Examples(
|
448 |
+
examples=[["智能定向天线阵列设计、高效能量转换模块、远距离无线充电控制算法"]],
|
449 |
+
inputs=[kp],
|
450 |
+
label="关键点示例"
|
451 |
+
)
|
452 |
+
|
453 |
adv = gr.Textbox(
|
454 |
label="六、本发明优点输入",
|
455 |
placeholder="请输入本发明的优点...",
|
456 |
lines=3
|
457 |
)
|
458 |
+
# 添加优点示例
|
459 |
+
gr.Examples(
|
460 |
+
examples=[["充电距离显著提升至3米,充电效率提高30%,支持多设备同时充电,智能识别待充电设备位置"]],
|
461 |
+
inputs=[adv],
|
462 |
+
label="优点示例"
|
463 |
+
)
|
464 |
+
|
465 |
alt = gr.Textbox(
|
466 |
label="七、替代方案输入",
|
467 |
placeholder="请输入替代方案...",
|
468 |
lines=3
|
469 |
)
|
470 |
+
# 添加替代方案示例
|
471 |
+
gr.Examples(
|
472 |
+
examples=[["天线阵列可替换为相控阵天线系统,能量转换模块可采用不同的整流电路方案,控制算法可使用其他机器学习方法"]],
|
473 |
+
inputs=[alt],
|
474 |
+
label="替代方案示例"
|
475 |
+
)
|
476 |
|
477 |
with gr.Row():
|
478 |
generate_button = gr.Button("🚀 开始生成", variant="primary")
|
479 |
clear_button = gr.Button("🧹 清空所有", variant="secondary")
|
480 |
|
481 |
progress_bar = gr.Progress()
|
482 |
+
status_box = gr.Textbox(label="生成状态", interactive=False) # 添加标签和设置为只读
|
483 |
+
|
484 |
final_output = gr.Markdown(label="生成的专利交底书文本")
|
485 |
with gr.Row():
|
486 |
download_button = gr.Button("⬇️ 创建下载文件", variant="secondary")
|
|
|
493 |
outputs=[download_file]
|
494 |
)
|
495 |
|
496 |
+
# 修改生成按钮的点击事件,添加模型选择参数
|
497 |
generate_button.click(
|
498 |
fn=generate_patent_document,
|
499 |
+
inputs=[bg, sh, pr, so, kp, adv, alt, model_dropdown], # 添加 model_dropdown
|
500 |
outputs=[status_box, final_output]
|
501 |
).then(
|
502 |
fn=lambda x: gr.update(visible=True),
|
|
|
504 |
outputs=[download_file]
|
505 |
)
|
506 |
|
507 |
+
|
508 |
clear_button.click(
|
509 |
fn=clear_all,
|
510 |
inputs=[],
|
511 |
outputs=[bg, sh, pr, so, kp, adv, alt, final_output, download_file]
|
512 |
)
|
513 |
|
514 |
+
demo.launch()
|