Spaces:
Sleeping
Sleeping
Download app.py from cozeapi/reupload_images: direct link, hf CLI and curl.
- Browser
- Download file 3.31 kB
-
https://huggingface.co/spaces/cozeapi/reupload_images/resolve/main/app.py
- Command line
-
hf download hf://spaces/cozeapi/reupload_images/app.py
-
curl -L -o app.py https://huggingface.co/spaces/cozeapi/reupload_images/resolve/main/app.py
3.31 kB
| from typing import Any | |
| from fastapi import FastAPI | |
| import httpx | |
| import asyncio | |
| import re | |
| from pydantic import BaseModel | |
| class ImageLinks(BaseModel): | |
| links: dict[str, Any] | |
| app = FastAPI() | |
| async def temp_upload_file(file_data: bytes) -> str: | |
| servers = [ | |
| ('https://x0.at/', 'file'), # 100 MiB | |
| ('https://uguu.se/upload.php', 'files[]'), # 64Mb | |
| ('https://tmpfiles.org/api/v1/upload', 'file'), # 100Mb | |
| ] | |
| for i in range(3): | |
| for server, file_key in servers: | |
| try: | |
| async with httpx.AsyncClient() as client: | |
| files = {file_key: file_data} | |
| response = await client.post(server, files=files) | |
| if response.status_code == 200: | |
| if server == 'https://tmpfiles.org/api/v1/upload': | |
| response_json = response.json() | |
| if response_json['status'] == 'success': | |
| return response_json['data']['url'].replace("https://tmpfiles.org/", "https://tmpfiles.org/dl/") | |
| elif server == 'https://uguu.se/upload.php': | |
| response_json = response.json() | |
| if response_json['success']: | |
| return response_json['files'][0]['url'] | |
| else: | |
| return response.text.strip() | |
| except: | |
| pass | |
| return None | |
| async def telegraph_upload_media(file_data: bytes) -> str | None: | |
| url = 'https://telegra.ph/upload' | |
| headers = { | |
| 'authority': url.rsplit('/')[2], | |
| 'accept': 'application/json, text/javascript, */*; q=0.01', | |
| 'origin': url.rsplit('/', 1)[0], | |
| 'referer': url.rsplit('/', 1)[0], | |
| 'x-requested-with': 'XMLHttpRequest', | |
| } | |
| async with httpx.AsyncClient() as client: | |
| response = await client.post(url, headers=headers, files={'file': ('blob', file_data, 'image/png')}) | |
| result = response.json() | |
| if response.is_success and 'error' not in result: | |
| link = result[0]['src'] | |
| return url.rsplit('/', 1)[0] + link | |
| else: | |
| return None | |
| async def download_image(url: str) -> bytes: | |
| try: | |
| async with httpx.AsyncClient() as client: | |
| response = await client.get(url) | |
| if response.status_code == 200: | |
| return response.content | |
| except: | |
| pass | |
| return None | |
| async def get_links(original_url: str) -> str: | |
| file = await download_image(original_url) | |
| if file: | |
| return await telegraph_upload_media(file) or await temp_upload_file(file) or original_url | |
| else: | |
| return original_url | |
| async def upload_images(links: dict): | |
| urls = [] | |
| for key, link in links.items(): | |
| match = re.search(r'\((.*?)\)', link) | |
| if match: | |
| url = match.group(1) | |
| urls.append(url) | |
| upload_tasks = [get_links(url) for url in urls] | |
| uploaded_urls = await asyncio.gather(*upload_tasks) | |
| if not all(uploaded_urls): | |
| uploaded_urls = [url for url in uploaded_urls if url] | |
| return {"uploaded_urls": uploaded_urls} | |
| if __name__ == "__main__": | |
| import uvicorn | |
| uvicorn.run(app, host="127.0.0.1", port=9898) | |