Spaces:
Sleeping
Sleeping
Commit ·
55fab09
1
Parent(s): 291c169
Add application file
Browse files- Dockerfile +19 -0
- app.py +43 -0
- requirements.txt +6 -0
Dockerfile
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.10-slim
|
| 2 |
+
|
| 3 |
+
WORKDIR /app
|
| 4 |
+
|
| 5 |
+
RUN apt-get update && apt-get install -y \
|
| 6 |
+
ffmpeg \
|
| 7 |
+
gcc \
|
| 8 |
+
&& rm -rf /var/lib/apt/lists/*
|
| 9 |
+
|
| 10 |
+
COPY requirements.txt .
|
| 11 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 12 |
+
|
| 13 |
+
COPY app.py .
|
| 14 |
+
COPY index.html .
|
| 15 |
+
|
| 16 |
+
ENV PORT=7860
|
| 17 |
+
EXPOSE 7860
|
| 18 |
+
|
| 19 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
app.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, File, UploadFile
|
| 2 |
+
from fastapi.responses import HTMLResponse, JSONResponse
|
| 3 |
+
from fastapi.staticfiles import StaticFiles
|
| 4 |
+
import whisper
|
| 5 |
+
import os
|
| 6 |
+
import uuid
|
| 7 |
+
from pathlib import Path
|
| 8 |
+
|
| 9 |
+
app = FastAPI()
|
| 10 |
+
|
| 11 |
+
model = whisper.load_model("base")
|
| 12 |
+
|
| 13 |
+
@app.get("/", response_class=HTMLResponse)
|
| 14 |
+
async def root():
|
| 15 |
+
with open("index.html", "r", encoding="utf-8") as f:
|
| 16 |
+
return f.read()
|
| 17 |
+
|
| 18 |
+
@app.post("/transcribe")
|
| 19 |
+
async def transcribe_audio(file: UploadFile = File(...)):
|
| 20 |
+
try:
|
| 21 |
+
ext = Path(file.filename).suffix
|
| 22 |
+
temp_path = f"/tmp/{uuid.uuid4()}{ext}"
|
| 23 |
+
|
| 24 |
+
with open(temp_path, "wb") as buffer:
|
| 25 |
+
content = await file.read()
|
| 26 |
+
buffer.write(content)
|
| 27 |
+
|
| 28 |
+
result = model.transcribe(temp_path, language="ru")
|
| 29 |
+
text = result["text"].strip()
|
| 30 |
+
|
| 31 |
+
os.remove(temp_path)
|
| 32 |
+
|
| 33 |
+
return JSONResponse({"text": text, "status": "ok"})
|
| 34 |
+
|
| 35 |
+
except Exception as e:
|
| 36 |
+
return JSONResponse(
|
| 37 |
+
{"text": f"Ошибка: {str(e)}", "status": "error"},
|
| 38 |
+
status_code=500
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
if __name__ == "__main__":
|
| 42 |
+
import uvicorn
|
| 43 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
uvicorn[standard]
|
| 3 |
+
openai-whisper
|
| 4 |
+
torch
|
| 5 |
+
torchaudio
|
| 6 |
+
python-multipart
|