Skip to content

Commit

Permalink
Fix for #17 and code cleaning
Browse files Browse the repository at this point in the history
* organise imports and comments
  • Loading branch information
kabanosk authored and kabanosk committed Sep 2, 2023
1 parent b01de4e commit 741c447
Showing 1 changed file with 30 additions and 26 deletions.
56 changes: 30 additions & 26 deletions src/main.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,22 @@
import os
import ast
import asyncio
import subprocess
from pprint import pprint
from datetime import timedelta
from pathlib import Path
import srt as srt
from datetime import timedelta, datetime
from fastapi import FastAPI, Request, File, Response, Form
from typing import Optional

from fastapi import FastAPI, Request, File, Form
from fastapi.responses import HTMLResponse, StreamingResponse
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
import ffmpeg
import numpy as np
import srt as srt
import stable_whisper
from typing import Optional
import shutil



# a function that takes a file and a start and length timestamps, and will return the audio data in that section as a
# np array which the model's transcribe function can take
def get_audio_buffer(filename: str, start: int, length: int):
"""
input: filename of the audio file, start time in seconds, length of the audio in seconds
output: np array of the audio data which the model's transcribe function can take as input
"""
out, _ = (
ffmpeg.input(filename, threads=0)
.output("-", format="s16le", acodec="pcm_s16le", ac=1, ar=16000, ss=start, t=length)
Expand All @@ -30,9 +26,11 @@ def get_audio_buffer(filename: str, start: int, length: int):
return np.frombuffer(out, np.int16).flatten().astype(np.float32) / 32768.0


# a function that takes a file and an interval that deterimines the distance between each timestamp in the
# outputted dictionary
def transcribe_time_stamps(segments: list):
"""
input: a list of segments from the model's transcribe function
output: a string of the timestamps and the text of each segment
"""
string = ""
for seg in segments:
string += " ".join([str(seg.start), "->", str(seg.end), ": ", seg.text.strip(), "\n"])
Expand All @@ -56,6 +54,7 @@ def make_srt_subtitles(segments: list):

return srt.compose(subtitles)


app = FastAPI()

app.mount('/static', StaticFiles(directory='static'), name='static')
Expand Down Expand Up @@ -94,11 +93,15 @@ def add_audio(
return template.TemplateResponse('index.html', {"request": request, "text": timestamps_text})



# Added the following feature to automatically download the transcripted file. The file will download in the web browser of the user.
@app.post('/download/')
async def download_subtitle(request: Request, file: bytes = File(), model_type: str = "tiny", timestamps: Optional[str] = Form("False"), filename: str = "subtitles", file_type: str = "srt"):

async def download_subtitle(
request: Request,
file: bytes = File(),
model_type: str = "tiny",
timestamps: Optional[str] = Form("False"),
filename: str = "subtitles",
file_type: str = "srt"
):
# Save the uploaded file
with open('audio.mp3', 'wb') as f:
f.write(file)
Expand All @@ -111,6 +114,7 @@ async def download_subtitle(request: Request, file: bytes = File(), model_type:
if timestamps == "True":
timestamps_text = transcribe_time_stamps(result.segments)

subtitle_file = "subtitle.srt"
# Create the subtitle file
if file_type == "srt":
subtitle_file = f"{filename}.srt"
Expand All @@ -128,12 +132,12 @@ async def download_subtitle(request: Request, file: bytes = File(), model_type:
f.write(result.text)

# Create a streaming response with the file
path = Path(subtitle_file)
media_type = "application/octet-stream"
response = StreamingResponse(path.open('rb'), media_type=media_type, headers={'Content-Disposition': f'attachment;filename={subtitle_file}'})

# Clean up the generated file after sending the response
shutil.move(subtitle_file, path) # This line ensures that the file is not deleted prematurely
os.remove(subtitle_file)
response = StreamingResponse(
open(subtitle_file, 'rb'),
media_type=media_type,
headers={'Content-Disposition': f'attachment;filename={subtitle_file}'}
)

return response
Path(subtitle_file).unlink()
return response

0 comments on commit 741c447

Please sign in to comment.