File size: 8,482 Bytes
3fe0be9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82307da
 
 
3fe0be9
 
 
 
82307da
3fe0be9
82307da
3fe0be9
82307da
3fe0be9
 
82307da
 
23ecab6
3fe0be9
 
82307da
3fe0be9
82307da
3fe0be9
 
 
82307da
3fe0be9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
from fastapi import FastAPI, Request, File, UploadFile, Form
from fastapi.responses import HTMLResponse
from pydantic import BaseModel
import yaml
import tempfile
import os
import traceback
from model.wav2vec2 import Wav2Vec2

# ---------------- μ„€μ • λ‘œλ“œ ----------------
with open("config/wav2vec2.yaml", "r") as f:
    config = yaml.safe_load(f)

# ---------------- λͺ¨λΈ μ΄ˆκΈ°ν™” ----------------
wav2vec2_model = Wav2Vec2(config)

# ---------------- FastAPI μ•± ----------------
app = FastAPI(
    title="Korean Speech Recognition API",
    description="FastAPI + Wav2Vec2 기반 ν•œκ΅­μ–΄ μŒμ„± 인식 μ„œλ²„",
    version="1.0.0"
)

# ---------------- μž…λ ₯ λͺ¨λΈ ----------------
class TranscriptionResponse(BaseModel):
    transcription: str
    status: str

# ---------------- API: 파일 μ—…λ‘œλ“œ POST ----------------
@app.post("/transcribe", response_model=TranscriptionResponse)
async def transcribe_audio(file: UploadFile = File(...)):
    """μ˜€λ””μ˜€ νŒŒμΌμ„ μ—…λ‘œλ“œν•˜μ—¬ μŒμ„± 인식 μˆ˜ν–‰"""
    
    # 파일 ν˜•μ‹ 검증
    if not file.filename.lower().endswith(('.wav', '.mp3', '.flac', '.m4a')):
        return TranscriptionResponse(
            transcription="",
            status="error: μ§€μ›λ˜μ§€ μ•ŠλŠ” 파일 ν˜•μ‹μž…λ‹ˆλ‹€. wav, mp3, flac, m4a 파일만 μ§€μ›λ©λ‹ˆλ‹€."
        )
    
    try:
        # 파일 λ‚΄μš© 읽기
        audio_bytes = await file.read()
        
        # μŒμ„± 인식 μˆ˜ν–‰
        result = wav2vec2_model.transcribe_from_bytes(audio_bytes, file.filename)
        
        return TranscriptionResponse(
            transcription=result,
            status="success"
        )
        
    except Exception as e:
        return TranscriptionResponse(
            transcription="",
            status=f"error: {str(e)}"
        )

# ---------------- HTML UI ----------------
@app.get("/", response_class=HTMLResponse)
async def main_ui():
    return """
    <html>
        <head>
            <title>Korean Speech Recognition</title>
            <meta charset="UTF-8">
            <style>
                body {
                    font-family: Arial, sans-serif;
                    max-width: 800px;
                    margin: auto;
                    padding: 2rem;
                    background-color: #f5f5f5;
                }
                .container {
                    background-color: white;
                    padding: 2rem;
                    border-radius: 10px;
                    box-shadow: 0 2px 10px rgba(0,0,0,0.1);
                }
                .form-group {
                    margin-bottom: 1.5rem;
                }
                label {
                    display: block;
                    margin-bottom: 0.5rem;
                    font-weight: bold;
                    color: #333;
                }
                input[type="file"] {
                    padding: 0.5rem;
                    border: 2px dashed #ccc;
                    border-radius: 5px;
                    width: 100%;
                    box-sizing: border-box;
                }
                input[type="submit"] {
                    background-color: #007bff;
                    color: white;
                    padding: 1rem 2rem;
                    border: none;
                    border-radius: 5px;
                    cursor: pointer;
                    font-size: 1rem;
                }
                input[type="submit"]:hover {
                    background-color: #0056b3;
                }
                .info {
                    background-color: #e7f3ff;
                    padding: 1rem;
                    border-radius: 5px;
                    margin-bottom: 1rem;
                    border-left: 4px solid #007bff;
                }
            </style>
        </head>
        <body>
            <div class="container">
                <h1>🎀 ν•œκ΅­μ–΄ μŒμ„± 인식</h1>
                <div class="info">
                    <strong>지원 ν˜•μ‹:</strong> WAV, MP3, FLAC, M4A<br>
                    <strong>λͺ¨λΈ:</strong> Wav2Vec2 Korean Fine-tuned
                </div>
                
                <form action="/submit" method="post" enctype="multipart/form-data">
                    <div class="form-group">
                        <label for="audio_file">🎡 μ˜€λ””μ˜€ 파일 선택:</label>
                        <input type="file" id="audio_file" name="audio_file" accept=".wav,.mp3,.flac,.m4a" required>
                    </div>
                    
                    <input type="submit" value="μŒμ„± 인식 μ‹€ν–‰">
                </form>
            </div>
        </body>
    </html>
    """

# ---------------- κ²°κ³Ό λ Œλ”λ§ ----------------
@app.post("/submit", response_class=HTMLResponse)
async def handle_form(request: Request, audio_file: UploadFile = File(...)):
    try:
        # 파일 ν˜•μ‹ 검증
        if not audio_file.filename.lower().endswith(('.wav', '.mp3', '.flac', '.m4a')):
            return f"""
            <html>
                <head><title>μ—λŸ¬</title><meta charset="UTF-8"></head>
                <body style="font-family: Arial, sans-serif; max-width: 600px; margin: auto; padding: 2rem;">
                    <h1>❌ 파일 ν˜•μ‹ 였λ₯˜</h1>
                    <p>μ§€μ›λ˜μ§€ μ•ŠλŠ” 파일 ν˜•μ‹μž…λ‹ˆλ‹€.</p>
                    <p><strong>지원 ν˜•μ‹:</strong> WAV, MP3, FLAC, M4A</p>
                    <br>
                    <a href="/" style="color: #007bff; text-decoration: none;">← λŒμ•„κ°€κΈ°</a>
                </body>
            </html>
            """
        
        # 파일 λ‚΄μš© 읽기
        audio_bytes = await audio_file.read()
        
        # μŒμ„± 인식 μˆ˜ν–‰
        result = wav2vec2_model.transcribe_from_bytes(audio_bytes, audio_file.filename)
        
    except Exception as e:
        error_details = traceback.format_exc()
        return f"""
        <html>
            <head><title>μ—λŸ¬</title><meta charset="UTF-8"></head>
            <body style="font-family: Arial, sans-serif; max-width: 600px; margin: auto; padding: 2rem;">
                <h1>❌ μ„œλ²„ 였λ₯˜ λ°œμƒ</h1>
                <p><strong>였λ₯˜ λ©”μ‹œμ§€:</strong></p>
                <pre style="background-color: #f8f9fa; padding: 1rem; border-radius: 5px; overflow-x: auto;">{str(e)}</pre>
                <hr>
                <details>
                    <summary><strong>μ—λŸ¬ 상세 (ν΄λ¦­ν•˜μ—¬ 펼치기)</strong></summary>
                    <pre style="background-color: #f8f9fa; padding: 1rem; border-radius: 5px; overflow-x: auto;">{error_details}</pre>
                </details>
                <br>
                <a href="/" style="color: #007bff; text-decoration: none;">← λŒμ•„κ°€κΈ°</a>
            </body>
        </html>
        """
    
    return f"""
    <html>
        <head><title>κ²°κ³Ό</title><meta charset="UTF-8"></head>
        <body style="font-family: Arial, sans-serif; max-width: 600px; margin: auto; padding: 2rem;">
            <h1>βœ… μŒμ„± 인식 κ²°κ³Ό</h1>
            <div style="background-color: #f8f9fa; padding: 1rem; border-radius: 5px; margin: 1rem 0;">
                <p><strong>μ—…λ‘œλ“œλœ 파일:</strong> {audio_file.filename}</p>
                <p><strong>파일 크기:</strong> {len(audio_bytes):,} bytes</p>
            </div>
            <hr>
            <h2>🎯 μΈμ‹λœ ν…μŠ€νŠΈ:</h2>
            <div style="background-color: #e7f3ff; padding: 1.5rem; border-radius: 5px; border-left: 4px solid #007bff;">
                <pre style="font-size: 1.1rem; margin: 0; white-space: pre-wrap; word-wrap: break-word;">{result}</pre>
            </div>
            <br>
            <a href="/" style="color: #007bff; text-decoration: none;">← λ‹€μ‹œ μ‹œλ„ν•˜κΈ°</a>
        </body>
    </html>
    """

# ---------------- ν—¬μŠ€ 체크 ----------------
@app.get("/health")
async def health_check():
    return {
        "status": "ok",
        "model": config["model"]["id"],
        "device": config["model"]["device"],
        "sampling_rate": config["model"]["sampling_rate"]
    }

# ---------------- λͺ¨λΈ 정보 ----------------
@app.get("/info")
async def model_info():
    return {
        "model_id": config["model"]["id"],
        "device": config["model"]["device"],
        "sampling_rate": config["model"]["sampling_rate"],
        "supported_formats": ["wav", "mp3", "flac", "m4a"],
        "description": "Korean Speech Recognition using Wav2Vec2"
    }