Update app.py
Browse files
app.py
CHANGED
|
@@ -4,10 +4,15 @@ import gradio as gr
|
|
| 4 |
from transformers import pipeline
|
| 5 |
from huggingface_hub import InferenceClient
|
| 6 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
MODEL_NAME = "openai/whisper-large-v3-turbo"
|
| 9 |
BATCH_SIZE = 8
|
| 10 |
FILE_LIMIT_MB = 1000
|
|
|
|
| 11 |
|
| 12 |
device = 0 if torch.cuda.is_available() else "cpu"
|
| 13 |
|
|
@@ -25,83 +30,140 @@ hf_client = InferenceClient(
|
|
| 25 |
token=os.getenv("HF_TOKEN")
|
| 26 |
)
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
| 32 |
|
| 33 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
result = pipe(
|
| 35 |
-
|
| 36 |
batch_size=BATCH_SIZE,
|
| 37 |
generate_kwargs={"task": task},
|
| 38 |
return_timestamps=True
|
| 39 |
)
|
| 40 |
-
transcribed_text = result["text"]
|
| 41 |
|
| 42 |
-
#
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
|
| 47 |
-
|
|
|
|
|
|
|
| 48 |
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
-
#
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
top_p=0.9,
|
| 58 |
-
repetition_penalty=1.2,
|
| 59 |
-
stop_sequences=["\n", "ν
μ€νΈ:", "μμ½:"]
|
| 60 |
-
)
|
| 61 |
|
| 62 |
-
#
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
summary_text = summary_text.split("μμ½:")[1].strip()
|
| 71 |
|
| 72 |
-
if not summary_text:
|
| 73 |
-
summary_text = "μμ½μ μμ±ν μ μμ΅λλ€."
|
| 74 |
-
|
| 75 |
except Exception as e:
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
print(f"λ³νλ ν
μ€νΈ: {transcribed_text}") # λλ²κΉ
μ© λ‘κ·Έ
|
| 80 |
-
print(f"μμ±λ μμ½: {summary_text}") # λλ²κΉ
μ© λ‘κ·Έ
|
| 81 |
-
|
| 82 |
-
return [transcribed_text, summary_text]
|
| 83 |
|
| 84 |
# CSS μ€νμΌ
|
| 85 |
css = """
|
| 86 |
footer { visibility: hidden; }
|
|
|
|
|
|
|
| 87 |
"""
|
| 88 |
|
| 89 |
# νμΌ μ
λ‘λ μΈν°νμ΄μ€
|
| 90 |
file_transcribe = gr.Interface(
|
| 91 |
fn=transcribe_summarize,
|
| 92 |
inputs=[
|
| 93 |
-
gr.Audio(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
gr.Radio(
|
| 95 |
choices=["transcribe", "translate"],
|
| 96 |
label="μμ
",
|
| 97 |
value="transcribe"
|
| 98 |
-
)
|
| 99 |
],
|
| 100 |
outputs=[
|
| 101 |
-
gr.Textbox(
|
| 102 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
],
|
| 104 |
-
title="λ°μμ°κΈ° AI:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
flagging_mode="never"
|
| 106 |
)
|
| 107 |
|
|
@@ -109,24 +171,34 @@ file_transcribe = gr.Interface(
|
|
| 109 |
mic_transcribe = gr.Interface(
|
| 110 |
fn=transcribe_summarize,
|
| 111 |
inputs=[
|
| 112 |
-
gr.Audio(
|
|
|
|
|
|
|
|
|
|
| 113 |
gr.Radio(
|
| 114 |
choices=["transcribe", "translate"],
|
| 115 |
label="μμ
",
|
| 116 |
value="transcribe"
|
| 117 |
-
)
|
| 118 |
],
|
| 119 |
outputs=[
|
| 120 |
-
gr.Textbox(
|
| 121 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
],
|
| 123 |
-
title="λ°μμ°κΈ° AI:
|
| 124 |
flagging_mode="never",
|
| 125 |
css=css
|
| 126 |
)
|
| 127 |
|
| 128 |
# λ©μΈ μ ν리μΌμ΄μ
|
| 129 |
-
demo = gr.Blocks(theme="
|
| 130 |
with demo:
|
| 131 |
gr.TabbedInterface(
|
| 132 |
[file_transcribe, mic_transcribe],
|
|
@@ -134,4 +206,8 @@ with demo:
|
|
| 134 |
)
|
| 135 |
|
| 136 |
# μ ν리μΌμ΄μ
μ€ν
|
| 137 |
-
demo.queue().launch(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
from transformers import pipeline
|
| 5 |
from huggingface_hub import InferenceClient
|
| 6 |
import os
|
| 7 |
+
import numpy as np
|
| 8 |
+
from pydub import AudioSegment
|
| 9 |
+
import tempfile
|
| 10 |
+
import math
|
| 11 |
|
| 12 |
MODEL_NAME = "openai/whisper-large-v3-turbo"
|
| 13 |
BATCH_SIZE = 8
|
| 14 |
FILE_LIMIT_MB = 1000
|
| 15 |
+
CHUNK_LENGTH = 10 * 60 # 10λΆ λ¨μλ‘ λΆν
|
| 16 |
|
| 17 |
device = 0 if torch.cuda.is_available() else "cpu"
|
| 18 |
|
|
|
|
| 30 |
token=os.getenv("HF_TOKEN")
|
| 31 |
)
|
| 32 |
|
| 33 |
+
def split_audio(audio_path, chunk_length=CHUNK_LENGTH):
|
| 34 |
+
"""μ€λμ€ νμΌμ μ²ν¬λ‘ λΆν """
|
| 35 |
+
audio = AudioSegment.from_file(audio_path)
|
| 36 |
+
duration = len(audio) / 1000 # μ΄ λ¨μ λ³ν
|
| 37 |
+
chunks = []
|
| 38 |
|
| 39 |
+
# μ²ν¬ κ°μ κ³μ°
|
| 40 |
+
num_chunks = math.ceil(duration / chunk_length)
|
| 41 |
+
|
| 42 |
+
for i in range(num_chunks):
|
| 43 |
+
start_time = i * chunk_length * 1000 # milliseconds
|
| 44 |
+
end_time = min((i + 1) * chunk_length * 1000, len(audio))
|
| 45 |
+
|
| 46 |
+
chunk = audio[start_time:end_time]
|
| 47 |
+
|
| 48 |
+
# μμ νμΌλ‘ μ μ₯
|
| 49 |
+
with tempfile.NamedTemporaryFile(suffix='.wav', delete=False) as temp_file:
|
| 50 |
+
chunk.export(temp_file.name, format='wav')
|
| 51 |
+
chunks.append(temp_file.name)
|
| 52 |
+
|
| 53 |
+
return chunks, num_chunks
|
| 54 |
+
|
| 55 |
+
def process_chunk(chunk_path, task):
|
| 56 |
+
"""κ°λ³ μ²ν¬ μ²λ¦¬"""
|
| 57 |
result = pipe(
|
| 58 |
+
chunk_path,
|
| 59 |
batch_size=BATCH_SIZE,
|
| 60 |
generate_kwargs={"task": task},
|
| 61 |
return_timestamps=True
|
| 62 |
)
|
|
|
|
| 63 |
|
| 64 |
+
# μμ νμΌ μμ
|
| 65 |
+
os.unlink(chunk_path)
|
| 66 |
+
|
| 67 |
+
return result["text"]
|
| 68 |
|
| 69 |
+
def update_progress(progress):
|
| 70 |
+
"""μ§ν μν© μ
λ°μ΄νΈ"""
|
| 71 |
+
return f"μ²λ¦¬ μ€... {progress}% μλ£"
|
| 72 |
|
| 73 |
+
@spaces.GPU
|
| 74 |
+
def transcribe_summarize(audio_input, task, progress=gr.Progress()):
|
| 75 |
+
if audio_input is None:
|
| 76 |
+
raise gr.Error("μ€λμ€ νμΌμ΄ μ μΆλμ§ μμμ΅λλ€!")
|
| 77 |
+
|
| 78 |
+
try:
|
| 79 |
+
# μ€λμ€ νμΌ λΆν
|
| 80 |
+
chunks, num_chunks = split_audio(audio_input)
|
| 81 |
+
progress(0, desc="μ€λμ€ νμΌ λΆν μλ£")
|
| 82 |
|
| 83 |
+
# κ° μ²ν¬ μ²λ¦¬
|
| 84 |
+
transcribed_texts = []
|
| 85 |
+
for i, chunk in enumerate(chunks):
|
| 86 |
+
chunk_text = process_chunk(chunk, task)
|
| 87 |
+
transcribed_texts.append(chunk_text)
|
| 88 |
+
progress((i + 1) / num_chunks, desc=f"μ²ν¬ {i+1}/{num_chunks} μ²λ¦¬ μ€")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
|
| 90 |
+
# μ 체 ν
μ€νΈ μ‘°ν©
|
| 91 |
+
transcribed_text = " ".join(transcribed_texts)
|
| 92 |
+
progress(0.9, desc="ν
μ€νΈ λ³ν μλ£")
|
| 93 |
+
|
| 94 |
+
# ν
μ€νΈ μμ½
|
| 95 |
+
try:
|
| 96 |
+
# κΈ΄ ν
μ€νΈλ₯Ό μν μμ½ ν둬ννΈ
|
| 97 |
+
prompt = f"""λ€μ κΈ΄ ν
μ€νΈλ₯Ό μ£Όμ λ΄μ© μ€μ¬μΌλ‘ κ°λ¨ν μμ½ν΄μ£ΌμΈμ:
|
| 98 |
+
ν
μ€νΈ: {transcribed_text[:3000]}... # ν
μ€νΈκ° λ무 κΈΈ κ²½μ° μλΆλΆλ§ μμ½
|
| 99 |
+
μμ½:"""
|
| 100 |
+
|
| 101 |
+
response = hf_client.text_generation(
|
| 102 |
+
model="CohereForAI/c4ai-command-r-plus-08-2024",
|
| 103 |
+
prompt=prompt,
|
| 104 |
+
max_new_tokens=250,
|
| 105 |
+
temperature=0.3,
|
| 106 |
+
top_p=0.9,
|
| 107 |
+
repetition_penalty=1.2,
|
| 108 |
+
stop_sequences=["\n", "ν
μ€νΈ:", "μμ½:"]
|
| 109 |
+
)
|
| 110 |
+
|
| 111 |
+
summary_text = str(response)
|
| 112 |
+
if "μμ½:" in summary_text:
|
| 113 |
+
summary_text = summary_text.split("μμ½:")[1].strip()
|
| 114 |
+
|
| 115 |
+
except Exception as e:
|
| 116 |
+
print(f"μμ½ μμ± μ€ μ€λ₯ λ°μ: {str(e)}")
|
| 117 |
+
summary_text = "μμ½μ μμ±ν μ μμ΅λλ€. ν
μ€νΈκ° λ무 κΈΈκ±°λ μ²λ¦¬ μ€ μ€λ₯κ° λ°μνμ΅λλ€."
|
| 118 |
|
| 119 |
+
progress(1.0, desc="μ²λ¦¬ μλ£")
|
| 120 |
+
return [transcribed_text, summary_text]
|
|
|
|
| 121 |
|
|
|
|
|
|
|
|
|
|
| 122 |
except Exception as e:
|
| 123 |
+
error_msg = f"μμ± μ²λ¦¬ μ€ μ€λ₯κ° λ°μνμ΅λλ€: {str(e)}"
|
| 124 |
+
return ["", error_msg]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
|
| 126 |
# CSS μ€νμΌ
|
| 127 |
css = """
|
| 128 |
footer { visibility: hidden; }
|
| 129 |
+
.progress-bar { height: 15px; border-radius: 5px; }
|
| 130 |
+
.container { max-width: 1200px; margin: auto; padding: 20px; }
|
| 131 |
"""
|
| 132 |
|
| 133 |
# νμΌ μ
λ‘λ μΈν°νμ΄μ€
|
| 134 |
file_transcribe = gr.Interface(
|
| 135 |
fn=transcribe_summarize,
|
| 136 |
inputs=[
|
| 137 |
+
gr.Audio(
|
| 138 |
+
sources="upload",
|
| 139 |
+
type="filepath",
|
| 140 |
+
label="μ€λμ€ νμΌ"
|
| 141 |
+
),
|
| 142 |
gr.Radio(
|
| 143 |
choices=["transcribe", "translate"],
|
| 144 |
label="μμ
",
|
| 145 |
value="transcribe"
|
| 146 |
+
)
|
| 147 |
],
|
| 148 |
outputs=[
|
| 149 |
+
gr.Textbox(
|
| 150 |
+
label="λ³νλ ν
μ€νΈ",
|
| 151 |
+
lines=10,
|
| 152 |
+
max_lines=30,
|
| 153 |
+
placeholder="μμ±μ΄ ν
μ€νΈλ‘ λ³νλμ΄ μ¬κΈ°μ νμλ©λλ€..."
|
| 154 |
+
),
|
| 155 |
+
gr.Textbox(
|
| 156 |
+
label="μμ½",
|
| 157 |
+
lines=5,
|
| 158 |
+
placeholder="ν
μ€νΈ μμ½μ΄ μ¬κΈ°μ νμλ©λλ€..."
|
| 159 |
+
)
|
| 160 |
],
|
| 161 |
+
title="λ°μμ°κΈ° AI: μ₯μκ° μμ± λ³ν λ° μμ½",
|
| 162 |
+
description="""
|
| 163 |
+
κΈ΄ μμ± νμΌ(1μκ° μ΄μ)λ μ²λ¦¬ν μ μμ΅λλ€.
|
| 164 |
+
μ²λ¦¬ μκ°μ νμΌ κΈΈμ΄μ λΉλ‘νμ¬ μ¦κ°ν©λλ€.
|
| 165 |
+
λ³ν μ€μλ μ§ν μν©μ΄ νμλ©λλ€.
|
| 166 |
+
""",
|
| 167 |
flagging_mode="never"
|
| 168 |
)
|
| 169 |
|
|
|
|
| 171 |
mic_transcribe = gr.Interface(
|
| 172 |
fn=transcribe_summarize,
|
| 173 |
inputs=[
|
| 174 |
+
gr.Audio(
|
| 175 |
+
sources="microphone",
|
| 176 |
+
type="filepath"
|
| 177 |
+
),
|
| 178 |
gr.Radio(
|
| 179 |
choices=["transcribe", "translate"],
|
| 180 |
label="μμ
",
|
| 181 |
value="transcribe"
|
| 182 |
+
)
|
| 183 |
],
|
| 184 |
outputs=[
|
| 185 |
+
gr.Textbox(
|
| 186 |
+
label="λ³νλ ν
μ€νΈ",
|
| 187 |
+
lines=10,
|
| 188 |
+
max_lines=30
|
| 189 |
+
),
|
| 190 |
+
gr.Textbox(
|
| 191 |
+
label="μμ½",
|
| 192 |
+
lines=5
|
| 193 |
+
)
|
| 194 |
],
|
| 195 |
+
title="λ°μμ°κΈ° AI: μμ± λ
Ήμ λ° λ³ν",
|
| 196 |
flagging_mode="never",
|
| 197 |
css=css
|
| 198 |
)
|
| 199 |
|
| 200 |
# λ©μΈ μ ν리μΌμ΄μ
|
| 201 |
+
demo = gr.Blocks(theme="gradio/soft", css=css)
|
| 202 |
with demo:
|
| 203 |
gr.TabbedInterface(
|
| 204 |
[file_transcribe, mic_transcribe],
|
|
|
|
| 206 |
)
|
| 207 |
|
| 208 |
# μ ν리μΌμ΄μ
μ€ν
|
| 209 |
+
demo.queue(concurrency_count=1).launch(
|
| 210 |
+
share=False,
|
| 211 |
+
debug=True,
|
| 212 |
+
ssr_mode=False
|
| 213 |
+
)
|