File size: 16,254 Bytes
4550586 2fec25c b675688 e3fe383 b675688 4550586 4055d4c 2fec25c 8ad9edd daf8462 8ad9edd fcee60b 8ad9edd df8d5b7 8ad9edd df8d5b7 4550586 daf8462 8ad9edd df8d5b7 8ad9edd ec7de72 2fec25c 4055d4c 2fec25c 4055d4c b675688 455b2c2 4055d4c 1a06eb9 d4e4dea 4550586 1ebc125 4550586 1ebc125 b675688 02f3fa6 1ebc125 4055d4c d4e4dea 4550586 1ebc125 a558193 512d231 49f4a8c 4550586 4a7efc9 15616e0 4a7efc9 1ebc125 b675688 4550586 1ebc125 4055d4c 86f2866 ec7de72 2fec25c fcee60b c636ab9 2cd1847 4550586 da87cda 4550586 c636ab9 4550586 c636ab9 4550586 ec7de72 4550586 fcee60b e3fe383 ec7de72 8e57d9e 8ad9edd 4550586 8ad9edd 4550586 8ad9edd 4550586 8ad9edd 8e57d9e 8ad9edd 4550586 8ad9edd 4550586 8ad9edd 4550586 8ad9edd d3c9c80 c5714b3 8ad9edd 8e57d9e 8ad9edd 10460dc 8ad9edd d803483 8ad9edd 4550586 3ca47bc 8ad9edd 4550586 8ad9edd |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 |
from fastapi import FastAPI, HTTPException, Request
from fastapi.staticfiles import StaticFiles
from concurrent.futures import ThreadPoolExecutor
from youtube_transcript_api import YouTubeTranscriptApi
import asyncio
import aiohttp
import aiofiles
import tempfile
import uuid
import base64
import io
import os
import random
import traceback
import string
import json
app = FastAPI()
MODAL_BASE_URL = "https://sxqib--api-fastapi-app.modal.run"
def generate_hash(length=12):
# Characters that can appear in the hash
characters = string.ascii_lowercase + string.digits
# Generate a random string of the specified length
hash_string = ''.join(random.choice(characters) for _ in range(length))
return hash_string
@app.get("/")
async def read_root():
return {"message": "Saqib's API"}
# Create a directory to store MP3 files if it doesn't exist
AUDIO_DIR = "audio_files"
os.makedirs(AUDIO_DIR, exist_ok=True)
# Create a directory for storing output files
OUTPUT_DIR = "output"
os.makedirs(OUTPUT_DIR, exist_ok=True)
# Mount the audio directory
app.mount("/audio", StaticFiles(directory=AUDIO_DIR), name="audio")
# Mount the output directory
app.mount("/output", StaticFiles(directory=OUTPUT_DIR), name="output")
thread_pool = ThreadPoolExecutor(max_workers=2)
async def run_ffmpeg_async(ffmpeg_command):
loop = asyncio.get_running_loop()
await loop.run_in_executor(thread_pool, ffmpeg_command)
async def download_file(url: str, suffix: str):
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
if response.status != 200:
raise HTTPException(status_code=400, detail=f"Failed to download file from {url}")
with tempfile.NamedTemporaryFile(delete=False, suffix=suffix) as temp_file:
temp_file.write(await response.read())
return temp_file.name
@app.post("/add_audio_to_image")
async def add_audio_to_image(request: Request):
try:
# Generate a unique filename
output_filename = f"{uuid.uuid4()}.mp4"
output_path = os.path.join(OUTPUT_DIR, output_filename)
# Call the modal API with the request data and download the output file
data = await request.json()
async with aiohttp.ClientSession() as session:
async with session.post(f"{MODAL_BASE_URL}/add_audio_to_image", json=data) as response:
if response.status != 200:
raise HTTPException(status_code=500, detail="Failed to process request")
output_data = await response.read()
async with aiofiles.open(output_path, "wb") as f:
await f.write(output_data)
# Return the URL path to the output file
return f"https://sxqib-api.hf.space/output/{output_filename}"
except Exception as e:
print(f"An error occurred: {str(e)}")
print(traceback.format_exc())
raise HTTPException(status_code=500, detail=f"An unexpected error occurred: {str(e)}")
@app.post("/add_audio_to_video")
async def add_audio_to_video(request: Request):
try:
# Generate a unique filename
output_filename = f"{uuid.uuid4()}.mp4"
output_path = os.path.join(OUTPUT_DIR, output_filename)
# Call the modal API with the request data and download the output file
data = await request.json()
async with aiohttp.ClientSession() as session:
async with session.post(f"{MODAL_BASE_URL}/add_audio_to_video", json=data) as response:
if response.status != 200:
raise HTTPException(status_code=500, detail="Failed to process request")
output_data = await response.read()
async with aiofiles.open(output_path, "wb") as f:
await f.write(output_data)
# Return the URL path to the output file
return f"https://sxqib-api.hf.space/output/{output_filename}"
except Exception as e:
print(f"An error occurred: {str(e)}")
print(traceback.format_exc())
raise HTTPException(status_code=500, detail=f"An unexpected error occurred: {str(e)}")
@app.post("/concatenate_videos")
async def concatenate_videos(request: Request):
try:
# Generate a unique filename for the output
output_filename = f"{uuid.uuid4()}.mp4"
output_path = os.path.join(OUTPUT_DIR, output_filename)
# Call the modal API with the request data and download the output file
data = await request.json()
async with aiohttp.ClientSession() as session:
async with session.post(f"{MODAL_BASE_URL}/concatenate_videos", json=data) as response:
if response.status != 200:
raise HTTPException(status_code=500, detail="Failed to process request")
output_data = await response.read()
async with aiofiles.open(output_path, "wb") as f:
await f.write(output_data)
# Return the URL path to the output file
return f"https://sxqib-api.hf.space/output/{output_filename}"
except Exception as e:
print(f"An error occurred: {str(e)}")
print(traceback.format_exc())
raise HTTPException(status_code=500, detail=f"An unexpected error occurred: {str(e)}")
@app.post("/concatenate_audio")
async def concatenate_audio(request: Request):
try:
# Generate a unique filename for the output
output_filename = f"{uuid.uuid4()}.mp3"
output_path = os.path.join(AUDIO_DIR, output_filename)
# Call the modal API with the request data and download the output file
data = await request.json()
async with aiohttp.ClientSession() as session:
async with session.post(f"{MODAL_BASE_URL}/concatenate_audio", json=data) as response:
if response.status != 200:
raise HTTPException(status_code=500, detail="Failed to process request")
output_data = await response.read()
async with aiofiles.open(output_path, "wb") as f:
await f.write(output_data)
# Return the URL path to the output file
return f"https://sxqib-api.hf.space/audio_files/{output_filename}"
except Exception as e:
print(f"An error occurred: {str(e)}")
print(traceback.format_exc())
raise HTTPException(status_code=500, detail=f"An unexpected error occurred: {str(e)}")
@app.post("/make_video")
async def make_video(request: Request):
try:
# Generate a unique filename for the output
output_filename = f"{uuid.uuid4()}.mp4"
output_path = os.path.join(OUTPUT_DIR, output_filename)
# Call the modal API with the request data and download the output file
data = await request.json()
async with aiohttp.ClientSession() as session:
async with session.post(f"{MODAL_BASE_URL}/make_video", json=data) as response:
if response.status != 200:
raise HTTPException(status_code=500, detail="Failed to process request")
output_data = await response.read()
async with aiofiles.open(output_path, "wb") as f:
await f.write(output_data)
# Return the URL path to the output file
return f"https://sxqib-api.hf.space/output/{output_filename}"
except Exception as e:
print(f"An error occurred: {str(e)}")
print(traceback.format_exc())
raise HTTPException(status_code=500, detail=f"An unexpected error occurred: {str(e)}")
@app.get("/get_youtube_audio")
async def get_youtube_audio(request: Request):
try:
# Generate a unique filename
unique_filename = f"{uuid.uuid4().hex}.mp3"
out_file = os.path.join(AUDIO_DIR, unique_filename)
# Call the modal API with the request parameters and download the output file
data = request.query_params
async with aiohttp.ClientSession() as session:
async with session.get(f"{MODAL_BASE_URL}/get_youtube_audio", params=data) as response:
if response.status != 200:
raise HTTPException(status_code=500, detail="Failed to process request")
audio_data = await response.read()
async with aiofiles.open(out_file, "wb") as f:
await f.write(audio_data)
# Return the URL path to the output file
return f"https://sxqib-api.hf.space/audio/{unique_filename}"
except Exception as e:
print(f"An error occurred: {str(e)}")
print(traceback.format_exc())
raise HTTPException(status_code=500, detail=f"An unexpected error occurred: {str(e)}")
@app.get("/get_youtube_transcript")
async def get_youtube_transcript(request: Request):
try:
# Get the video ID from the query parameters
video_id = request.query_params.get("video_id")
if not video_id:
raise HTTPException(status_code=400, detail="video_id parameter is required")
# Get the transcript for the video
transcript = YouTubeTranscriptApi.get_transcript(video_id)
# Format the transcript into a single string
transcript_text = " ".join([i["text"] for i in transcript])
return {"transcript": transcript_text}
except Exception as e:
print(f"An error occurred: {str(e)}")
print(traceback.format_exc())
raise HTTPException(status_code=500, detail=f"An unexpected error occurred: {str(e)}")
@app.post("/whisper")
async def whisper(request: Request):
data = await request.json() # Extracting JSON data from request
if "audio_url" not in data:
raise HTTPException(status_code=400, detail="audio_url not found in request")
url = data["audio_url"]
headers = {
'Accept': 'application/json, text/plain, */*',
'Accept-Language': 'en-US,en;q=0.9',
'Cache-Control': 'no-cache',
'Connection': 'keep-alive',
'Content-Type': 'application/json',
'DNT': '1',
'Origin': 'https://deepinfra.com',
'Pragma': 'no-cache',
'Referer': 'https://deepinfra.com/',
'Sec-Fetch-Dest': 'empty',
'Sec-Fetch-Mode': 'cors',
'Sec-Fetch-Site': 'same-site',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36 Edg/124.0.0.0',
'sec-ch-ua': '"Chromium";v="124", "Microsoft Edge";v="124", "Not-A.Brand";v="99"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': '"Windows"',
}
# Async HTTP request to get the audio file
async with aiohttp.ClientSession() as session:
async with session.get(url) as resp:
if resp.status != 200:
return f"Failed to download audio: {resp.status}"
audio_data = await resp.read()
# Encode the audio data to base64
audio_base64 = base64.b64encode(audio_data).decode("utf-8")
json_data = '{"audio": "' + audio_base64 + '"}'
# Post request to the API
async with session.post(f'https://api.deepinfra.com/v1/inference/openai/whisper-large', headers=headers, data=json_data) as post_resp:
if post_resp.status != 200:
return f"API request failed: {post_resp.status}"
return await post_resp.json()
@app.post("/img2location")
async def img2location(request: Request):
request_json = await request.json()
image_url = request_json.get("image_url", None)
if not image_url:
raise HTTPException(status_code=400, detail="image_url not found in request")
headers = {
'accept': '*/*',
'accept-language': 'en-US,en;q=0.9',
'cache-control': 'no-cache',
'dnt': '1',
'origin': 'https://geospy.ai',
'pragma': 'no-cache',
'priority': 'u=1, i',
'referer': 'https://geospy.ai/',
'sec-ch-ua': '"Chromium";v="124", "Microsoft Edge";v="124", "Not-A.Brand";v="99"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': '"Windows"',
'sec-fetch-dest': 'empty',
'sec-fetch-mode': 'cors',
'sec-fetch-site': 'cross-site',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36 Edg/124.0.0.0',
}
async with aiohttp.ClientSession() as session:
# Fetch the image from the URL
async with session.get(image_url) as img_response:
if img_response.status != 200:
return f"Failed to fetch image: HTTP {img_response.status}"
image_data = await img_response.read()
# Using BytesIO to handle the byte content
data = aiohttp.FormData()
data.add_field('image', io.BytesIO(image_data), filename="image.png", content_type='image/png')
# Sending the POST request
async with session.post("https://locate-image-7cs5mab6na-uc.a.run.app/", headers=headers, data=data) as response:
if response.status != 200:
return f"Failed to upload image: HTTP {response.status}"
json_response = await response.json()
if json_response["message"]["latitude"] and json_response["message"]["longitude"]:
json_response["message"]["latitude"] = str(json_response["message"]["latitude"])
json_response["message"]["longitude"] = str(json_response["message"]["longitude"])
return json_response
@app.post("/pixart-sigma")
async def pixart_sigma(request: Request):
request_json = await request.json()
prompt = request_json.get("prompt", None)
negative_prompt = request_json.get("negative_prompt", "")
style = request_json.get("style", "(No style)")
use_negative_prompt = request_json.get("use_negative_prompt", True)
num_imgs = request_json.get("num_imgs", 1)
seed = request_json.get("seed", 0)
width = request_json.get("width", 1024)
height = request_json.get("height", 1024)
schedule = request_json.get("schedule", "DPM-Solver")
dpms_guidance_scale = request_json.get("dpms_guidance_scale", 4.5)
sas_guidance_scale = request_json.get("sas_guidance_scale", 3)
dpms_inference_steps = request_json.get("dpms_inference_steps", 14)
sas_inference_steps = request_json.get("sas_inference_steps", 25)
randomize_seed = request_json.get("randomize_seed", True)
hash = generate_hash()
headers = {
'accept': '*/*'
}
params = {
'__theme': 'light',
}
json_data = {
'data': [
prompt,
negative_prompt,
style,
use_negative_prompt,
num_imgs,
seed,
width,
height,
schedule,
dpms_guidance_scale,
sas_guidance_scale,
dpms_inference_steps,
sas_inference_steps,
randomize_seed,
],
'event_data': None,
'fn_index': 3,
'trigger_id': 7,
'session_hash': hash,
}
async with aiohttp.ClientSession() as session:
async with session.post(f'https://pixart-alpha-pixart-sigma.hf.space/queue/join', params=params, headers=headers, json=json_data, ssl=False) as response:
print(response.status)
params = {
'session_hash': hash,
}
async with session.get(f'https://pixart-alpha-pixart-sigma.hf.space/queue/data', params=params, headers=headers, ssl=False) as response:
async for line in response.content:
try:
if line:
line = line.decode('utf-8')
line = line.replace('data: ', '')
line_json = json.loads(line)
if line_json["msg"] == "process_completed":
image_url = line_json["output"]["data"][0][0]["image"]["url"]
return {"image_url": image_url}
except:
pass
# if __name__ == "__main__":
# import uvicorn
# uvicorn.run(app, host="0.0.0.0", port=8000) |