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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, HTTPException, Request
|
| 2 |
+
from fastapi.staticfiles import StaticFiles
|
| 3 |
+
from concurrent.futures import ThreadPoolExecutor
|
| 4 |
+
import asyncio
|
| 5 |
+
import aiohttp
|
| 6 |
+
import tempfile
|
| 7 |
+
import uuid
|
| 8 |
+
import os
|
| 9 |
+
import random
|
| 10 |
+
import traceback
|
| 11 |
+
import string
|
| 12 |
+
|
| 13 |
+
app = FastAPI()
|
| 14 |
+
|
| 15 |
+
def generate_hash(length=12):
|
| 16 |
+
# Characters that can appear in the hash
|
| 17 |
+
characters = string.ascii_lowercase + string.digits
|
| 18 |
+
# Generate a random string of the specified length
|
| 19 |
+
hash_string = ''.join(random.choice(characters) for _ in range(length))
|
| 20 |
+
return hash_string
|
| 21 |
+
|
| 22 |
+
@app.get("/")
|
| 23 |
+
async def read_root():
|
| 24 |
+
return {"message": "Saqib's API"}
|
| 25 |
+
|
| 26 |
+
# Create a directory to store MP3 files if it doesn't exist
|
| 27 |
+
AUDIO_DIR = "audio_files"
|
| 28 |
+
os.makedirs(AUDIO_DIR, exist_ok=True)
|
| 29 |
+
|
| 30 |
+
# Create a directory for storing output files
|
| 31 |
+
OUTPUT_DIR = "output"
|
| 32 |
+
os.makedirs(OUTPUT_DIR, exist_ok=True)
|
| 33 |
+
|
| 34 |
+
# Mount the audio directory
|
| 35 |
+
app.mount("/audio", StaticFiles(directory=AUDIO_DIR), name="audio")
|
| 36 |
+
|
| 37 |
+
# Mount the output directory
|
| 38 |
+
app.mount("/output", StaticFiles(directory=OUTPUT_DIR), name="output")
|
| 39 |
+
|
| 40 |
+
thread_pool = ThreadPoolExecutor(max_workers=2)
|
| 41 |
+
|
| 42 |
+
async def run_ffmpeg_async(ffmpeg_command):
|
| 43 |
+
loop = asyncio.get_running_loop()
|
| 44 |
+
await loop.run_in_executor(thread_pool, ffmpeg_command)
|
| 45 |
+
|
| 46 |
+
async def download_file(url: str, suffix: str):
|
| 47 |
+
async with aiohttp.ClientSession() as session:
|
| 48 |
+
async with session.get(url) as response:
|
| 49 |
+
if response.status != 200:
|
| 50 |
+
raise HTTPException(status_code=400, detail=f"Failed to download file from {url}")
|
| 51 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=suffix) as temp_file:
|
| 52 |
+
temp_file.write(await response.read())
|
| 53 |
+
return temp_file.name
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
@app.post("/add_audio_to_image")
|
| 57 |
+
async def add_audio_to_image(request: Request):
|
| 58 |
+
try:
|
| 59 |
+
# Generate a unique filename
|
| 60 |
+
output_filename = f"{uuid.uuid4()}.mp4"
|
| 61 |
+
output_path = os.path.join(OUTPUT_DIR, output_filename)
|
| 62 |
+
|
| 63 |
+
# Call the modal API with the request data and download the output file
|
| 64 |
+
data = await request.json()
|
| 65 |
+
async with aiohttp.ClientSession() as session:
|
| 66 |
+
async with session.post(f"{MODAL_BASE_URL}/add_audio_to_image", json=data) as response:
|
| 67 |
+
if response.status != 200:
|
| 68 |
+
raise HTTPException(status_code=500, detail="Failed to process request")
|
| 69 |
+
output_data = await response.read()
|
| 70 |
+
async with aiofiles.open(output_path, "wb") as f:
|
| 71 |
+
await f.write(output_data)
|
| 72 |
+
|
| 73 |
+
# Return the URL path to the output file
|
| 74 |
+
return f"https://sxqib-api.hf.space/output/{output_filename}"
|
| 75 |
+
except Exception as e:
|
| 76 |
+
print(f"An error occurred: {str(e)}")
|
| 77 |
+
print(traceback.format_exc())
|
| 78 |
+
raise HTTPException(status_code=500, detail=f"An unexpected error occurred: {str(e)}")
|
| 79 |
+
|
| 80 |
+
@app.post("/add_audio_to_video")
|
| 81 |
+
async def add_audio_to_video(request: Request):
|
| 82 |
+
try:
|
| 83 |
+
# Generate a unique filename
|
| 84 |
+
output_filename = f"{uuid.uuid4()}.mp4"
|
| 85 |
+
output_path = os.path.join(OUTPUT_DIR, output_filename)
|
| 86 |
+
|
| 87 |
+
# Call the modal API with the request data and download the output file
|
| 88 |
+
data = await request.json()
|
| 89 |
+
async with aiohttp.ClientSession() as session:
|
| 90 |
+
async with session.post(f"{MODAL_BASE_URL}/add_audio_to_video", json=data) as response:
|
| 91 |
+
if response.status != 200:
|
| 92 |
+
raise HTTPException(status_code=500, detail="Failed to process request")
|
| 93 |
+
output_data = await response.read()
|
| 94 |
+
async with aiofiles.open(output_path, "wb") as f:
|
| 95 |
+
await f.write(output_data)
|
| 96 |
+
|
| 97 |
+
# Return the URL path to the output file
|
| 98 |
+
return f"https://sxqib-api.hf.space/output/{output_filename}"
|
| 99 |
+
except Exception as e:
|
| 100 |
+
print(f"An error occurred: {str(e)}")
|
| 101 |
+
print(traceback.format_exc())
|
| 102 |
+
raise HTTPException(status_code=500, detail=f"An unexpected error occurred: {str(e)}")
|
| 103 |
+
|
| 104 |
+
@app.post("/concatenate_videos")
|
| 105 |
+
async def concatenate_videos(request: Request):
|
| 106 |
+
try:
|
| 107 |
+
# Generate a unique filename for the output
|
| 108 |
+
output_filename = f"{uuid.uuid4()}.mp4"
|
| 109 |
+
output_path = os.path.join(OUTPUT_DIR, output_filename)
|
| 110 |
+
|
| 111 |
+
# Call the modal API with the request data and download the output file
|
| 112 |
+
data = await request.json()
|
| 113 |
+
async with aiohttp.ClientSession() as session:
|
| 114 |
+
async with session.post(f"{MODAL_BASE_URL}/concatenate_videos", json=data) as response:
|
| 115 |
+
if response.status != 200:
|
| 116 |
+
raise HTTPException(status_code=500, detail="Failed to process request")
|
| 117 |
+
output_data = await response.read()
|
| 118 |
+
async with aiofiles.open(output_path, "wb") as f:
|
| 119 |
+
await f.write(output_data)
|
| 120 |
+
|
| 121 |
+
# Return the URL path to the output file
|
| 122 |
+
return f"https://sxqib-api.hf.space/output/{output_filename}"
|
| 123 |
+
|
| 124 |
+
except Exception as e:
|
| 125 |
+
print(f"An error occurred: {str(e)}")
|
| 126 |
+
print(traceback.format_exc())
|
| 127 |
+
raise HTTPException(status_code=500, detail=f"An unexpected error occurred: {str(e)}")
|
| 128 |
+
|
| 129 |
+
@app.post("/concatenate_audio")
|
| 130 |
+
async def concatenate_audio(request: Request):
|
| 131 |
+
try:
|
| 132 |
+
# Generate a unique filename for the output
|
| 133 |
+
output_filename = f"{uuid.uuid4()}.mp3"
|
| 134 |
+
output_path = os.path.join(AUDIO_DIR, output_filename)
|
| 135 |
+
|
| 136 |
+
# Call the modal API with the request data and download the output file
|
| 137 |
+
data = await request.json()
|
| 138 |
+
async with aiohttp.ClientSession() as session:
|
| 139 |
+
async with session.post(f"{MODAL_BASE_URL}/concatenate_audio", json=data) as response:
|
| 140 |
+
if response.status != 200:
|
| 141 |
+
raise HTTPException(status_code=500, detail="Failed to process request")
|
| 142 |
+
output_data = await response.read()
|
| 143 |
+
async with aiofiles.open(output_path, "wb") as f:
|
| 144 |
+
await f.write(output_data)
|
| 145 |
+
|
| 146 |
+
# Return the URL path to the output file
|
| 147 |
+
return f"https://sxqib-api.hf.space/audio_files/{output_filename}"
|
| 148 |
+
|
| 149 |
+
except Exception as e:
|
| 150 |
+
print(f"An error occurred: {str(e)}")
|
| 151 |
+
print(traceback.format_exc())
|
| 152 |
+
raise HTTPException(status_code=500, detail=f"An unexpected error occurred: {str(e)}")
|
| 153 |
+
|
| 154 |
+
@app.post("/make_video")
|
| 155 |
+
async def make_video(request: Request):
|
| 156 |
+
try:
|
| 157 |
+
# Generate a unique filename for the output
|
| 158 |
+
output_filename = f"{uuid.uuid4()}.mp4"
|
| 159 |
+
output_path = os.path.join(OUTPUT_DIR, output_filename)
|
| 160 |
+
|
| 161 |
+
# Call the modal API with the request data and download the output file
|
| 162 |
+
data = await request.json()
|
| 163 |
+
async with aiohttp.ClientSession() as session:
|
| 164 |
+
async with session.post(f"{MODAL_BASE_URL}/make_video", json=data) as response:
|
| 165 |
+
if response.status != 200:
|
| 166 |
+
raise HTTPException(status_code=500, detail="Failed to process request")
|
| 167 |
+
output_data = await response.read()
|
| 168 |
+
async with aiofiles.open(output_path, "wb") as f:
|
| 169 |
+
await f.write(output_data)
|
| 170 |
+
|
| 171 |
+
# Return the URL path to the output file
|
| 172 |
+
return f"https://sxqib-api.hf.space/output/{output_filename}"
|
| 173 |
+
|
| 174 |
+
except Exception as e:
|
| 175 |
+
print(f"An error occurred: {str(e)}")
|
| 176 |
+
print(traceback.format_exc())
|
| 177 |
+
raise HTTPException(status_code=500, detail=f"An unexpected error occurred: {str(e)}")
|
| 178 |
+
|
| 179 |
+
# if __name__ == "__main__":
|
| 180 |
+
# import uvicorn
|
| 181 |
+
# uvicorn.run(app, host="0.0.0.0", port=8000)
|