Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,161 +1,82 @@
|
|
| 1 |
-
|
| 2 |
-
"""
|
| 3 |
-
|
| 4 |
-
import logging
|
| 5 |
-
from fastapi import FastAPI
|
| 6 |
from fastapi.responses import HTMLResponse
|
| 7 |
-
from
|
| 8 |
-
import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
|
| 12 |
-
logger = logging.getLogger(__name__)
|
| 13 |
|
| 14 |
-
# FastAPI ์ฑ
|
| 15 |
app = FastAPI(
|
| 16 |
-
title="Korean
|
| 17 |
-
description="
|
| 18 |
version="1.0.0"
|
| 19 |
)
|
| 20 |
|
| 21 |
-
#
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
allow_credentials=True,
|
| 26 |
-
allow_methods=["*"],
|
| 27 |
-
allow_headers=["*"],
|
| 28 |
-
)
|
| 29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
@app.get("/", response_class=HTMLResponse)
|
| 31 |
-
async def
|
| 32 |
-
"""๊ธฐ๋ณธ ํ์ด์ง"""
|
| 33 |
return """
|
| 34 |
-
<!DOCTYPE html>
|
| 35 |
<html>
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
<
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
<div class="container">
|
| 52 |
-
<h1>๐ค Korean Speech Recognition API</h1>
|
| 53 |
-
<p>์ค์๊ฐ ํ๊ตญ์ด ์์ฑ ์ธ์ ์๋น์ค๊ฐ ์คํ ์ค์
๋๋ค.</p>
|
| 54 |
-
|
| 55 |
-
<h3>API ์๋ํฌ์ธํธ:</h3>
|
| 56 |
-
<div class="endpoint">WebSocket /ws - ์ค์๊ฐ ์์ฑ ์ธ์</div>
|
| 57 |
-
<div class="endpoint">POST /transcribe - ํ์ผ ์
๋ก๋ ์์ฑ ์ธ์</div>
|
| 58 |
-
<div class="endpoint">GET /health - ํฌ์ค ์ฒดํฌ</div>
|
| 59 |
-
|
| 60 |
-
<h3>WebSocket ํ
์คํธ:</h3>
|
| 61 |
-
<button onclick="testWebSocket()">WebSocket ์ฐ๊ฒฐ ํ
์คํธ</button>
|
| 62 |
-
<div id="status"></div>
|
| 63 |
-
|
| 64 |
-
<script>
|
| 65 |
-
function testWebSocket() {
|
| 66 |
-
const status = document.getElementById('status');
|
| 67 |
-
status.innerHTML = '<div class="status warning">์ฐ๊ฒฐ ์๋ ์ค...</div>';
|
| 68 |
-
|
| 69 |
-
try {
|
| 70 |
-
const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
|
| 71 |
-
const wsUrl = `${protocol}//${window.location.host}/ws`;
|
| 72 |
-
console.log('WebSocket URL:', wsUrl);
|
| 73 |
-
|
| 74 |
-
const ws = new WebSocket(wsUrl);
|
| 75 |
-
let messageReceived = false;
|
| 76 |
-
|
| 77 |
-
ws.onopen = () => {
|
| 78 |
-
console.log('WebSocket ์ฐ๊ฒฐ๋จ');
|
| 79 |
-
status.innerHTML = '<div class="status success">โ
WebSocket ์ฐ๊ฒฐ ์ฑ๊ณต! ๋ฉ์์ง ์ ์ก ์ค...</div>';
|
| 80 |
-
|
| 81 |
-
// ํ
์คํธ ๋ฉ์์ง ์ ์ก
|
| 82 |
-
ws.send('ping');
|
| 83 |
-
};
|
| 84 |
-
|
| 85 |
-
ws.onmessage = (event) => {
|
| 86 |
-
messageReceived = true;
|
| 87 |
-
console.log('๋ฐ์ ๋ฉ์์ง:', event.data);
|
| 88 |
-
try {
|
| 89 |
-
const data = JSON.parse(event.data);
|
| 90 |
-
status.innerHTML = '<div class="status success">โ
์๋ฒ ์๋ต ์์ ์ฑ๊ณต!</div>';
|
| 91 |
-
|
| 92 |
-
// 3์ด ํ ์ ์์ ์ผ๋ก ์ฐ๊ฒฐ ์ข
๋ฃ
|
| 93 |
-
setTimeout(() => {
|
| 94 |
-
if (ws.readyState === WebSocket.OPEN) {
|
| 95 |
-
ws.close(1000, 'Test completed successfully');
|
| 96 |
-
}
|
| 97 |
-
}, 3000);
|
| 98 |
-
} catch (e) {
|
| 99 |
-
status.innerHTML = '<div class="status success">โ
ํ
์คํธ ๋ฉ์์ง ์์ : ' + event.data + '</div>';
|
| 100 |
-
setTimeout(() => {
|
| 101 |
-
if (ws.readyState === WebSocket.OPEN) {
|
| 102 |
-
ws.close(1000, 'Test completed successfully');
|
| 103 |
-
}
|
| 104 |
-
}, 3000);
|
| 105 |
-
}
|
| 106 |
-
};
|
| 107 |
-
|
| 108 |
-
ws.onerror = (error) => {
|
| 109 |
-
console.error('WebSocket ์ค๋ฅ:', error);
|
| 110 |
-
status.innerHTML = '<div class="status error">โ WebSocket ์ฐ๊ฒฐ ์คํจ</div>';
|
| 111 |
-
};
|
| 112 |
-
|
| 113 |
-
ws.onclose = (event) => {
|
| 114 |
-
console.log('WebSocket ์ฐ๊ฒฐ ์ข
๋ฃ', event.code, event.reason);
|
| 115 |
-
|
| 116 |
-
if (event.code === 1000) {
|
| 117 |
-
status.innerHTML = '<div class="status success">โ
WebSocket ํ
์คํธ ์๋ฃ (์ ์ ์ข
๋ฃ)</div>';
|
| 118 |
-
} else if (messageReceived) {
|
| 119 |
-
status.innerHTML = '<div class="status success">โ
๋ฉ์์ง ๊ตํ ์ฑ๊ณต (์ฝ๋: ' + event.code + ')</div>';
|
| 120 |
-
} else {
|
| 121 |
-
status.innerHTML = '<div class="status error">โ WebSocket ์ฐ๊ฒฐ์ด ์๊ธฐ์น ์๊ฒ ์ข
๋ฃ๋จ (์ฝ๋: ' + event.code + ')</div>';
|
| 122 |
-
}
|
| 123 |
-
};
|
| 124 |
-
|
| 125 |
-
// 10์ด ํ ํ์์์
|
| 126 |
-
setTimeout(() => {
|
| 127 |
-
if (ws.readyState === WebSocket.CONNECTING || ws.readyState === WebSocket.OPEN) {
|
| 128 |
-
if (!messageReceived) {
|
| 129 |
-
status.innerHTML = '<div class="status error">โ ์๋ต ํ์์์</div>';
|
| 130 |
-
ws.close();
|
| 131 |
-
}
|
| 132 |
-
}
|
| 133 |
-
}, 10000);
|
| 134 |
-
|
| 135 |
-
} catch (error) {
|
| 136 |
-
console.error('WebSocket ํ
์คํธ ์ค๋ฅ:', error);
|
| 137 |
-
status.innerHTML = '<div class="status error">โ WebSocket ํ
์คํธ ์คํจ: ' + error.message + '</div>';
|
| 138 |
-
}
|
| 139 |
-
}
|
| 140 |
-
</script>
|
| 141 |
-
</div>
|
| 142 |
-
</body>
|
| 143 |
</html>
|
| 144 |
"""
|
| 145 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 146 |
@app.get("/health")
|
| 147 |
async def health_check():
|
| 148 |
-
"""ํฌ์ค ์ฒดํฌ"""
|
| 149 |
return {
|
| 150 |
-
"status": "
|
| 151 |
-
"model": "
|
| 152 |
-
"
|
| 153 |
-
"version": "1.0.0",
|
| 154 |
}
|
| 155 |
-
|
| 156 |
-
@app.on_event("startup")
|
| 157 |
-
async def startup_event():
|
| 158 |
-
logger.info("๐ Korean Speech Recognition API ์๋ฒ ์์๋จ")
|
| 159 |
-
|
| 160 |
-
if __name__ == "__main__":
|
| 161 |
-
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
| 1 |
+
from fastapi import FastAPI, Request, Form
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
from fastapi.responses import HTMLResponse
|
| 3 |
+
from pydantic import BaseModel
|
| 4 |
+
import yaml
|
| 5 |
+
from model.llama3 import LLaMA3
|
| 6 |
+
|
| 7 |
+
# ---------------- ์ค์ ๋ก๋ ----------------
|
| 8 |
+
with open("data/config/llama3.yaml", "r") as f:
|
| 9 |
+
config = yaml.safe_load(f)
|
| 10 |
|
| 11 |
+
# ---------------- ๋ชจ๋ธ ์ด๊ธฐํ ----------------
|
| 12 |
+
llama3 = LLaMA3(config)
|
|
|
|
| 13 |
|
| 14 |
+
# ---------------- FastAPI ์ฑ ----------------
|
| 15 |
app = FastAPI(
|
| 16 |
+
title="Korean Pronunciation Correction API",
|
| 17 |
+
description="FastAPI + LLaMA3 ๊ธฐ๋ฐ ๋ฐ์ ๊ต์ ์๋ฒ",
|
| 18 |
version="1.0.0"
|
| 19 |
)
|
| 20 |
|
| 21 |
+
# ---------------- ์
๋ ฅ ๋ชจ๋ธ ----------------
|
| 22 |
+
class InputData(BaseModel):
|
| 23 |
+
user_input: str
|
| 24 |
+
correct_input: str
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
+
# ---------------- API: JSON POST ----------------
|
| 27 |
+
@app.post("/generate")
|
| 28 |
+
async def generate_correction(data: InputData):
|
| 29 |
+
result = llama3.generate(data.user_input, data.correct_input)
|
| 30 |
+
return {"result": result}
|
| 31 |
+
|
| 32 |
+
# ---------------- HTML UI ----------------
|
| 33 |
@app.get("/", response_class=HTMLResponse)
|
| 34 |
+
async def form_ui():
|
|
|
|
| 35 |
return """
|
|
|
|
| 36 |
<html>
|
| 37 |
+
<head>
|
| 38 |
+
<title>Korean Pronunciation Correction</title>
|
| 39 |
+
</head>
|
| 40 |
+
<body style="font-family: sans-serif; max-width: 600px; margin: auto; padding: 2rem;">
|
| 41 |
+
<h1>๐ฃ๏ธ ๋ฐ์ ๊ต์ ํ
์คํธ</h1>
|
| 42 |
+
<form action="/submit" method="post">
|
| 43 |
+
<label for="user_input">๐ง ์๋ชป๋ ๋ฐ์:</label><br>
|
| 44 |
+
<input type="text" id="user_input" name="user_input" value="๋ฐ๋ผ" required><br><br>
|
| 45 |
+
|
| 46 |
+
<label for="correct_input">๐ฏ ์ฌ๋ฐ๋ฅธ ๋ฐ์:</label><br>
|
| 47 |
+
<input type="text" id="correct_input" name="correct_input" value="๋ฐ๋ผ" required><br><br>
|
| 48 |
+
|
| 49 |
+
<input type="submit" value="๊ต์ ์คํ">
|
| 50 |
+
</form>
|
| 51 |
+
</body>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
</html>
|
| 53 |
"""
|
| 54 |
|
| 55 |
+
# ---------------- ๊ฒฐ๊ณผ ๋ ๋๋ง ----------------
|
| 56 |
+
@app.post("/submit", response_class=HTMLResponse)
|
| 57 |
+
async def handle_form(user_input: str = Form(...), correct_input: str = Form(...)):
|
| 58 |
+
result = llama3.generate(user_input, correct_input)
|
| 59 |
+
return f"""
|
| 60 |
+
<html>
|
| 61 |
+
<head><title>๊ฒฐ๊ณผ</title></head>
|
| 62 |
+
<body style="font-family: sans-serif; max-width: 600px; margin: auto; padding: 2rem;">
|
| 63 |
+
<h1>โ
๊ฒฐ๊ณผ</h1>
|
| 64 |
+
<p><strong>์
๋ ฅ๋ ๋ฐ์:</strong> {user_input}</p>
|
| 65 |
+
<p><strong>์ ๋ต ๋ฐ์:</strong> {correct_input}</p>
|
| 66 |
+
<hr>
|
| 67 |
+
<h2>๐ง ๋ชจ๋ธ ์๋ต:</h2>
|
| 68 |
+
<pre>{result}</pre>
|
| 69 |
+
<br>
|
| 70 |
+
<a href="/">๋ค์ ์๋ํ๊ธฐ</a>
|
| 71 |
+
</body>
|
| 72 |
+
</html>
|
| 73 |
+
"""
|
| 74 |
+
|
| 75 |
+
# ---------------- ํฌ์ค ์ฒดํฌ ----------------
|
| 76 |
@app.get("/health")
|
| 77 |
async def health_check():
|
|
|
|
| 78 |
return {
|
| 79 |
+
"status": "ok",
|
| 80 |
+
"model": config["model"]["id"],
|
| 81 |
+
"device": config["model"]["device"]
|
|
|
|
| 82 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|