Spaces:
Sleeping
Sleeping
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"
} |