englissi commited on
Commit
11cf154
Β·
verified Β·
1 Parent(s): 9b0ebaa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -38
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
- audio_file = generate_audio(current_text)
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("πŸ‘‰ λ‹€μŒ 이야기", elem_classes=["btn-custom", "next-btn"])
113
- play_button_short = gr.Button("πŸ”Š λ‹€μ‹œ λ“£κΈ°", elem_classes=["btn-custom", "play-btn"])
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("πŸ‘‰ λ‹€μŒ 이야기", elem_classes=["btn-custom", "next-btn"])
137
- play_button_long = gr.Button("πŸ”Š λ‹€μ‹œ λ“£κΈ°", elem_classes=["btn-custom", "play-btn"])
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()