Saqib
commited on
Update modules/app.py
Browse files- modules/app.py +0 -43
modules/app.py
CHANGED
@@ -25,52 +25,9 @@ def generate_hash(length=12):
|
|
25 |
hash_string = ''.join(random.choice(characters) for _ in range(length))
|
26 |
return hash_string
|
27 |
|
28 |
-
def cleanup_file(path: str) -> None:
|
29 |
-
if os.path.exists(path):
|
30 |
-
os.remove(path)
|
31 |
-
|
32 |
-
class VideoInfo(BaseModel):
|
33 |
-
url: str
|
34 |
-
|
35 |
@app.get("/")
|
36 |
async def read_root():
|
37 |
return {"message": "Saqib's API"}
|
38 |
-
|
39 |
-
@app.post("/audio-file")
|
40 |
-
async def get_audio_file(video: VideoInfo, background_tasks: BackgroundTasks):
|
41 |
-
unique_id = str(uuid.uuid4())
|
42 |
-
output_path = f"temp_{unique_id}.mp3"
|
43 |
-
|
44 |
-
ydl_opts = {
|
45 |
-
'format': 'bestaudio/best',
|
46 |
-
'postprocessors': [{
|
47 |
-
'key': 'FFmpegExtractAudio',
|
48 |
-
'preferredcodec': 'mp3',
|
49 |
-
'preferredquality': '192',
|
50 |
-
}],
|
51 |
-
'outtmpl': output_path,
|
52 |
-
}
|
53 |
-
|
54 |
-
try:
|
55 |
-
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
56 |
-
info = ydl.extract_info(video.url, download=True)
|
57 |
-
title = info['title']
|
58 |
-
|
59 |
-
if not os.path.exists(output_path):
|
60 |
-
raise HTTPException(status_code=500, detail="Failed to download audio")
|
61 |
-
|
62 |
-
background_tasks.add_task(cleanup_file, output_path)
|
63 |
-
|
64 |
-
return FileResponse(
|
65 |
-
output_path,
|
66 |
-
media_type="audio/mpeg",
|
67 |
-
filename=f"{title}.mp3"
|
68 |
-
)
|
69 |
-
|
70 |
-
except Exception as e:
|
71 |
-
if os.path.exists(output_path):
|
72 |
-
os.remove(output_path)
|
73 |
-
raise HTTPException(status_code=400, detail=str(e))
|
74 |
|
75 |
@app.post("/whisper")
|
76 |
async def whisper(request: Request):
|
|
|
25 |
hash_string = ''.join(random.choice(characters) for _ in range(length))
|
26 |
return hash_string
|
27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
@app.get("/")
|
29 |
async def read_root():
|
30 |
return {"message": "Saqib's API"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
|
32 |
@app.post("/whisper")
|
33 |
async def whisper(request: Request):
|