Akhil commited on
Commit
3e9f66f
·
1 Parent(s): 22f48d5

fix: better FastAPI2

Browse files
.gitignore CHANGED
@@ -1,4 +1,5 @@
1
  audio/*
2
  .venv
3
  .idea
4
- models/*
 
 
1
  audio/*
2
  .venv
3
  .idea
4
+ models/*
5
+ __pychache__
Dockerfile CHANGED
@@ -7,6 +7,7 @@ RUN apt-get update && \
7
  WORKDIR /app
8
 
9
  COPY requirements.txt .
10
- RUN pip3 install --no-cache-dir -r requirements.txt
11
 
12
- CMD ["tail", "-f", "/dev/null"]
 
 
7
  WORKDIR /app
8
 
9
  COPY requirements.txt .
10
+ RUN pip3 install --no-cache-dir --upgrade -r requirements.txt
11
 
12
+ CMD ["tail", "-f", "/dev/null"]
13
+ CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
__pycache__/main.cpython-312.pyc ADDED
Binary file (13.3 kB). View file
 
__pycache__/main.cpython-313.pyc ADDED
Binary file (14.4 kB). View file
 
main.py CHANGED
@@ -3,12 +3,27 @@ import time
3
  import os
4
  import yt_dlp
5
  import subprocess
 
6
  import logging
7
- from fastapi import FastAPI
 
 
 
 
 
 
8
 
9
 
10
  app = FastAPI()
11
 
 
 
 
 
 
 
 
 
12
  logging.basicConfig()
13
  logging.getLogger("faster_whisper").setLevel(logging.DEBUG)
14
 
@@ -69,7 +84,6 @@ def local_audio_file(DOWNLOAD_DIR, AUDIO_FILE):
69
  finally:
70
  return final_filepath
71
 
72
-
73
  def create_subtitle_chunks(segments, max_words=8, max_duration=5.0):
74
  subtitle_chunks = []
75
 
@@ -111,7 +125,6 @@ def create_subtitle_chunks(segments, max_words=8, max_duration=5.0):
111
 
112
  return subtitle_chunks
113
 
114
-
115
  def format_time(seconds):
116
  seconds -= 0.2
117
  hours = int(seconds // 3600)
@@ -121,7 +134,6 @@ def format_time(seconds):
121
 
122
  return f"{hours:02d}:{minutes:02d}:{int(seconds_remainder):02d},{milliseconds:03d}"
123
 
124
-
125
  def add_subtitles(media_path):
126
  base, ext = os.path.splitext(os.path.basename(media_path))
127
  dir_path = os.path.dirname(media_path)
@@ -168,25 +180,30 @@ def add_subtitles(media_path):
168
  except Exception as e:
169
  print(f"An error occurred: {e}")
170
 
171
-
172
- def main():
173
- all_files = ["YOUTUBE VIDEO URL"]
174
- all_files += os.listdir('audio')
175
-
176
- for i, file in enumerate(all_files):
177
- print(f"[{i+1}] - {file}")
178
-
179
- DOWNLOAD_DIR = "audio"
180
- output_template = os.path.join(DOWNLOAD_DIR, '%(title)s.%(ext)s')
181
-
182
- file_idx = int(input('Enter file index: '))
183
- if file_idx != 1:
184
- input_file_path = all_files[file_idx-1];
185
- AUDIO_FILE=os.path.join(DOWNLOAD_DIR, input_file_path)
186
- final_filepath = local_audio_file(DOWNLOAD_DIR=DOWNLOAD_DIR, AUDIO_FILE=AUDIO_FILE)
 
 
 
 
 
 
187
  else:
188
- YT_URL = input("Enter URL: ")
189
- final_filepath = youtube_download_video(YT_URL, DOWNLOAD_DIR, output_template)
190
 
191
 
192
  if final_filepath and os.path.exists(final_filepath):
@@ -216,8 +233,8 @@ def main():
216
  word_timestamps=True
217
  )
218
 
219
- os.makedirs(DOWNLOAD_DIR, exist_ok=True)
220
- transcript_filename = os.path.join(DOWNLOAD_DIR, f"{FILE_NAME_FOR_TXT}.srt")
221
 
222
  subtitle_chunks = create_subtitle_chunks(segments, max_words=12, max_duration=4.0)
223
 
@@ -243,10 +260,22 @@ def main():
243
  print(f"\nTranscription complete and saved to {transcript_filename}.")
244
  print(f"Processed in {processed_time:.2f} seconds")
245
 
246
- add_subtitles(final_filepath)
 
 
 
 
 
 
 
 
 
 
 
 
247
 
248
  except Exception as e:
249
- print(f"An error occurred during transcription: {e}")
250
 
251
  finally:
252
  if 'model' in locals():
@@ -258,7 +287,6 @@ def main():
258
  gc.collect()
259
 
260
  else:
261
- print("Audio file acquisition failed (YouTube download or local file not found). Cannot proceed with transcription.")
262
-
263
- if __name__ == "__main__":
264
- main()
 
3
  import os
4
  import yt_dlp
5
  import subprocess
6
+ from typing import Optional
7
  import logging
8
+ from fastapi import FastAPI, File, UploadFile, HTTPException, Form
9
+ from fastapi.responses import FileResponse
10
+ import os, time
11
+ from fastapi.middleware.cors import CORSMiddleware
12
+ from pathlib import Path
13
+ import zipfile
14
+ import tempfile
15
 
16
 
17
  app = FastAPI()
18
 
19
+ app.add_middleware(
20
+ CORSMiddleware,
21
+ allow_origins=["http://localhost:5173", "http://127.0.0.1:5173"],
22
+ allow_credentials=True,
23
+ allow_methods=["GET", "POST", "PUT", "DELETE", "OPTIONS"],
24
+ allow_headers=["*"],
25
+ )
26
+
27
  logging.basicConfig()
28
  logging.getLogger("faster_whisper").setLevel(logging.DEBUG)
29
 
 
84
  finally:
85
  return final_filepath
86
 
 
87
  def create_subtitle_chunks(segments, max_words=8, max_duration=5.0):
88
  subtitle_chunks = []
89
 
 
125
 
126
  return subtitle_chunks
127
 
 
128
  def format_time(seconds):
129
  seconds -= 0.2
130
  hours = int(seconds // 3600)
 
134
 
135
  return f"{hours:02d}:{minutes:02d}:{int(seconds_remainder):02d},{milliseconds:03d}"
136
 
 
137
  def add_subtitles(media_path):
138
  base, ext = os.path.splitext(os.path.basename(media_path))
139
  dir_path = os.path.dirname(media_path)
 
180
  except Exception as e:
181
  print(f"An error occurred: {e}")
182
 
183
+ @app.get('/test')
184
+ async def test_endpoint():
185
+ return {"message": "FastAPI is working!"}
186
+
187
+ @app.post('/generate-subtitles')
188
+ async def generate_subtitles(
189
+ file: Optional[UploadFile] = File(None),
190
+ youtube_url: Optional[str] = Form(None)
191
+ ):
192
+
193
+ upload_dir = 'audio'
194
+ os.makedirs(upload_dir, exist_ok=True)
195
+
196
+ if file:
197
+ file_path = os.path.join(upload_dir, file.filename)
198
+ with open(file_path, "wb") as f:
199
+ f.write(await file.read())
200
+ final_filepath = file_path
201
+ print(f"Uploaded file saved to {final_filepath}")
202
+ elif youtube_url:
203
+ output_template = os.path.join(upload_dir, "%(title)s.%(ext)s")
204
+ final_filepath = youtube_download_video(youtube_url, upload_dir, output_template)
205
  else:
206
+ raise HTTPException(status_code=400, detail="You must provide either a file or youtube URL.")
 
207
 
208
 
209
  if final_filepath and os.path.exists(final_filepath):
 
233
  word_timestamps=True
234
  )
235
 
236
+ os.makedirs(upload_dir, exist_ok=True)
237
+ transcript_filename = os.path.join(upload_dir, f"{FILE_NAME_FOR_TXT}.srt")
238
 
239
  subtitle_chunks = create_subtitle_chunks(segments, max_words=12, max_duration=4.0)
240
 
 
260
  print(f"\nTranscription complete and saved to {transcript_filename}.")
261
  print(f"Processed in {processed_time:.2f} seconds")
262
 
263
+ video_output = Path(final_filepath).resolve()
264
+ subtitle_output = Path(transcript_filename).resolve()
265
+
266
+ files_to_send = [video_output, subtitle_output]
267
+
268
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".zip") as tmp:
269
+ with zipfile.ZipFile(tmp, "w", zipfile.ZIP_DEFLATED) as zf:
270
+ for f in files_to_send:
271
+ zf.write(f, arcname=f.name)
272
+ tmp_path = tmp.name
273
+
274
+
275
+ return FileResponse(tmp_path, media_type="application/zip", filename="subtitles.zip")
276
 
277
  except Exception as e:
278
+ raise HTTPException(status_code=400, detail=str(e))
279
 
280
  finally:
281
  if 'model' in locals():
 
287
  gc.collect()
288
 
289
  else:
290
+ raise HTTPException(status_code=400, detail="Failed to process the file.")
291
+
292
+
 
test.py CHANGED
@@ -1,14 +1,4 @@
1
- import os
2
 
3
- all_files = os.listdir('audio')
4
-
5
- for i, file in enumerate(all_files):
6
- print(f"[{i}] - {file}")
7
-
8
- file_idx = int(input('Enter file index: '))
9
- input_file_path = all_files[file_idx];
10
- DOWNLOAD_DIR = "audio"
11
- AUDIO_FILE=os.path.join(DOWNLOAD_DIR, input_file_path)
12
-
13
- print(AUDIO_FILE)
14
- print(DOWNLOAD_DIR)
 
1
+ from pathlib import Path
2
 
3
+ file_path = r'audio\Never install locally [J0NuOlA2xDc].mp4'
4
+ print(Path(file_path).resolve())