Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,11 +1,10 @@
|
|
1 |
import os
|
2 |
-
import requests
|
3 |
import gradio as gr
|
4 |
-
from gtts import gTTS # β
gTTS
|
5 |
|
6 |
-
# μ€ν 리 λ°μ΄ν° (ν
μ€νΈ + μ΄λ―Έμ§ URL ν¬ν¨)
|
7 |
image_base_url = "https://huggingface.co/spaces/englissi/englishstories/resolve/main/image/"
|
8 |
-
|
9 |
{"text": "Sam has a cat.", "image": f"{image_base_url}1.webp"},
|
10 |
{"text": "The cat is fat and tan.", "image": f"{image_base_url}2.webp"},
|
11 |
{"text": "Sam and the cat nap on a mat.", "image": f"{image_base_url}3.webp"},
|
@@ -18,6 +17,20 @@ stories = [
|
|
18 |
{"text": "Fun in the sun is the best!", "image": f"{image_base_url}10.webp"}
|
19 |
]
|
20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
# π gTTSλ₯Ό μ¬μ©νμ¬ μμ± νμΌ μμ±
|
22 |
def generate_audio(text, filename="story.mp3"):
|
23 |
try:
|
@@ -29,7 +42,7 @@ def generate_audio(text, filename="story.mp3"):
|
|
29 |
print(f"β οΈ μμ± μμ± μ€ν¨: {e}")
|
30 |
return None
|
31 |
|
32 |
-
# μ€ν 리 ν
μ€νΈλ₯Ό HTML μ€νμΌλ‘ ν¬κ² νμνκ³ μ€μ μ λ ¬νλ ν¨μ
|
33 |
def format_story_text(text):
|
34 |
return f"""
|
35 |
<div style='
|
@@ -44,70 +57,94 @@ def format_story_text(text):
|
|
44 |
</div>
|
45 |
"""
|
46 |
|
47 |
-
# μ΄κΈ° μ€ν 리 λ‘λ© ν¨μ
|
48 |
-
def init_story():
|
49 |
index = 0
|
50 |
-
story =
|
51 |
text = story["text"]
|
52 |
image = story["image"]
|
53 |
audio_file = generate_audio(text, filename="story.mp3")
|
54 |
return index, text, image, audio_file
|
55 |
|
56 |
-
# "λ€μ" λ²νΌ ν΄λ¦ μ νΈμΆλλ ν¨μ
|
57 |
-
def next_story(current_index):
|
58 |
-
new_index = (current_index + 1) % len(
|
59 |
-
story =
|
60 |
text = story["text"]
|
61 |
image = story["image"]
|
62 |
audio_file = generate_audio(text, filename="story.mp3")
|
63 |
return new_index, format_story_text(text), image, audio_file, text
|
64 |
|
65 |
-
# "μ¬μ" λ²νΌ ν΄λ¦ μ νΈμΆλλ ν¨μ
|
66 |
def play_story(current_text):
|
67 |
audio_file = generate_audio(current_text, filename="story.mp3")
|
68 |
return audio_file
|
69 |
|
70 |
-
# μ΄κΈ° λ°μ΄ν° μ€μ
|
71 |
-
|
|
|
72 |
|
73 |
-
# Gradio μΈν°νμ΄μ€ ꡬμ±
|
74 |
with gr.Blocks(title="π κ·μ¬μ΄ μ€ν 리 μ±") as demo:
|
75 |
gr.HTML("<h1 style='text-align:center;'>π μ¬λ―Έμλ μμ΄ μ€ν 리 νμ! π</h1>")
|
76 |
-
|
77 |
gr.HTML("<p style='text-align:center; font-size:1.2em;'>π± κ·μ¬μ΄ μ΄μΌκΈ°μ ν¨κ» μμ΄λ₯Ό λ°°μ보μμ! <br> "
|
78 |
"λ²νΌμ λλ¬ λ€μ μ΄μΌκΈ°λ‘ λμ΄κ°κ³ , μμ±μ λ€μΌλ©° λ°λΌ μ½μ΄λ³΄μΈμ! π΅</p>")
|
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 |
# CSS μ€νμΌ μΆκ° (κ·μ¬μ΄ ν
λ§)
|
113 |
demo.css = """
|
|
|
1 |
import os
|
|
|
2 |
import gradio as gr
|
3 |
+
from gtts import gTTS # β
gTTS μ¬μ©
|
4 |
|
5 |
+
# π λ¨λͺ¨μ(Short Vowel) μ€ν 리 λ°μ΄ν° (ν
μ€νΈ + μ΄λ―Έμ§ URL ν¬ν¨)
|
6 |
image_base_url = "https://huggingface.co/spaces/englissi/englishstories/resolve/main/image/"
|
7 |
+
short_vowel_stories = [
|
8 |
{"text": "Sam has a cat.", "image": f"{image_base_url}1.webp"},
|
9 |
{"text": "The cat is fat and tan.", "image": f"{image_base_url}2.webp"},
|
10 |
{"text": "Sam and the cat nap on a mat.", "image": f"{image_base_url}3.webp"},
|
|
|
17 |
{"text": "Fun in the sun is the best!", "image": f"{image_base_url}10.webp"}
|
18 |
]
|
19 |
|
20 |
+
# π μ₯λͺ¨μ(Long Vowel) μ€ν 리 λ°μ΄ν°
|
21 |
+
long_vowel_stories = [
|
22 |
+
{"text": "Kate ate a big cake.", "image": f"{image_base_url}11.webp"},
|
23 |
+
{"text": "The train is late today.", "image": f"{image_base_url}12.webp"},
|
24 |
+
{"text": "I see a green tree.", "image": f"{image_base_url}13.webp"},
|
25 |
+
{"text": "He likes to read a book.", "image": f"{image_base_url}14.webp"},
|
26 |
+
{"text": "The kite is flying high.", "image": f"{image_base_url}15.webp"},
|
27 |
+
{"text": "The light is very bright.", "image": f"{image_base_url}16.webp"},
|
28 |
+
{"text": "The rose is red.", "image": f"{image_base_url}17.webp"},
|
29 |
+
{"text": "We saw a boat on the lake.", "image": f"{image_base_url}18.webp"},
|
30 |
+
{"text": "He is cute and kind.", "image": f"{image_base_url}19.webp"},
|
31 |
+
{"text": "A baby bird flew away.", "image": f"{image_base_url}20.webp"}
|
32 |
+
]
|
33 |
+
|
34 |
# π gTTSλ₯Ό μ¬μ©νμ¬ μμ± νμΌ μμ±
|
35 |
def generate_audio(text, filename="story.mp3"):
|
36 |
try:
|
|
|
42 |
print(f"β οΈ μμ± μμ± μ€ν¨: {e}")
|
43 |
return None
|
44 |
|
45 |
+
# π μ€ν 리 ν
μ€νΈλ₯Ό HTML μ€νμΌλ‘ ν¬κ² νμνκ³ μ€μ μ λ ¬νλ ν¨μ
|
46 |
def format_story_text(text):
|
47 |
return f"""
|
48 |
<div style='
|
|
|
57 |
</div>
|
58 |
"""
|
59 |
|
60 |
+
# π μ΄κΈ° μ€ν 리 λ‘λ© ν¨μ
|
61 |
+
def init_story(story_list):
|
62 |
index = 0
|
63 |
+
story = story_list[index]
|
64 |
text = story["text"]
|
65 |
image = story["image"]
|
66 |
audio_file = generate_audio(text, filename="story.mp3")
|
67 |
return index, text, image, audio_file
|
68 |
|
69 |
+
# π "λ€μ" λ²νΌ ν΄λ¦ μ νΈμΆλλ ν¨μ
|
70 |
+
def next_story(current_index, story_list):
|
71 |
+
new_index = (current_index + 1) % len(story_list)
|
72 |
+
story = story_list[new_index]
|
73 |
text = story["text"]
|
74 |
image = story["image"]
|
75 |
audio_file = generate_audio(text, filename="story.mp3")
|
76 |
return new_index, format_story_text(text), image, audio_file, text
|
77 |
|
78 |
+
# π "μ¬μ" λ²νΌ ν΄λ¦ μ νΈμΆλλ ν¨μ
|
79 |
def play_story(current_text):
|
80 |
audio_file = generate_audio(current_text, filename="story.mp3")
|
81 |
return audio_file
|
82 |
|
83 |
+
# π μ΄κΈ° λ°μ΄ν° μ€μ (λ¨λͺ¨μ & μ₯λͺ¨μ)
|
84 |
+
init_index_short, init_text_short, init_image_short, init_audio_short = init_story(short_vowel_stories)
|
85 |
+
init_index_long, init_text_long, init_image_long, init_audio_long = init_story(long_vowel_stories)
|
86 |
|
87 |
+
# π Gradio μΈν°νμ΄μ€ κ΅¬μ± (ν μΆκ°)
|
88 |
with gr.Blocks(title="π κ·μ¬μ΄ μ€ν 리 μ±") as demo:
|
89 |
gr.HTML("<h1 style='text-align:center;'>π μ¬λ―Έμλ μμ΄ μ€ν 리 νμ! π</h1>")
|
90 |
+
|
91 |
gr.HTML("<p style='text-align:center; font-size:1.2em;'>π± κ·μ¬μ΄ μ΄μΌκΈ°μ ν¨κ» μμ΄λ₯Ό λ°°μ보μμ! <br> "
|
92 |
"λ²νΌμ λλ¬ λ€μ μ΄μΌκΈ°λ‘ λμ΄κ°κ³ , μμ±μ λ€μΌλ©° λ°λΌ μ½μ΄λ³΄μΈμ! π΅</p>")
|
93 |
|
94 |
+
with gr.Tabs():
|
95 |
+
with gr.TabItem("Short Vowel (λ¨λͺ¨μ)"):
|
96 |
+
# λ¨λͺ¨μ μνκ°
|
97 |
+
state_index_short = gr.State(value=init_index_short)
|
98 |
+
state_text_short = gr.State(value=init_text_short)
|
99 |
+
|
100 |
+
# λ¨λͺ¨μ UI
|
101 |
+
story_text_short = gr.Markdown(value=format_story_text(init_text_short), label="μ€ν 리")
|
102 |
+
story_image_short = gr.Image(value=init_image_short, label="μ΄λ―Έμ§", type="filepath", width=400, height=400)
|
103 |
+
audio_output_short = gr.Audio(value=init_audio_short, label="π§ μμ± μ¬μ", type="filepath", autoplay=True)
|
104 |
+
|
105 |
+
with gr.Row():
|
106 |
+
next_button_short = gr.Button("π λ€μ μ΄μΌκΈ°", elem_id="next-btn")
|
107 |
+
play_button_short = gr.Button("π λ€μ λ£κΈ°", elem_id="play-btn")
|
108 |
+
|
109 |
+
next_button_short.click(
|
110 |
+
fn=next_story,
|
111 |
+
inputs=[state_index_short, gr.State(value=short_vowel_stories)],
|
112 |
+
outputs=[state_index_short, story_text_short, story_image_short, audio_output_short, state_text_short]
|
113 |
+
)
|
114 |
+
|
115 |
+
play_button_short.click(
|
116 |
+
fn=play_story,
|
117 |
+
inputs=[state_text_short],
|
118 |
+
outputs=[audio_output_short]
|
119 |
+
)
|
120 |
+
|
121 |
+
with gr.TabItem("Long Vowel (μ₯λͺ¨μ)"):
|
122 |
+
# μ₯λͺ¨μ μνκ°
|
123 |
+
state_index_long = gr.State(value=init_index_long)
|
124 |
+
state_text_long = gr.State(value=init_text_long)
|
125 |
+
|
126 |
+
# μ₯λͺ¨μ UI
|
127 |
+
story_text_long = gr.Markdown(value=format_story_text(init_text_long), label="μ€ν 리")
|
128 |
+
story_image_long = gr.Image(value=init_image_long, label="μ΄λ―Έμ§", type="filepath", width=400, height=400)
|
129 |
+
audio_output_long = gr.Audio(value=init_audio_long, label="π§ μμ± μ¬μ", type="filepath", autoplay=True)
|
130 |
+
|
131 |
+
with gr.Row():
|
132 |
+
next_button_long = gr.Button("π λ€μ μ΄μΌκΈ°", elem_id="next-btn")
|
133 |
+
play_button_long = gr.Button("π λ€μ λ£κΈ°", elem_id="play-btn")
|
134 |
+
|
135 |
+
next_button_long.click(
|
136 |
+
fn=next_story,
|
137 |
+
inputs=[state_index_long, gr.State(value=long_vowel_stories)],
|
138 |
+
outputs=[state_index_long, story_text_long, story_image_long, audio_output_long, state_text_long]
|
139 |
+
)
|
140 |
+
|
141 |
+
play_button_long.click(
|
142 |
+
fn=play_story,
|
143 |
+
inputs=[state_text_long],
|
144 |
+
outputs=[audio_output_long]
|
145 |
+
)
|
146 |
+
|
147 |
+
|
148 |
|
149 |
# CSS μ€νμΌ μΆκ° (κ·μ¬μ΄ ν
λ§)
|
150 |
demo.css = """
|