Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import pandas as pd
|
3 |
+
import gradio as gr
|
4 |
+
from openai import OpenAI
|
5 |
+
|
6 |
+
# 初始化 OpenAI 客户端
|
7 |
+
client = OpenAI(
|
8 |
+
api_key=os.getenv("DEEPSEEK_API_KEY"),
|
9 |
+
base_url="https://api.deepseek.com"
|
10 |
+
)
|
11 |
+
|
12 |
+
def read_csv_prefix(file, n_lines=128):
|
13 |
+
"""读取 CSV 文件的前 n 行并提取统一前缀"""
|
14 |
+
df = pd.read_csv(file.name)
|
15 |
+
if 'prompt' in df.columns:
|
16 |
+
return df['prompt'].head(n_lines).tolist()
|
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:
|
47 |
+
gr.Markdown("## 🎨 基于 CSV 的提示词生成器")
|
48 |
+
|
49 |
+
with gr.Row():
|
50 |
+
with gr.Column():
|
51 |
+
input_text = gr.Textbox(
|
52 |
+
label="输入文本",
|
53 |
+
placeholder="例如:森林里的阳光透过树叶洒落...",
|
54 |
+
lines=5
|
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():
|
61 |
+
output_text = gr.Textbox(
|
62 |
+
label="生成的提示词",
|
63 |
+
lines=8,
|
64 |
+
interactive=False
|
65 |
+
)
|
66 |
+
|
67 |
+
# 示例数据
|
68 |
+
examples = gr.Examples(
|
69 |
+
examples=[
|
70 |
+
["清晨的阳光洒在湖面上"],
|
71 |
+
["夜晚的城市灯火璀璨"]
|
72 |
+
],
|
73 |
+
inputs=[input_text],
|
74 |
+
label="点击试试示例"
|
75 |
+
)
|
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)
|