Saqib commited on
Commit
fcee60b
1 Parent(s): e8a3016

Update modules/app.py

Browse files
Files changed (1) hide show
  1. modules/app.py +18 -6
modules/app.py CHANGED
@@ -9,6 +9,7 @@ import base64
9
  import io
10
  import os
11
  import random
 
12
  import string
13
  import json
14
 
@@ -42,20 +43,24 @@ os.makedirs(AUDIO_DIR, exist_ok=True)
42
  app.mount("/audio", StaticFiles(directory=AUDIO_DIR), name="audio")
43
 
44
  @app.get("/get_audio")
45
- async def get_audio(url: str):
46
- print(url)
47
-
48
  if not url:
49
  raise HTTPException(status_code=400, detail="URL is required")
50
 
51
  try:
52
- time.sleep(2)
53
  yt = YouTube(url)
54
  video = yt.streams.filter(only_audio=True).first()
55
 
 
 
 
 
56
  # Generate a unique filename
57
  unique_filename = f"{uuid.uuid4().hex}.mp3"
58
  out_file = os.path.join(AUDIO_DIR, unique_filename)
 
 
59
 
60
  # Download the audio
61
  video.download(output_path=AUDIO_DIR, filename=unique_filename)
@@ -70,9 +75,16 @@ async def get_audio(url: str):
70
  else:
71
  os.remove(out_file)
72
  raise HTTPException(status_code=413, detail="Audio file is too large. Limited to about 1.5 hours.")
 
 
 
 
 
 
73
  except Exception as e:
74
- print(e)
75
- raise HTTPException(status_code=500, detail=str(e))
 
76
 
77
  @app.post("/whisper")
78
  async def whisper(request: Request):
 
9
  import io
10
  import os
11
  import random
12
+ import traceback
13
  import string
14
  import json
15
 
 
43
  app.mount("/audio", StaticFiles(directory=AUDIO_DIR), name="audio")
44
 
45
  @app.get("/get_audio")
46
+ async def get_audio(url: str):
 
 
47
  if not url:
48
  raise HTTPException(status_code=400, detail="URL is required")
49
 
50
  try:
51
+ print(f"Attempting to process YouTube URL: {url}")
52
  yt = YouTube(url)
53
  video = yt.streams.filter(only_audio=True).first()
54
 
55
+ if not video:
56
+ print(f"No audio stream found for URL: {url}")
57
+ raise HTTPException(status_code=404, detail="No audio stream found for this video")
58
+
59
  # Generate a unique filename
60
  unique_filename = f"{uuid.uuid4().hex}.mp3"
61
  out_file = os.path.join(AUDIO_DIR, unique_filename)
62
+
63
+ print((f"Downloading audio to: {out_file}"))
64
 
65
  # Download the audio
66
  video.download(output_path=AUDIO_DIR, filename=unique_filename)
 
75
  else:
76
  os.remove(out_file)
77
  raise HTTPException(status_code=413, detail="Audio file is too large. Limited to about 1.5 hours.")
78
+ except PytubeError as e:
79
+ print(f"PytubeError occurred: {str(e)}")
80
+ raise HTTPException(status_code=400, detail=f"Error processing YouTube video: {str(e)}")
81
+ except HTTPException as he:
82
+ # Re-raise HTTP exceptions
83
+ raise he
84
  except Exception as e:
85
+ print(f"Unexpected error occurred: {str(e)}")
86
+ print(traceback.format_exc())
87
+ raise HTTPException(status_code=500, detail=f"An unexpected error occurred: {str(e)}")
88
 
89
  @app.post("/whisper")
90
  async def whisper(request: Request):