Delete app.py
Browse files
app.py
DELETED
@@ -1,216 +0,0 @@
|
|
1 |
-
import spaces
|
2 |
-
import torch
|
3 |
-
import gradio as gr
|
4 |
-
from transformers import pipeline
|
5 |
-
from huggingface_hub import InferenceClient
|
6 |
-
import os
|
7 |
-
from datetime import datetime
|
8 |
-
|
9 |
-
MODEL_NAME = "openai/whisper-large-v3-turbo"
|
10 |
-
BATCH_SIZE = 8
|
11 |
-
FILE_LIMIT_MB = 1000
|
12 |
-
|
13 |
-
device = 0 if torch.cuda.is_available() else "cpu"
|
14 |
-
|
15 |
-
# Whisper ํ์ดํ๋ผ์ธ ์ด๊ธฐํ
|
16 |
-
pipe = pipeline(
|
17 |
-
task="automatic-speech-recognition",
|
18 |
-
model=MODEL_NAME,
|
19 |
-
chunk_length_s=30,
|
20 |
-
device=device,
|
21 |
-
)
|
22 |
-
|
23 |
-
# Hugging Face ์ถ๋ก ํด๋ผ์ด์ธํธ ์ค์
|
24 |
-
hf_client = InferenceClient(
|
25 |
-
"CohereForAI/c4ai-command-r-plus-08-2024",
|
26 |
-
token=os.getenv("HF_TOKEN")
|
27 |
-
)
|
28 |
-
|
29 |
-
def get_word_count(text):
|
30 |
-
"""ํ
์คํธ์ ๋จ์ด ์ ๊ณ์ฐ"""
|
31 |
-
if not text:
|
32 |
-
return 0
|
33 |
-
return len(text.split())
|
34 |
-
|
35 |
-
def format_duration(seconds):
|
36 |
-
"""์ด ๋จ์ ์๊ฐ์ mm:ss ํ์์ผ๋ก ๋ณํ"""
|
37 |
-
try:
|
38 |
-
minutes = int(seconds // 60)
|
39 |
-
seconds = int(seconds % 60)
|
40 |
-
return f"{minutes:02d}:{seconds:02d}"
|
41 |
-
except:
|
42 |
-
return "00:00"
|
43 |
-
|
44 |
-
@spaces.GPU
|
45 |
-
def transcribe_summarize(audio_input, task):
|
46 |
-
if audio_input is None:
|
47 |
-
raise gr.Error("์ค๋์ค ํ์ผ์ด ์ ์ถ๋์ง ์์์ต๋๋ค!")
|
48 |
-
|
49 |
-
try:
|
50 |
-
# ์์ฑ์ ํ
์คํธ๋ก ๋ณํ
|
51 |
-
result = pipe(
|
52 |
-
audio_input,
|
53 |
-
batch_size=BATCH_SIZE,
|
54 |
-
generate_kwargs={"task": task},
|
55 |
-
return_timestamps=True
|
56 |
-
)
|
57 |
-
transcribed_text = result["text"]
|
58 |
-
|
59 |
-
# ๊ธฐ๋ณธ ๋ถ์ ์ ๋ณด
|
60 |
-
word_count = get_word_count(transcribed_text)
|
61 |
-
duration = format_duration(result.get("duration", 0))
|
62 |
-
|
63 |
-
# ํ
์คํธ ์์ฝ
|
64 |
-
try:
|
65 |
-
prompt = (
|
66 |
-
"๋ค์ ํ
์คํธ๋ฅผ ํ๊ตญ์ด๋ก ๊ฐ๋จํ ์์ฝํด์ฃผ์ธ์:\n\n"
|
67 |
-
f"ํ
์คํธ: {transcribed_text}\n"
|
68 |
-
"์์ฝ:"
|
69 |
-
)
|
70 |
-
|
71 |
-
response = hf_client.text_generation(
|
72 |
-
prompt=prompt,
|
73 |
-
max_new_tokens=500,
|
74 |
-
temperature=0.3,
|
75 |
-
top_p=0.9,
|
76 |
-
repetition_penalty=1.2
|
77 |
-
)
|
78 |
-
|
79 |
-
summary_text = str(response)
|
80 |
-
if "์์ฝ:" in summary_text:
|
81 |
-
summary_text = summary_text.split("์์ฝ:")[1].strip()
|
82 |
-
|
83 |
-
except Exception as e:
|
84 |
-
print(f"์์ฝ ์์ฑ ์ค ์ค๋ฅ: {str(e)}")
|
85 |
-
summary_text = "์์ฝ์ ์์ฑํ ์ ์์ต๋๋ค."
|
86 |
-
|
87 |
-
# ๋ถ์ ์ ๋ณด ํฌ๋งทํ
|
88 |
-
stats = f"""
|
89 |
-
๐ ๋ถ์ ์ ๋ณด:
|
90 |
-
- ๋จ์ด ์: {word_count}๊ฐ
|
91 |
-
- ์์ฑ ๊ธธ์ด: {duration}
|
92 |
-
- ์์ฑ ์๊ฐ: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}
|
93 |
-
"""
|
94 |
-
|
95 |
-
return [transcribed_text, summary_text, stats]
|
96 |
-
|
97 |
-
except Exception as e:
|
98 |
-
error_msg = f"์ฒ๋ฆฌ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}"
|
99 |
-
return ["", error_msg, ""]
|
100 |
-
|
101 |
-
# CSS ์คํ์ผ
|
102 |
-
css = """
|
103 |
-
footer { visibility: hidden; }
|
104 |
-
.gradio-container {
|
105 |
-
max-width: 1000px;
|
106 |
-
margin: auto;
|
107 |
-
padding: 20px;
|
108 |
-
}
|
109 |
-
.output-stats {
|
110 |
-
background-color: #f5f5f5;
|
111 |
-
padding: 10px;
|
112 |
-
border-radius: 5px;
|
113 |
-
font-family: monospace;
|
114 |
-
}
|
115 |
-
"""
|
116 |
-
|
117 |
-
# ํ์ผ ์
๋ก๋ ์ธํฐํ์ด์ค
|
118 |
-
file_transcribe = gr.Interface(
|
119 |
-
fn=transcribe_summarize,
|
120 |
-
inputs=[
|
121 |
-
gr.Audio(
|
122 |
-
sources="upload",
|
123 |
-
type="filepath",
|
124 |
-
label="์ค๋์ค ํ์ผ"
|
125 |
-
),
|
126 |
-
gr.Radio(
|
127 |
-
choices=["transcribe", "translate"],
|
128 |
-
label="์์
์ ํ",
|
129 |
-
value="transcribe"
|
130 |
-
)
|
131 |
-
],
|
132 |
-
outputs=[
|
133 |
-
gr.Textbox(
|
134 |
-
label="๋ณํ๋ ํ
์คํธ",
|
135 |
-
lines=5,
|
136 |
-
placeholder="์์ฑ์ด ํ
์คํธ๋ก ๋ณํ๋์ด ์ฌ๊ธฐ์ ํ์๋ฉ๋๋ค..."
|
137 |
-
),
|
138 |
-
gr.Textbox(
|
139 |
-
label="์์ฝ",
|
140 |
-
lines=3,
|
141 |
-
placeholder="ํ
์คํธ ์์ฝ์ด ์ฌ๊ธฐ์ ํ์๋ฉ๋๋ค..."
|
142 |
-
),
|
143 |
-
gr.Textbox(
|
144 |
-
label="๋ถ์ ์ ๋ณด",
|
145 |
-
lines=4,
|
146 |
-
placeholder="๋ถ์ ์ ๋ณด๊ฐ ์ฌ๊ธฐ์ ํ์๋ฉ๋๋ค..."
|
147 |
-
)
|
148 |
-
],
|
149 |
-
title="๐ค ๋ฐ์์ฐ๊ธฐ AI",
|
150 |
-
description="""
|
151 |
-
์์ฑ ํ์ผ์ ์
๋ก๋ํ๊ฑฐ๋ ์ง์ ๋
น์ํ์ฌ ํ
์คํธ๋ก ๋ณํํ๊ณ ์์ฝํ ์ ์์ต๋๋ค.
|
152 |
-
|
153 |
-
์ฌ์ฉ ๋ฐฉ๋ฒ:
|
154 |
-
1. ์ค๋์ค ํ์ผ์ ์
๋ก๋ํ๊ฑฐ๋ ๋ง์ดํฌ๋ก ๋
น์ํ์ธ์
|
155 |
-
2. ์์
์ ํ์ ์ ํํ์ธ์ (๋ณํ ๋๋ ๋ฒ์ญ)
|
156 |
-
3. ๋ณํ ๋ฒํผ์ ํด๋ฆญํ์ธ์
|
157 |
-
""",
|
158 |
-
article="developed by Claude",
|
159 |
-
examples=[],
|
160 |
-
cache_examples=False,
|
161 |
-
flagging_mode="never"
|
162 |
-
)
|
163 |
-
|
164 |
-
# ๋ง์ดํฌ ๋
น์ ์ธํฐํ์ด์ค
|
165 |
-
mic_transcribe = gr.Interface(
|
166 |
-
fn=transcribe_summarize,
|
167 |
-
inputs=[
|
168 |
-
gr.Audio(
|
169 |
-
sources="microphone",
|
170 |
-
type="filepath",
|
171 |
-
label="๋ง์ดํฌ ๋
น์"
|
172 |
-
),
|
173 |
-
gr.Radio(
|
174 |
-
choices=["transcribe", "translate"],
|
175 |
-
label="์์
์ ํ",
|
176 |
-
value="transcribe"
|
177 |
-
)
|
178 |
-
],
|
179 |
-
outputs=[
|
180 |
-
gr.Textbox(
|
181 |
-
label="๋ณํ๋ ํ
์คํธ",
|
182 |
-
lines=5,
|
183 |
-
placeholder="์์ฑ์ด ํ
์คํธ๋ก ๋ณํ๋์ด ์ฌ๊ธฐ์ ํ์๋ฉ๋๋ค..."
|
184 |
-
),
|
185 |
-
gr.Textbox(
|
186 |
-
label="์์ฝ",
|
187 |
-
lines=3,
|
188 |
-
placeholder="ํ
์คํธ ์์ฝ์ด ์ฌ๊ธฐ์ ํ์๋ฉ๋๋ค..."
|
189 |
-
),
|
190 |
-
gr.Textbox(
|
191 |
-
label="๋ถ์ ์ ๋ณด",
|
192 |
-
lines=4,
|
193 |
-
placeholder="๋ถ์ ์ ๋ณด๊ฐ ์ฌ๊ธฐ์ ํ์๋ฉ๋๋ค..."
|
194 |
-
)
|
195 |
-
],
|
196 |
-
title="๐ค ๋ฐ์์ฐ๊ธฐ AI",
|
197 |
-
description="๋ง์ดํฌ๋ก ์์ฑ์ ๋
น์ํ์ฌ ํ
์คํธ๋ก ๋ณํํ๊ณ ์์ฝํ ์ ์์ต๋๋ค.",
|
198 |
-
flagging_mode="never",
|
199 |
-
css=css
|
200 |
-
)
|
201 |
-
|
202 |
-
# ๋ฉ์ธ ์ ํ๋ฆฌ์ผ์ด์
|
203 |
-
demo = gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css)
|
204 |
-
with demo:
|
205 |
-
gr.TabbedInterface(
|
206 |
-
[file_transcribe, mic_transcribe],
|
207 |
-
["์ค๋์ค ํ์ผ", "๋ง์ดํฌ ๋
น์"]
|
208 |
-
)
|
209 |
-
|
210 |
-
# ์ ํ๋ฆฌ์ผ์ด์
์คํ
|
211 |
-
demo.queue().launch(
|
212 |
-
share=False,
|
213 |
-
debug=True,
|
214 |
-
show_error=True,
|
215 |
-
ssr_mode=False
|
216 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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