svjack commited on
Commit
ba05ee6
·
verified ·
1 Parent(s): 58260e0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -13
app.py CHANGED
@@ -17,30 +17,40 @@ def read_csv_prefix(file, n_lines=128):
17
  else:
18
  raise ValueError("CSV 文件中没有 'prompt' 列")
19
 
20
- def generate_prompt(user_input, csv_prompts, language='English'):
21
- """基于用户输入和 CSV 提示生成新提示词"""
22
- # 统一前缀
23
- prefix = "In the style of anime landscape,"
24
-
25
  # 构建系统提示
26
  system_prompt = (
27
  f"你是一位自然诗人,请将以下内容转化为{'英文' if language == 'English' else '中文'}的风景描写。"
28
  "请确保风格与提供的 CSV 中的描述一致,并以统一前缀 '{prefix}' 开头。"
 
29
  )
30
-
31
  # 拼接用户输入和 CSV 内容
32
  user_content = f"转换以下内容:\n{user_input}\n\n参考示例:\n" + "\n".join(csv_prompts[:5])
33
-
34
  response = client.chat.completions.create(
35
  model="deepseek-chat",
36
  messages=[
37
- {"role": "system", "content": system_prompt},
38
  {"role": "user", "content": user_content}
39
  ],
40
  temperature=0.7,
41
- max_tokens=300
42
  )
43
- return response.choices[0].message.content.strip()
 
 
 
 
 
 
 
 
 
 
 
 
44
 
45
  # Gradio 界面定义
46
  with gr.Blocks(title="提示词生成器", theme=gr.themes.Soft()) as app:
@@ -55,6 +65,8 @@ with gr.Blocks(title="提示词生成器", theme=gr.themes.Soft()) as app:
55
  )
56
  csv_file = gr.File(label=".csv 文件", file_types=[".csv"])
57
  line_count = gr.Number(label="读取行数", value=128, minimum=1, maximum=10000)
 
 
58
  language_choice = gr.Radio(choices=["English", "Chinese"], label="输出语言", value="English")
59
  btn = gr.Button("生成提示词", variant="primary")
60
  with gr.Column():
@@ -63,6 +75,11 @@ with gr.Blocks(title="提示词生成器", theme=gr.themes.Soft()) as app:
63
  lines=8,
64
  interactive=False
65
  )
 
 
 
 
 
66
 
67
  # 示例数据
68
  examples = gr.Examples(
@@ -76,9 +93,9 @@ with gr.Blocks(title="提示词生成器", theme=gr.themes.Soft()) as app:
76
 
77
  # 事件绑定
78
  btn.click(
79
- fn=lambda text, file, lines, lang: generate_prompt(text, read_csv_prefix(file, lines), lang),
80
- inputs=[input_text, csv_file, line_count, language_choice],
81
- outputs=output_text
82
  )
83
 
84
  app.launch(share=True)
 
17
  else:
18
  raise ValueError("CSV 文件中没有 'prompt' 列")
19
 
20
+ def generate_prompts(user_input, csv_prompts, prefix, num_examples=3, language='English'):
21
+ """基于用户输入和 CSV 提示生成多个新提示词"""
 
 
 
22
  # 构建系统提示
23
  system_prompt = (
24
  f"你是一位自然诗人,请将以下内容转化为{'英文' if language == 'English' else '中文'}的风景描写。"
25
  "请确保风格与提供的 CSV 中的描述一致,并以统一前缀 '{prefix}' 开头。"
26
+ "请生成 {num_examples} 条不同风格的结果,每条结果应独立成段。"
27
  )
28
+
29
  # 拼接用户输入和 CSV 内容
30
  user_content = f"转换以下内容:\n{user_input}\n\n参考示例:\n" + "\n".join(csv_prompts[:5])
31
+
32
  response = client.chat.completions.create(
33
  model="deepseek-chat",
34
  messages=[
35
+ {"role": "system", "content": system_prompt.format(prefix=prefix, num_examples=num_examples)},
36
  {"role": "user", "content": user_content}
37
  ],
38
  temperature=0.7,
39
+ max_tokens=300 * num_examples
40
  )
41
+
42
+ # 将模型输出按换行分割为多个提示词
43
+ raw_output = response.choices[0].message.content.strip()
44
+ prompts = [line.strip() for line in raw_output.split('\n') if line.strip()]
45
+
46
+ # 取前 num_examples 个有效结果
47
+ prompts = prompts[:num_examples]
48
+
49
+ # 处理后文本:每行以指定前缀开头
50
+ processed_prompts = [f"{prefix} {p}" if not p.startswith(prefix) else p for p in prompts]
51
+ processed_text = "\n".join(processed_prompts)
52
+
53
+ return "\n\n".join(prompts), processed_text
54
 
55
  # Gradio 界面定义
56
  with gr.Blocks(title="提示词生成器", theme=gr.themes.Soft()) as app:
 
65
  )
66
  csv_file = gr.File(label=".csv 文件", file_types=[".csv"])
67
  line_count = gr.Number(label="读取行数", value=128, minimum=1, maximum=10000)
68
+ prefix_input = gr.Textbox(label="提示词前缀", value="In the style of anime landscape,", lines=1)
69
+ num_examples = gr.Number(label="生成示例数量", value=3, minimum=1, maximum=10)
70
  language_choice = gr.Radio(choices=["English", "Chinese"], label="输出语言", value="English")
71
  btn = gr.Button("生成提示词", variant="primary")
72
  with gr.Column():
 
75
  lines=8,
76
  interactive=False
77
  )
78
+ processed_text = gr.Textbox(
79
+ label="处理后文本(每行以指定前缀开头)",
80
+ lines=8,
81
+ interactive=False
82
+ )
83
 
84
  # 示例数据
85
  examples = gr.Examples(
 
93
 
94
  # 事件绑定
95
  btn.click(
96
+ fn=lambda text, file, lines, prefix, num, lang: generate_prompts(text, read_csv_prefix(file, lines), prefix, int(num), lang),
97
+ inputs=[input_text, csv_file, line_count, prefix_input, num_examples, language_choice],
98
+ outputs=[output_text, processed_text]
99
  )
100
 
101
  app.launch(share=True)