Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from openai import OpenAI
|
| 4 |
+
|
| 5 |
+
# 从环境变量加载API密钥
|
| 6 |
+
client = OpenAI(
|
| 7 |
+
api_key=os.getenv("DEEPSEEK_API_KEY"),
|
| 8 |
+
base_url="https://api.deepseek.com"
|
| 9 |
+
)
|
| 10 |
+
|
| 11 |
+
def generate_scenery(lyrics):
|
| 12 |
+
"""将中文歌词转换为纯自然风景描写的英文文本"""
|
| 13 |
+
response = client.chat.completions.create(
|
| 14 |
+
model="deepseek-chat",
|
| 15 |
+
messages=[
|
| 16 |
+
{"role": "system", "content": "你是一位自然诗人,请将中文歌词转化为英文的纯风景描写,严格避免任何人称代词或人物动作。只描述自然现象如风、光、水、植物等。"},
|
| 17 |
+
{"role": "user", "content": f"转换以下歌词(仅输出英文描写):\n{lyrics}"}
|
| 18 |
+
],
|
| 19 |
+
temperature=0.7,
|
| 20 |
+
max_tokens=300
|
| 21 |
+
)
|
| 22 |
+
return response.choices[0].message.content
|
| 23 |
+
|
| 24 |
+
# Gradio界面
|
| 25 |
+
with gr.Blocks(title="歌词转风景描写", theme=gr.themes.Soft()) as app:
|
| 26 |
+
gr.Markdown("## 🌿 中文歌词转英文风景描写")
|
| 27 |
+
with gr.Row():
|
| 28 |
+
with gr.Column():
|
| 29 |
+
input_text = gr.Textbox(
|
| 30 |
+
label="输入中文歌词",
|
| 31 |
+
placeholder="例:晚风吹来多么清凉...",
|
| 32 |
+
lines=5
|
| 33 |
+
)
|
| 34 |
+
btn = gr.Button("生成", variant="primary")
|
| 35 |
+
with gr.Column():
|
| 36 |
+
output_text = gr.Textbox(
|
| 37 |
+
label="英文风景描写",
|
| 38 |
+
lines=8,
|
| 39 |
+
interactive=False
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
# 示例数据
|
| 43 |
+
examples = gr.Examples(
|
| 44 |
+
examples=[
|
| 45 |
+
["对空气不停念你的名字"],
|
| 46 |
+
["会不会让你我重来一次"]
|
| 47 |
+
],
|
| 48 |
+
inputs=[input_text],
|
| 49 |
+
label="点击试试示例歌词"
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
btn.click(
|
| 53 |
+
fn=generate_scenery,
|
| 54 |
+
inputs=input_text,
|
| 55 |
+
outputs=output_text
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
app.launch(share = True)
|