from fastapi import FastAPI, File, UploadFile, Request from fastapi.responses import HTMLResponse, JSONResponse, StreamingResponse import requests import time import asyncio from typing import Dict import aiohttp app = FastAPI() HTML_CONTENT = """ Radd PRO Uploader

Radd PRO Uploader

or drag and drop file here/paste image

Allowed file types: .zip, .mp4, .txt, .mp3, all image types, .pdf
×

Upload History

×
""" @app.get("/", response_class=HTMLResponse) async def index(): return HTML_CONTENT @app.post("/upload") async def handle_upload(file: UploadFile = File(...)): if not file.filename: return JSONResponse(content={"error": "No file selected."}, status_code=400) cookies = await get_cookies() if 'csrftoken' not in cookies or 'sessionid' not in cookies: return JSONResponse(content={"error": "Failed to get cookies"}, status_code=500) upload_result = await initiate_upload(cookies, file.filename, file.content_type) if not upload_result or 'upload_url' not in upload_result: return JSONResponse(content={"error": "Failed to initiate upload"}, status_code=500) upload_success = await chunked_upload(upload_result['upload_url'], file, file.content_type) if not upload_success: return JSONResponse(content={"error": "Failed to upload file after multiple attempts"}, status_code=500) original_url = upload_result['serving_url'] mirrored_url = f"/rbxg/{original_url.split('/pbxt/')[1]}" return JSONResponse(content={"url": mirrored_url}) @app.get("/rbxg/{path:path}") async def handle_video_stream(path: str, request: Request): original_url = f'https://replicate.delivery/pbxt/{path}' range_header = request.headers.get('Range') headers = {'Range': range_header} if range_header else {} async with aiohttp.ClientSession() as session: async with session.get(original_url, headers=headers) as response: content = await response.read() headers = dict(response.headers) headers['Access-Control-Allow-Origin'] = '*' headers['Content-Disposition'] = 'inline' if response.status == 206: headers['Content-Range'] = response.headers.get('Content-Range') return StreamingResponse(content, status_code=response.status, headers=headers) @app.get("/embed") async def embed_video(url: str, thumbnail: str): html = f''' ''' return HTMLResponse(content=html) async def get_cookies() -> Dict[str, str]: async with aiohttp.ClientSession() as session: async with session.get('https://replicate.com/levelsio/neon-tokyo', headers={ 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36' }) as response: return dict(response.cookies) async def initiate_upload(cookies: Dict[str, str], filename: str, content_type: str) -> Dict: url = f'https://replicate.com/api/upload/{filename}?content_type={content_type}' async with aiohttp.ClientSession() as session: async with session.post(url, cookies=cookies, headers={ 'X-CSRFToken': cookies.get('csrftoken'), 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36', 'Referer': 'https://replicate.com/levelsio/neon-tokyo', 'Origin': 'https://replicate.com', 'Accept': '*/*', 'Accept-Language': 'en-US,en;q=0.5', 'Accept-Encoding': 'identity', 'Sec-Fetch-Dest': 'empty', 'Sec-Fetch-Mode': 'cors', 'Sec-Fetch-Site': 'same-origin', 'Sec-GPC': '1', 'Priority': 'u=1, i' }) as response: return await response.json() async def chunked_upload(upload_url: str, file: UploadFile, content_type: str, chunk_size: int = 1024 * 1024) -> bool: async with aiohttp.ClientSession() as session: file_size = await file.seek(0, 2) await file.seek(0) for start in range(0, file_size, chunk_size): end = min(start + chunk_size, file_size) chunk = await file.read(end - start) headers = { 'Content-Type': content_type, 'Content-Range': f'bytes {start}-{end-1}/{file_size}' } for attempt in range(5): # 5 retries try: async with session.put(upload_url, data=chunk, headers=headers) as response: if response.status == 200: break except aiohttp.ClientError: if attempt == 4: # Last attempt return False await asyncio.sleep(2 ** attempt) # Exponential backoff else: return False return True if __name__ == "__main__": import uvicorn uvicorn.run(app, host="0.0.0.0", port=7860)