eluvletter / app.py
admin
2 en
cf4ccbb
import os
import json
import base64
import gradio as gr
def oversize(file_path: str, size_kb=1024):
size_bytes = size_kb * 1024
file_size = os.path.getsize(file_path)
return file_size >= size_bytes
def toBase64(file_path: str):
if not file_path:
file_path = "./example.mp3"
if oversize(file_path):
return ""
with open(file_path, "rb") as audio_file:
audio_data = audio_file.read()
base64_encoded = base64.b64encode(audio_data)
return "data:audio/mpeg;base64," + base64_encoded.decode("utf-8")
def infer(
recipient: str,
sender: str,
salutation: str,
signature: str,
body: str,
title: str,
bgm: str,
out_json="./content.json",
):
if not bgm:
return None, "Please upload a BGM"
if os.path.exists(out_json):
os.remove(out_json)
content = {
"recipient": recipient.replace(" ", " "),
"sender": sender.replace(" ", " "),
"salutation": salutation.replace(" ", " "),
"signature": signature.replace(" ", " "),
"body": body.replace(" ", " "),
"title": title,
"bgm": toBase64(bgm),
}
if not content["bgm"]:
return None, "Your uploaded BGM is too large"
with open(out_json, "w", encoding="utf-8") as json_file:
json.dump(
content,
json_file,
ensure_ascii=False,
indent=4,
)
return out_json, "Generation success"
if __name__ == "__main__":
with gr.Blocks() as demo:
gr.Interface(
fn=infer,
inputs=[
gr.Textbox(
label="Recipient",
placeholder="The recipient centered on front of the envelope",
),
gr.Textbox(
label="Sender",
placeholder="The sender name on back of the envelope",
),
gr.Textbox(
label="Salutation",
placeholder="The salutation in letter's upper left corner",
),
gr.Textbox(
label="Signature",
placeholder="The signature in letter's lower right corner",
),
gr.TextArea(
label="Body",
placeholder="Body of the letter, <br> represents a line break and the number after ^ represents the number of milliseconds that the typewriter's effects pause",
),
gr.Textbox(
label="Title",
placeholder="Browser tab text",
),
gr.Audio(
label="BGM",
type="filepath",
format="mp3",
),
],
outputs=[
gr.File(label="Download JSON file"),
gr.Textbox(label="Status bar"),
],
examples=[
[
"To Hiro",
"Mika",
"弘树",
"美嘉",
" 如果那天...^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恋爱着",
"eLuvLetter",
"./example.mp3",
]
],
title="eLuvLetter JSON Generator",
submit_btn="Generate",
clear_btn="Clear",
flagging_mode="never",
cache_examples=False,
description="""
This tool can generate your customized content.json to replace the font/content.json in your forked <a href='https://github.com/Society-Genius/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.""",
)
gr.HTML(
"""
<iframe src="//player.bilibili.com/player.html?bvid=BV1hergYREEG&autoplay=0" scrolling="no" border="0" frameborder="no" framespacing="0" allowfullscreen="true" width="100%" style="aspect-ratio: 16 / 9;"></iframe>
"""
)
demo.launch()