Skip to content

Instantly share code, notes, and snippets.

@hunzo
Last active March 11, 2025 08:31
Show Gist options
  • Save hunzo/cd364f0178c4afa7e65c7284f3c3608c to your computer and use it in GitHub Desktop.
Save hunzo/cd364f0178c4afa7e65c7284f3c3608c to your computer and use it in GitHub Desktop.

install

pip install fastapi[standard] uvicorn openai-whisper websockets
from fastapi import FastAPI, WebSocket, UploadFile, File
import whisper
import os
import time
app = FastAPI()
model = whisper.load_model("large", device="cpu")
UPLOAD_DIR = "uploads"
OUTPUT_DIR = "output"
os.makedirs(UPLOAD_DIR, exist_ok=True)
os.makedirs(OUTPUT_DIR, exist_ok=True)
clients = {}
@app.websocket("/ws/{client_id}")
async def websocket_endpoint(websocket: WebSocket, client_id: str):
await websocket.accept()
clients[client_id] = websocket
try:
while True:
await websocket.receive_text()
except:
del clients[client_id]
@app.post("/upload/")
async def upload_audio(file: UploadFile = File(...)):
file_path = os.path.join(UPLOAD_DIR, file.filename)
with open(file_path, "wb") as f:
f.write(await file.read())
client_id = file.filename
if client_id in clients:
await clients[client_id].send_json({"progress": 10})
result = model.transcribe(file_path, language="th")
text_path = os.path.join(OUTPUT_DIR, file.filename + ".txt")
with open(text_path, "w", encoding="utf-8") as f:
f.write(result["text"])
if client_id in clients:
await clients[client_id].send_json({"progress": 100})
return {"message": "Transcription completed", "download_url": f"/download/{file.filename}.txt"}
@app.get("/download/{filename}")
async def download_text(filename: str):
file_path = os.path.join(OUTPUT_DIR, filename)
if os.path.exists(file_path):
return {"message": "Download file here", "url": file_path}
return {"error": "File not found"}
import torch
print(torch.cuda.is_available()) # ควรแสดง True ถ้า GPU พร้อมใช้งาน
print(torch.cuda.device_count()) # ควรแสดงจำนวน GPU
annotated-types==0.7.0
anyio==4.8.0
certifi==2025.1.31
charset-normalizer==3.4.1
click==8.1.8
dnspython==2.7.0
email_validator==2.2.0
fastapi==0.115.11
fastapi-cli==0.0.7
filelock==3.17.0
fsspec==2025.3.0
h11==0.14.0
httpcore==1.0.7
httptools==0.6.4
httpx==0.28.1
idna==3.10
Jinja2==3.1.6
llvmlite==0.44.0
markdown-it-py==3.0.0
MarkupSafe==3.0.2
mdurl==0.1.2
more-itertools==10.6.0
mpmath==1.3.0
networkx==3.4.2
numba==0.61.0
numpy==2.1.3
nvidia-cublas-cu12==12.4.5.8
nvidia-cuda-cupti-cu12==12.4.127
nvidia-cuda-nvrtc-cu12==12.4.127
nvidia-cuda-runtime-cu12==12.4.127
nvidia-cudnn-cu12==9.1.0.70
nvidia-cufft-cu12==11.2.1.3
nvidia-curand-cu12==10.3.5.147
nvidia-cusolver-cu12==11.6.1.9
nvidia-cusparse-cu12==12.3.1.170
nvidia-cusparselt-cu12==0.6.2
nvidia-nccl-cu12==2.21.5
nvidia-nvjitlink-cu12==12.4.127
nvidia-nvtx-cu12==12.4.127
openai-whisper==20240930
pydantic==2.10.6
pydantic_core==2.27.2
Pygments==2.19.1
python-dotenv==1.0.1
python-multipart==0.0.20
PyYAML==6.0.2
regex==2024.11.6
requests==2.32.3
rich==13.9.4
rich-toolkit==0.13.2
setuptools==76.0.0
shellingham==1.5.4
sniffio==1.3.1
starlette==0.46.1
sympy==1.13.1
tiktoken==0.9.0
torch==2.6.0
tqdm==4.67.1
triton==3.2.0
typer==0.15.2
typing_extensions==4.12.2
urllib3==2.3.0
uvicorn==0.34.0
uvloop==0.21.0
watchfiles==1.0.4
websockets==15.0.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment