Spaces:
Sleeping
Sleeping
Commit
Β·
c12e8b3
1
Parent(s):
905c475
- .env +2 -0
- __pycache__/app.cpython-310.pyc +0 -0
- __pycache__/app.cpython-37.pyc +0 -0
- __pycache__/app.cpython-39.pyc +0 -0
- app.py +106 -45
- requirements.txt +2 -1
.env
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
SMTP_USER=kyowoon.lee1924@gmail.com
|
| 2 |
+
SMTP_PASSWORD=rhwy amyv hdwq pspa
|
__pycache__/app.cpython-310.pyc
ADDED
|
Binary file (2.79 kB). View file
|
|
|
__pycache__/app.cpython-37.pyc
ADDED
|
Binary file (2.78 kB). View file
|
|
|
__pycache__/app.cpython-39.pyc
ADDED
|
Binary file (3.74 kB). View file
|
|
|
app.py
CHANGED
|
@@ -1,65 +1,126 @@
|
|
| 1 |
import os
|
| 2 |
-
import
|
| 3 |
-
|
| 4 |
-
|
| 5 |
import whisper
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
"""
|
| 18 |
-
|
| 19 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
try:
|
| 21 |
-
#
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 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:]
|
| 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 |
-
#
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
)
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
except Exception as e:
|
| 58 |
-
|
| 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)
|
|
|
|
| 1 |
import os
|
| 2 |
+
import re
|
| 3 |
+
import uuid
|
| 4 |
+
import gdown
|
| 5 |
import whisper
|
| 6 |
+
import smtplib
|
| 7 |
+
from email.mime.text import MIMEText
|
| 8 |
+
from email.mime.multipart import MIMEMultipart
|
| 9 |
+
from concurrent.futures import ThreadPoolExecutor
|
| 10 |
+
from fastapi import FastAPI, HTTPException, BackgroundTasks
|
| 11 |
+
from fastapi.responses import JSONResponse
|
| 12 |
+
from dotenv import load_dotenv
|
| 13 |
|
| 14 |
+
# κ΅¬κΈ λλΌμ΄λΈ λ§ν¬, μ΄λ€ μΈμ΄, λ°μ μ΄λ©μΌ μ£Όμ
|
| 15 |
|
| 16 |
+
# .env νμΌμμ μ€μν νκ²½ λ³μ λ‘λ
|
| 17 |
+
load_dotenv()
|
| 18 |
|
| 19 |
+
app = FastAPI(
|
| 20 |
+
version="0.0.1",
|
| 21 |
+
servers=[
|
| 22 |
+
{
|
| 23 |
+
"url": "https://leekwoon-whisper-api.hf.space",
|
| 24 |
+
"description": "video/audio transcription API",
|
| 25 |
+
}
|
| 26 |
+
],
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
# Whisper λͺ¨λΈ λ‘λ
|
| 30 |
+
model = whisper.load_model("tiny")
|
| 31 |
+
|
| 32 |
+
executor = ThreadPoolExecutor(max_workers=3) # μ΅λ 3κ°μ μ€λ λλ‘ λΉλκΈ° μμ
μ²λ¦¬
|
| 33 |
+
|
| 34 |
+
def extract_file_id(drive_url: str) -> str:
|
| 35 |
"""
|
| 36 |
+
Google Drive URLμμ νμΌ IDλ₯Ό μΆμΆν©λλ€.
|
| 37 |
"""
|
| 38 |
+
match = re.search(r'/d/([a-zA-Z0-9_-]+)', drive_url)
|
| 39 |
+
if match:
|
| 40 |
+
return match.group(1)
|
| 41 |
+
match = re.search(r'file/d/([a-zA-Z0-9_-]+)', drive_url)
|
| 42 |
+
if match:
|
| 43 |
+
return match.group(1)
|
| 44 |
+
match = re.search(r'([a-zA-Z0-9_-]{33,})', drive_url)
|
| 45 |
+
if match:
|
| 46 |
+
return match.group(1)
|
| 47 |
+
raise ValueError("Invalid Google Drive URL")
|
| 48 |
+
|
| 49 |
+
def send_email(to_email: str, srt_file_path: str, transcription_time: float):
|
| 50 |
+
smtp_server = "smtp.gmail.com"
|
| 51 |
+
smtp_port = 587
|
| 52 |
+
smtp_user = os.getenv("SMTP_USER")
|
| 53 |
+
smtp_password = os.getenv("SMTP_PASSWORD")
|
| 54 |
+
|
| 55 |
+
subject = "[kyobody - μλ§μμ±] μμ
μ΄ μλ£λμμ΅λλ€."
|
| 56 |
+
body = f"[kyobody - μλ§μμ±] μμ
μ΄ μλ£λμμ΅λλ€. μ΄ μμ μκ°: {transcription_time:.2f} μ΄. SRT νμΌμ 첨λΆν©λλ€."
|
| 57 |
+
|
| 58 |
+
msg = MIMEMultipart()
|
| 59 |
+
msg["From"] = smtp_user
|
| 60 |
+
msg["To"] = to_email
|
| 61 |
+
msg["Subject"] = subject
|
| 62 |
+
|
| 63 |
+
msg.attach(MIMEText(body, "plain"))
|
| 64 |
+
|
| 65 |
+
with open(srt_file_path, "r") as file:
|
| 66 |
+
attachment = MIMEText(file.read())
|
| 67 |
+
attachment.add_header("Content-Disposition", "attachment", filename=os.path.basename(srt_file_path))
|
| 68 |
+
msg.attach(attachment)
|
| 69 |
+
|
| 70 |
+
with smtplib.SMTP(smtp_server, smtp_port) as server:
|
| 71 |
+
server.starttls()
|
| 72 |
+
server.login(smtp_user, smtp_password)
|
| 73 |
+
server.sendmail(smtp_user, to_email, msg.as_string())
|
| 74 |
+
|
| 75 |
+
def transcribe_and_send_email(temp_input_file: str, srt_file_path: str, email: str):
|
| 76 |
try:
|
| 77 |
+
# Transcribe the video/audio file
|
| 78 |
+
import time
|
| 79 |
+
start_time = time.time()
|
| 80 |
+
result = model.transcribe(temp_input_file)
|
| 81 |
+
transcription_time = time.time() - start_time
|
| 82 |
+
|
| 83 |
+
# Save the transcription to an SRT file
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
with open(srt_file_path, "w") as srt_file:
|
| 85 |
for i, segment in enumerate(result["segments"]):
|
| 86 |
start = segment['start']
|
| 87 |
end = segment['end']
|
| 88 |
+
text = segment['text'][1:]
|
| 89 |
|
|
|
|
| 90 |
start_time = f"{int(start // 3600):02}:{int((start % 3600) // 60):02}:{int(start % 60):02},{int((start * 1000) % 1000):03}"
|
| 91 |
end_time = f"{int(end // 3600):02}:{int((end % 3600) // 60):02}:{int(end % 60):02},{int((end * 1000) % 1000):03}"
|
| 92 |
|
|
|
|
| 93 |
srt_file.write(f"{i + 1}\n")
|
| 94 |
srt_file.write(f"{start_time} --> {end_time}\n")
|
| 95 |
srt_file.write(f"{text}\n\n")
|
| 96 |
+
|
| 97 |
+
# Send the result via email
|
| 98 |
+
send_email(email, srt_file_path, transcription_time)
|
| 99 |
+
|
| 100 |
+
# Clean up the temporary files
|
| 101 |
+
os.remove(temp_input_file)
|
| 102 |
+
os.remove(srt_file_path)
|
| 103 |
+
|
| 104 |
+
except Exception as e:
|
| 105 |
+
print(f"Error in transcription or email sending: {e}")
|
| 106 |
+
|
| 107 |
+
@app.post("/transcribe/")
|
| 108 |
+
def transcribe_video(url: str, email: str, background_tasks: BackgroundTasks):
|
| 109 |
+
try:
|
| 110 |
+
# Extract file ID and download the file
|
| 111 |
+
file_id = extract_file_id(url)
|
| 112 |
+
download_url = f"https://drive.google.com/uc?id={file_id}"
|
| 113 |
+
temp_input_file = f'/tmp/{uuid.uuid4()}.mp4'
|
| 114 |
+
gdown.download(download_url, temp_input_file, quiet=False)
|
| 115 |
+
|
| 116 |
+
# Define SRT file path
|
| 117 |
+
srt_file_path = f'/tmp/{uuid.uuid4()}.srt'
|
| 118 |
+
|
| 119 |
+
# Schedule the transcription and email sending in the background
|
| 120 |
+
background_tasks.add_task(executor.submit, transcribe_and_send_email, temp_input_file, srt_file_path, email)
|
| 121 |
+
|
| 122 |
+
# Respond to the client immediately
|
| 123 |
+
return JSONResponse(status_code=202, content={"message": "Transcription started, you will receive an email when it's done."})
|
| 124 |
+
|
| 125 |
except Exception as e:
|
| 126 |
+
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
requirements.txt
CHANGED
|
@@ -3,4 +3,5 @@ openai-whisper # Whisper λΌμ΄λΈλ¬λ¦¬λ₯Ό μ€μΉν©λλ€.
|
|
| 3 |
|
| 4 |
fastapi # FastAPI νλ μμν¬
|
| 5 |
uvicorn # FastAPI μλ² μ€νμ μν Uvicorn ASGI μλ²
|
| 6 |
-
python-
|
|
|
|
|
|
| 3 |
|
| 4 |
fastapi # FastAPI νλ μμν¬
|
| 5 |
uvicorn # FastAPI μλ² μ€νμ μν Uvicorn ASGI μλ²
|
| 6 |
+
python-dotenv
|
| 7 |
+
gdown
|