Spaces:
Running
Running
File size: 4,663 Bytes
84608c2 cf4ccbb 84608c2 cf4ccbb 84608c2 cf4ccbb 84608c2 cf4ccbb 84608c2 cf4ccbb 84608c2 cf4ccbb 84608c2 cf4ccbb 84608c2 cf4ccbb 84608c2 cf4ccbb 84608c2 cf4ccbb 84608c2 cf4ccbb 84608c2 cf4ccbb 84608c2 e218f8e 84608c2 80bf620 84608c2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
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()
|