dahyedahye commited on
Commit
ec97684
ยท
1 Parent(s): c2cf644
Files changed (3) hide show
  1. Dockerfile +18 -0
  2. app.py +65 -0
  3. requirements.txt +5 -0
Dockerfile ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Read the doc: https://huggingface.co/docs/hub/spaces-sdks-docker
2
+ # you will also find guides on how best to write your Dockerfile
3
+
4
+ FROM python:3.9
5
+
6
+ RUN useradd -m -u 1000 user
7
+ USER user
8
+ ENV PATH="/home/user/.local/bin:$PATH"
9
+
10
+ WORKDIR /app
11
+
12
+ COPY --chown=user ./requirements.txt requirements.txt
13
+ RUN pip install --no-cache-dir --upgrade -r requirements.txt
14
+
15
+ COPY --chown=user . /app
16
+ CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
17
+
18
+
app.py ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import shutil
3
+ from fastapi import FastAPI, File, UploadFile, Form
4
+ from fastapi.responses import FileResponse, JSONResponse
5
+ import whisper
6
+
7
+ app = FastAPI()
8
+
9
+ # Whisper ๋ชจ๋ธ ๋กœ๋“œ (ํ•„์š”์— ๋”ฐ๋ผ ๋ชจ๋ธ ํฌ๊ธฐ๋ฅผ ์„ ํƒํ•˜์„ธ์š”)
10
+ model = whisper.load_model("tiny") # "tiny", "base", "small", "medium", "large-v2" ๋“ฑ
11
+
12
+ @app.post("/transcribe/")
13
+ async def transcribe_video(
14
+ file: UploadFile = File(...),
15
+ language: str = Form("en") # ์–ธ์–ด ์„ ํƒ ๊ฐ€๋Šฅ (๊ธฐ๋ณธ๊ฐ’: ์˜์–ด)
16
+ ):
17
+ """
18
+ Upload a video/audio file and get the generated SRT file as a response.
19
+ """
20
+ try:
21
+ # Create a temporary directory
22
+ temp_dir = "temp"
23
+ os.makedirs(temp_dir, exist_ok=True)
24
+
25
+ # Save the uploaded file temporarily
26
+ input_file_path = os.path.join(temp_dir, file.filename)
27
+ with open(input_file_path, "wb") as buffer:
28
+ shutil.copyfileobj(file.file, buffer)
29
+
30
+ # Transcribe the file using Whisper
31
+ result = model.transcribe(input_file_path, language=language)
32
+
33
+ # SRT ํŒŒ์ผ ์ƒ์„ฑ
34
+ srt_file_path = os.path.join(temp_dir, "output.srt")
35
+ with open(srt_file_path, "w") as srt_file:
36
+ for i, segment in enumerate(result["segments"]):
37
+ start = segment['start']
38
+ end = segment['end']
39
+ text = segment['text'][1:] # Remove leading space
40
+
41
+ # ํƒ€์ž„์Šคํƒฌํ”„ ํ˜•์‹ ๋ณ€ํ™˜ (SRT ํ˜•์‹์— ๋งž๊ฒŒ)
42
+ start_time = f"{int(start // 3600):02}:{int((start % 3600) // 60):02}:{int(start % 60):02},{int((start * 1000) % 1000):03}"
43
+ end_time = f"{int(end // 3600):02}:{int((end % 3600) // 60):02}:{int(end % 60):02},{int((end * 1000) % 1000):03}"
44
+
45
+ # SRT ํŒŒ์ผ์— ๊ธฐ๋ก
46
+ srt_file.write(f"{i + 1}\n")
47
+ srt_file.write(f"{start_time} --> {end_time}\n")
48
+ srt_file.write(f"{text}\n\n")
49
+
50
+ # Return the SRT file as a response
51
+ return FileResponse(
52
+ path=srt_file_path,
53
+ filename="output.srt",
54
+ media_type='application/x-subrip'
55
+ )
56
+
57
+ except Exception as e:
58
+ return JSONResponse(status_code=500, content={"message": str(e)})
59
+
60
+ finally:
61
+ # Clean up temporary files
62
+ if os.path.exists(input_file_path):
63
+ os.remove(input_file_path)
64
+ if os.path.exists(srt_file_path):
65
+ os.remove(srt_file_path)
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ torch # PyTorch๋Š” Whisper ๋ชจ๋ธ์„ ์‹คํ–‰ํ•˜๊ธฐ ์œ„ํ•ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.
2
+ whisper # Whisper ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์„ค์น˜ํ•ฉ๋‹ˆ๋‹ค.
3
+
4
+ fastapi # FastAPI ํ”„๋ ˆ์ž„์›Œํฌ
5
+ uvicorn # FastAPI ์„œ๋ฒ„ ์‹คํ–‰์„ ์œ„ํ•œ Uvicorn ASGI ์„œ๋ฒ„