Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,5 @@
|
|
1 |
import os
|
|
|
2 |
import gradio as gr
|
3 |
from gtts import gTTS
|
4 |
import hashlib # π΅ νμΌλͺ
μ κ³ μ νκ² νκΈ° μν΄ μΆκ°
|
@@ -33,18 +34,31 @@ long_vowel_stories = [
|
|
33 |
{"text": "A baby bird flew away.", "image": f"{image_base_url}20.webp"}
|
34 |
]
|
35 |
|
36 |
-
# π gTTSλ₯Ό μ¬μ©νμ¬ μμ± νμΌ μμ± (κ³ μ ν νμΌλͺ
|
|
|
|
|
37 |
def generate_audio(text):
|
38 |
try:
|
|
|
|
|
|
|
|
|
39 |
# π΅ κ³ μ ν νμΌλͺ
μμ±
|
40 |
hash_key = hashlib.md5(text.encode()).hexdigest()[:10] # ν΄μ μ 10μ리 μ¬μ©
|
41 |
filename = f"audio_{hash_key}.mp3"
|
42 |
|
43 |
-
# π΅ μμ±
|
44 |
tts = gTTS(text=text, lang="en")
|
45 |
tts.save(filename)
|
46 |
|
47 |
print(f"β
μμ± νμΌ μμ± μλ£: {filename}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
return filename
|
49 |
except Exception as e:
|
50 |
print(f"β οΈ μμ± μμ± μ€ν¨: {e}")
|
@@ -59,10 +73,9 @@ def next_story(current_index, story_list):
|
|
59 |
audio_file = generate_audio(text)
|
60 |
return new_index, text, image, audio_file, text
|
61 |
|
62 |
-
# π "μ¬μ" λ²νΌ ν΄λ¦ μ νΈμΆλλ ν¨μ
|
63 |
def play_story(current_text):
|
64 |
-
|
65 |
-
return gr.Audio.update(value=audio_file)
|
66 |
|
67 |
# π μ΄κΈ° λ°μ΄ν° μ€μ
|
68 |
init_index_short, init_text_short, init_image_short, init_audio_short = 0, short_vowel_stories[0]["text"], short_vowel_stories[0]["image"], generate_audio(short_vowel_stories[0]["text"])
|
@@ -70,35 +83,6 @@ init_index_long, init_text_long, init_image_long, init_audio_long = 0, long_vowe
|
|
70 |
|
71 |
# π Gradio UI ꡬμ±
|
72 |
with gr.Blocks(title="π κ·μ¬μ΄ μ€ν 리 μ±") as demo:
|
73 |
-
gr.HTML("""
|
74 |
-
<style>
|
75 |
-
body {
|
76 |
-
background-color: #FFFAF0; /* λ°λ»ν ν¬λ¦Όμ λ°°κ²½ */
|
77 |
-
}
|
78 |
-
|
79 |
-
.btn-custom {
|
80 |
-
font-size: 1.5em;
|
81 |
-
font-weight: bold;
|
82 |
-
border-radius: 20px;
|
83 |
-
padding: 10px;
|
84 |
-
margin: 5px;
|
85 |
-
}
|
86 |
-
|
87 |
-
.next-btn {
|
88 |
-
background-color: #FFD700; /* λ
Έλμ λ²νΌ */
|
89 |
-
}
|
90 |
-
|
91 |
-
.play-btn {
|
92 |
-
background-color: #90EE90; /* μ°ν μ΄λ‘μ λ²νΌ */
|
93 |
-
}
|
94 |
-
|
95 |
-
img {
|
96 |
-
border-radius: 10px;
|
97 |
-
border: 5px solid #FFFFFF;
|
98 |
-
}
|
99 |
-
</style>
|
100 |
-
""")
|
101 |
-
|
102 |
with gr.Tabs():
|
103 |
with gr.TabItem("Short Vowel (λ¨λͺ¨μ)"):
|
104 |
state_index_short = gr.State(value=init_index_short)
|
@@ -109,8 +93,8 @@ with gr.Blocks(title="π κ·μ¬μ΄ μ€ν 리 μ±") as demo:
|
|
109 |
audio_output_short = gr.Audio(value=init_audio_short, autoplay=True)
|
110 |
|
111 |
with gr.Row():
|
112 |
-
next_button_short = gr.Button("π λ€μ μ΄μΌκΈ°"
|
113 |
-
play_button_short = gr.Button("π λ€μ λ£κΈ°"
|
114 |
|
115 |
next_button_short.click(
|
116 |
fn=next_story,
|
@@ -133,8 +117,8 @@ with gr.Blocks(title="π κ·μ¬μ΄ μ€ν 리 μ±") as demo:
|
|
133 |
audio_output_long = gr.Audio(value=init_audio_long, autoplay=False)
|
134 |
|
135 |
with gr.Row():
|
136 |
-
next_button_long = gr.Button("π λ€μ μ΄μΌκΈ°"
|
137 |
-
play_button_long = gr.Button("π λ€μ λ£κΈ°"
|
138 |
|
139 |
next_button_long.click(
|
140 |
fn=next_story,
|
@@ -148,5 +132,6 @@ with gr.Blocks(title="π κ·μ¬μ΄ μ€ν 리 μ±") as demo:
|
|
148 |
outputs=[audio_output_long]
|
149 |
)
|
150 |
|
|
|
151 |
# π μ± μ€ν
|
152 |
demo.launch()
|
|
|
1 |
import os
|
2 |
+
import time
|
3 |
import gradio as gr
|
4 |
from gtts import gTTS
|
5 |
import hashlib # π΅ νμΌλͺ
μ κ³ μ νκ² νκΈ° μν΄ μΆκ°
|
|
|
34 |
{"text": "A baby bird flew away.", "image": f"{image_base_url}20.webp"}
|
35 |
]
|
36 |
|
37 |
+
# π gTTSλ₯Ό μ¬μ©νμ¬ μμ± νμΌ μμ± (κ³ μ ν νμΌλͺ
+ μΊμ± μμ€ν
μΆκ°)
|
38 |
+
cached_audio = {} # κ°μ λ¬Έμ₯μ λν΄ APIλ₯Ό μ¬λ¬ λ² νΈμΆνμ§ μλλ‘ μ μ₯
|
39 |
+
|
40 |
def generate_audio(text):
|
41 |
try:
|
42 |
+
# π΅ μ΄λ―Έ μμ±λ μμ±μ΄ μμΌλ©΄ μ¬μ¬μ© (429 μ€λ₯ λ°©μ§)
|
43 |
+
if text in cached_audio:
|
44 |
+
return cached_audio[text]
|
45 |
+
|
46 |
# π΅ κ³ μ ν νμΌλͺ
μμ±
|
47 |
hash_key = hashlib.md5(text.encode()).hexdigest()[:10] # ν΄μ μ 10μ리 μ¬μ©
|
48 |
filename = f"audio_{hash_key}.mp3"
|
49 |
|
50 |
+
# π΅ gTTSλ₯Ό μ¬μ©νμ¬ μμ± μμ±
|
51 |
tts = gTTS(text=text, lang="en")
|
52 |
tts.save(filename)
|
53 |
|
54 |
print(f"β
μμ± νμΌ μμ± μλ£: {filename}")
|
55 |
+
|
56 |
+
# π΅ μμ±λ νμΌ μΊμ±
|
57 |
+
cached_audio[text] = filename
|
58 |
+
|
59 |
+
# π΅ μμ² μ νμ νΌνκΈ° μν΄ 1.5μ΄ λκΈ°
|
60 |
+
time.sleep(1.5)
|
61 |
+
|
62 |
return filename
|
63 |
except Exception as e:
|
64 |
print(f"β οΈ μμ± μμ± μ€ν¨: {e}")
|
|
|
73 |
audio_file = generate_audio(text)
|
74 |
return new_index, text, image, audio_file, text
|
75 |
|
76 |
+
# π "μ¬μ" λ²νΌ ν΄λ¦ μ νΈμΆλλ ν¨μ (Gradio μ΅μ λ²μ μ λ§κ² μμ )
|
77 |
def play_story(current_text):
|
78 |
+
return generate_audio(current_text)
|
|
|
79 |
|
80 |
# π μ΄κΈ° λ°μ΄ν° μ€μ
|
81 |
init_index_short, init_text_short, init_image_short, init_audio_short = 0, short_vowel_stories[0]["text"], short_vowel_stories[0]["image"], generate_audio(short_vowel_stories[0]["text"])
|
|
|
83 |
|
84 |
# π Gradio UI ꡬμ±
|
85 |
with gr.Blocks(title="π κ·μ¬μ΄ μ€ν 리 μ±") as demo:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
86 |
with gr.Tabs():
|
87 |
with gr.TabItem("Short Vowel (λ¨λͺ¨μ)"):
|
88 |
state_index_short = gr.State(value=init_index_short)
|
|
|
93 |
audio_output_short = gr.Audio(value=init_audio_short, autoplay=True)
|
94 |
|
95 |
with gr.Row():
|
96 |
+
next_button_short = gr.Button("π λ€μ μ΄μΌκΈ°")
|
97 |
+
play_button_short = gr.Button("π λ€μ λ£κΈ°")
|
98 |
|
99 |
next_button_short.click(
|
100 |
fn=next_story,
|
|
|
117 |
audio_output_long = gr.Audio(value=init_audio_long, autoplay=False)
|
118 |
|
119 |
with gr.Row():
|
120 |
+
next_button_long = gr.Button("π λ€μ μ΄μΌκΈ°")
|
121 |
+
play_button_long = gr.Button("π λ€μ λ£κΈ°")
|
122 |
|
123 |
next_button_long.click(
|
124 |
fn=next_story,
|
|
|
132 |
outputs=[audio_output_long]
|
133 |
)
|
134 |
|
135 |
+
|
136 |
# π μ± μ€ν
|
137 |
demo.launch()
|