englissi commited on
Commit
7ad7fe5
Β·
verified Β·
1 Parent(s): 139bc41

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +85 -48
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
- stories = [
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 = stories[index]
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(stories)
59
- story = stories[new_index]
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
- init_index, init_text, init_image, init_audio = init_story()
 
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
- state_index = gr.State(value=init_index)
82
- state_text = gr.State(value=init_text)
83
-
84
- # UI μ»΄ν¬λ„ŒνŠΈ 생성
85
- story_text = gr.Markdown(value=format_story_text(init_text), label="μŠ€ν† λ¦¬")
86
-
87
- story_image = gr.Image(
88
- value=init_image, label="이미지", type="filepath", width=400, height=400,
89
- container=True
90
- )
91
-
92
- audio_output = gr.Audio(value=init_audio, label="🎧 μŒμ„± μž¬μƒ", type="filepath", autoplay=True)
93
-
94
- with gr.Row():
95
- next_button = gr.Button("πŸ‘‰ λ‹€μŒ 이야기", elem_id="next-btn")
96
- play_button = gr.Button("πŸ”Š λ‹€μ‹œ λ“£κΈ°", elem_id="play-btn")
97
-
98
- # "λ‹€μŒ" λ²„νŠΌ 클릭 이벀트 처리
99
- next_button.click(
100
- fn=next_story,
101
- inputs=[state_index],
102
- outputs=[state_index, story_text, story_image, audio_output, state_text]
103
- )
104
-
105
- # "μž¬μƒ" λ²„νŠΌ 클릭 이벀트 처리
106
- play_button.click(
107
- fn=play_story,
108
- inputs=[state_text],
109
- outputs=[audio_output]
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 = """