Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,6 @@
|
|
1 |
from asyncio import create_subprocess_shell, gather, sleep
|
2 |
from logging import ERROR, INFO, basicConfig, getLogger
|
3 |
from pathlib import Path
|
4 |
-
from random import choice
|
5 |
from shutil import rmtree
|
6 |
from subprocess import CalledProcessError, PIPE
|
7 |
from typing import Any, List
|
@@ -12,6 +11,7 @@ from fastapi import FastAPI, HTTPException
|
|
12 |
from fastapi.responses import PlainTextResponse
|
13 |
from httpx import AsyncClient, HTTPStatusError, RequestError
|
14 |
from pydantic import BaseModel, HttpUrl
|
|
|
15 |
from uvicorn import run as uvicorn_run
|
16 |
|
17 |
need_logging = False
|
@@ -135,25 +135,44 @@ async def upload_image(file_path: Path | str, file_type: str = 'png') -> str | N
|
|
135 |
async def optimize_and_upload(images_urls: list[str] | str, convert: bool = False) -> list[str]:
|
136 |
images_urls = [images_urls] if isinstance(images_urls, str) else images_urls
|
137 |
print(f'принятые ссылки в обработку ({len(images_urls)}): {images_urls}')
|
|
|
138 |
images_paths = await download_pngs(images_urls)
|
139 |
if not convert:
|
140 |
await optimize_pngs(images_paths)
|
|
|
141 |
new_images_urls = []
|
142 |
images_paths = images_paths if not convert else await convert_to_jpegs(images_paths)
|
143 |
-
|
|
|
144 |
new_url = await upload_image(image_path, 'png' if not convert else 'jpeg')
|
145 |
if new_url:
|
146 |
new_images_urls.append(new_url)
|
147 |
-
|
|
|
|
|
|
|
|
|
148 |
try:
|
149 |
image_path.unlink()
|
150 |
except Exception as e:
|
151 |
print(f'не удалось удалить файл {image_path}: {e}')
|
|
|
152 |
print(f'новые ссылки: ({len(new_images_urls)}): {new_images_urls}')
|
|
|
153 |
try:
|
154 |
rmtree(images_paths[0].parent)
|
155 |
except Exception as e:
|
156 |
print(f'не удалось удалить файл {images_paths[0].parent}: {e}')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
157 |
return new_images_urls
|
158 |
|
159 |
|
|
|
1 |
from asyncio import create_subprocess_shell, gather, sleep
|
2 |
from logging import ERROR, INFO, basicConfig, getLogger
|
3 |
from pathlib import Path
|
|
|
4 |
from shutil import rmtree
|
5 |
from subprocess import CalledProcessError, PIPE
|
6 |
from typing import Any, List
|
|
|
11 |
from fastapi.responses import PlainTextResponse
|
12 |
from httpx import AsyncClient, HTTPStatusError, RequestError
|
13 |
from pydantic import BaseModel, HttpUrl
|
14 |
+
from ytelegraph import TelegraphAPI
|
15 |
from uvicorn import run as uvicorn_run
|
16 |
|
17 |
need_logging = False
|
|
|
135 |
async def optimize_and_upload(images_urls: list[str] | str, convert: bool = False) -> list[str]:
|
136 |
images_urls = [images_urls] if isinstance(images_urls, str) else images_urls
|
137 |
print(f'принятые ссылки в обработку ({len(images_urls)}): {images_urls}')
|
138 |
+
|
139 |
images_paths = await download_pngs(images_urls)
|
140 |
if not convert:
|
141 |
await optimize_pngs(images_paths)
|
142 |
+
|
143 |
new_images_urls = []
|
144 |
images_paths = images_paths if not convert else await convert_to_jpegs(images_paths)
|
145 |
+
|
146 |
+
for old_url, image_path in zip(images_urls, images_paths):
|
147 |
new_url = await upload_image(image_path, 'png' if not convert else 'jpeg')
|
148 |
if new_url:
|
149 |
new_images_urls.append(new_url)
|
150 |
+
print(f'загружено изображение {image_path} в {new_url}')
|
151 |
+
else:
|
152 |
+
new_images_urls.append(old_url)
|
153 |
+
print(f'не удалось загрузить изображение {image_path}, оставим старую ссылку: {old_url}')
|
154 |
+
|
155 |
try:
|
156 |
image_path.unlink()
|
157 |
except Exception as e:
|
158 |
print(f'не удалось удалить файл {image_path}: {e}')
|
159 |
+
|
160 |
print(f'новые ссылки: ({len(new_images_urls)}): {new_images_urls}')
|
161 |
+
|
162 |
try:
|
163 |
rmtree(images_paths[0].parent)
|
164 |
except Exception as e:
|
165 |
print(f'не удалось удалить файл {images_paths[0].parent}: {e}')
|
166 |
+
|
167 |
+
if convert:
|
168 |
+
try:
|
169 |
+
ph = TelegraphAPI()
|
170 |
+
content = '\n\n'.join([f'' for url in new_images_urls])
|
171 |
+
ph_link = ph.create_page_md('Dall-E 3 картинки', content)
|
172 |
+
return ph_link
|
173 |
+
except Exception as e:
|
174 |
+
print(f'не удалось создать страницу в телеграфе: {e}')
|
175 |
+
|
176 |
return new_images_urls
|
177 |
|
178 |
|