Spaces:
Running
Running
Upload 2 files
Browse files- app.py +134 -0
- example.mp3 +0 -0
app.py
ADDED
@@ -0,0 +1,134 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import json
|
3 |
+
import base64
|
4 |
+
import gradio as gr
|
5 |
+
|
6 |
+
|
7 |
+
def oversize(file_path: str, size_kb=1024):
|
8 |
+
size_bytes = size_kb * 1024
|
9 |
+
file_size = os.path.getsize(file_path)
|
10 |
+
return file_size >= size_bytes
|
11 |
+
|
12 |
+
|
13 |
+
def toBase64(file_path: str):
|
14 |
+
if not file_path:
|
15 |
+
file_path = "./example.mp3"
|
16 |
+
|
17 |
+
if oversize(file_path):
|
18 |
+
return ""
|
19 |
+
|
20 |
+
with open(file_path, "rb") as audio_file:
|
21 |
+
audio_data = audio_file.read()
|
22 |
+
|
23 |
+
base64_encoded = base64.b64encode(audio_data)
|
24 |
+
return "data:audio/mpeg;base64," + base64_encoded.decode("utf-8")
|
25 |
+
|
26 |
+
|
27 |
+
def infer(
|
28 |
+
recipient: str,
|
29 |
+
sender: str,
|
30 |
+
salutation: str,
|
31 |
+
signature: str,
|
32 |
+
body: str,
|
33 |
+
title: str,
|
34 |
+
bgm: str,
|
35 |
+
out_json="./content.json",
|
36 |
+
):
|
37 |
+
if not bgm:
|
38 |
+
return None, "请上传背景音乐 (Please upload a BGM)"
|
39 |
+
|
40 |
+
if os.path.exists(out_json):
|
41 |
+
os.remove(out_json)
|
42 |
+
|
43 |
+
content = {
|
44 |
+
"recipient": recipient.replace(" ", " "),
|
45 |
+
"sender": sender.replace(" ", " "),
|
46 |
+
"salutation": salutation.replace(" ", " "),
|
47 |
+
"signature": signature.replace(" ", " "),
|
48 |
+
"body": body.replace(" ", " "),
|
49 |
+
"title": title,
|
50 |
+
"bgm": toBase64(bgm),
|
51 |
+
}
|
52 |
+
|
53 |
+
if not content["bgm"]:
|
54 |
+
return None, "上传的 BGM 过大 (Your uploaded BGM is too large)"
|
55 |
+
|
56 |
+
with open(out_json, "w", encoding="utf-8") as json_file:
|
57 |
+
json.dump(
|
58 |
+
content,
|
59 |
+
json_file,
|
60 |
+
ensure_ascii=False,
|
61 |
+
indent=4,
|
62 |
+
)
|
63 |
+
|
64 |
+
return out_json, "生成成功 (Generation success)"
|
65 |
+
|
66 |
+
|
67 |
+
if __name__ == "__main__":
|
68 |
+
with gr.Blocks() as demo:
|
69 |
+
gr.Interface(
|
70 |
+
fn=infer,
|
71 |
+
inputs=[
|
72 |
+
gr.Textbox(
|
73 |
+
label="收信人 (Recipient)",
|
74 |
+
placeholder="信封正面中心人名 (The recipient centered on front of the envelope)",
|
75 |
+
),
|
76 |
+
gr.Textbox(
|
77 |
+
label="发信人 (Sender)",
|
78 |
+
placeholder="信封背面落款人名 (The sender name on back of the envelope)",
|
79 |
+
),
|
80 |
+
gr.Textbox(
|
81 |
+
label="称呼 (Salutation)",
|
82 |
+
placeholder="信纸左上角称呼 (The salutation in letter's upper left corner)",
|
83 |
+
),
|
84 |
+
gr.Textbox(
|
85 |
+
label="落款 (Signature)",
|
86 |
+
placeholder="信纸右下角落款 (The signature in letter's lower right corner)",
|
87 |
+
),
|
88 |
+
gr.TextArea(
|
89 |
+
label="正文 (Body)",
|
90 |
+
placeholder="信纸上的正文,<br>代表换行,^后的数字代表打字机特效停顿毫秒数\n\nBody of the letter, <br> represents a line break and the number after ^ represents the number of milliseconds that the typewriter's effects pause",
|
91 |
+
),
|
92 |
+
gr.Textbox(
|
93 |
+
label="标题 (Title)",
|
94 |
+
placeholder="浏览器标签文本 (Browser tab text)",
|
95 |
+
),
|
96 |
+
gr.Audio(
|
97 |
+
label="背景音乐 (BGM)",
|
98 |
+
type="filepath",
|
99 |
+
format="mp3",
|
100 |
+
),
|
101 |
+
],
|
102 |
+
outputs=[
|
103 |
+
gr.File(label="下载 JSON 文件 (Download JSON file)"),
|
104 |
+
gr.Textbox(label="状态栏 (Status bar)"),
|
105 |
+
],
|
106 |
+
examples=[
|
107 |
+
[
|
108 |
+
"To Hiro",
|
109 |
+
"Mika",
|
110 |
+
"弘树",
|
111 |
+
"美嘉",
|
112 |
+
" 如果那天...^600没有^200见到你<br> 我想我^600不会^200那么伤心<br> 那么难过<br> 不会^200泪流满面<br> 但是^600如果^200没有遇见你<br> 我就^200不会了解^600如此高兴<br> 如此^200温柔<br> 如此^200可爱<br> 如此^200温暖<br> 如此^200幸福^200的感觉<br> ^600现在^600还好吗?<br> 我...^600现在还和天空^200恋爱着",
|
113 |
+
"eLuvLetter",
|
114 |
+
"./example.mp3",
|
115 |
+
]
|
116 |
+
],
|
117 |
+
title="表白信封自定义配置工具 (eLuvLetter JSON Generator)",
|
118 |
+
submit_btn="生成 Generate",
|
119 |
+
clear_btn="清空 Clear",
|
120 |
+
flagging_mode="never",
|
121 |
+
cache_examples=False,
|
122 |
+
description="""
|
123 |
+
本工具可为你生成自定义版 content.json 用于替换你复刻得到的 <a href='https://github.com/MuGemSt/eLuvLetter' target='_blank'>eLuvLetter</a> 仓库内的 font/content.json, 其中的背景音乐控件用于上传拆信封时所播放的音频,建议不要太大,请确保音频上传完整后再点击生成按钮。
|
124 |
+
|
125 |
+
This tool can generate your customized content.json to replace the font/content.json in your forked <a href='https://github.com/MuGemSt/eLuvLetter' target='_blank'>eLuvLetter</a> repository, in which the BGM widget is used to upload the audio played when opening the envelope, it is recommended not to be too large, please make sure the audio is completely uploaded before clicking the Generate button.""",
|
126 |
+
)
|
127 |
+
|
128 |
+
gr.HTML(
|
129 |
+
"""
|
130 |
+
<iframe src="//player.bilibili.com/player.html?bvid=BV1NADfYCE1V&autoplay=0" scrolling="no" border="0" frameborder="no" framespacing="0" allowfullscreen="true" width="100%" style="aspect-ratio: 16 / 9;"></iframe>
|
131 |
+
"""
|
132 |
+
)
|
133 |
+
|
134 |
+
demo.launch()
|
example.mp3
ADDED
Binary file (469 kB). View file
|
|