File size: 10,661 Bytes
a4b70d9 |
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 |
from __future__ import annotations
import os
import time
import json
import random
from pathlib import Path
from aiohttp import ClientSession, ClientResponse
import asyncio
from ...typing import AsyncResult, Messages
from ...providers.response import ImageResponse, Reasoning
from ...errors import ResponseError, ModelNotFoundError
from ...cookies import get_cookies_dir
from ..base_provider import AsyncGeneratorProvider, ProviderModelMixin
from ..helper import format_media_prompt
from ... import debug
class ARTA(AsyncGeneratorProvider, ProviderModelMixin):
url = "https://ai-arta.com"
auth_url = "https://www.googleapis.com/identitytoolkit/v3/relyingparty/signupNewUser?key=AIzaSyB3-71wG0fIt0shj0ee4fvx1shcjJHGrrQ"
token_refresh_url = "https://securetoken.googleapis.com/v1/token?key=AIzaSyB3-71wG0fIt0shj0ee4fvx1shcjJHGrrQ"
image_generation_url = "https://img-gen-prod.ai-arta.com/api/v1/text2image"
status_check_url = "https://img-gen-prod.ai-arta.com/api/v1/text2image/{record_id}/status"
working = False # Take down request
default_model = "flux"
default_image_model = default_model
model_aliases = {
"anything-xl": "Anything-xl",
"high-gpt4o": "High GPT4o",
"on-limbs-black": "On limbs black",
"f-dev": "F Dev",
"flux-dev": "F Dev", # Added
"sdxl-1.0": "SDXL 1.0", # Added
"old-school": "Old School",
"vincent-van-gogh": "Vincent Van Gogh",
"cor-epica-xl": "Cor-epica-xl",
"professional": "Professional",
"cheyenne-xl": "Cheyenne-xl",
"chicano": "Chicano",
"sdxl-l": "SDXL L", # Added
"black-ink": "Black Ink",
"juggernaut-xl": "Juggernaut-xl",
"cinematic-art": "Cinematic Art",
"dreamshaper-xl": "Dreamshaper-xl",
"fantasy-art": "Fantasy Art",
"neo-traditional": "Neo-traditional",
"realistic-stock-xl": "Realistic-stock-xl",
"flame-design": "Flame design",
"japanese-2": "Japanese_2",
"medieval": "Medieval",
"surrealism": "Surrealism",
"dotwork": "Dotwork",
"graffiti": "Graffiti",
"revanimated": "RevAnimated",
"on-limbs-color": "On limbs color",
"old-school-colored": "Old school colored",
"gpt4o-ghibli": "GPT4o Ghibli",
"low-poly": "Low Poly",
"gpt4o": "GPT4o",
"gpt-image": ["GPT4o", "High GPT4o", "GPT4o Ghibli"],
"no-style": "No Style",
"anime": "Anime",
"tattoo": "tattoo",
"embroidery-tattoo": "Embroidery tattoo",
"mini-tattoo": "Mini tattoo",
"realistic-tattoo": "Realistic tattoo",
"playground-xl": "Playground-xl",
"Watercolor": "Watercolor",
"f-pro": "F Pro",
"flux-pro": "F Pro", # Added
"kawaii": "Kawaii",
"photographic": "Photographic",
"katayama-mix-xl": "Katayama-mix-xl",
"death-metal": "Death metal",
"new-school": "New School",
"pony-xl": "Pony-xl",
"anima-pencil-xl": "Anima-pencil-xl",
default_image_model: "Flux", # Added
"biomech": "Biomech",
"yamers-realistic-xl": "Yamers-realistic-xl",
"trash-polka": "Trash Polka",
"red-and-black": "Red and Black",
}
image_models = list(model_aliases.keys())
models = image_models
@classmethod
def get_model(cls, model: str) -> str:
"""Get the internal model name from the user-provided model name."""
if not model:
return cls.model_aliases[cls.default_model]
# Always check aliases first to get the proper API name
if model in cls.model_aliases:
alias = cls.model_aliases[model]
# If the alias is a list, randomly select one of the options
if isinstance(alias, list):
selected_model = random.choice(alias)
debug.log(f"ARTA: Selected model '{selected_model}' from alias '{model}'")
return selected_model
debug.log(f"ARTA: Using model '{alias}' for alias '{model}'")
return alias
# If not in aliases, check if it's a direct API model name
api_model_names = [v for v in cls.model_aliases.values() if isinstance(v, str)]
if model in api_model_names:
return model
raise ModelNotFoundError(f"Model {model} not found")
@classmethod
def get_auth_file(cls):
path = Path(get_cookies_dir())
path.mkdir(exist_ok=True)
filename = f"auth_{cls.__name__}.json"
return path / filename
@classmethod
async def create_token(cls, path: Path, proxy: str | None = None):
async with ClientSession() as session:
# Step 1: Generate Authentication Token
auth_payload = {"clientType": "CLIENT_TYPE_ANDROID"}
async with session.post(cls.auth_url, json=auth_payload, proxy=proxy) as auth_response:
await raise_error(f"Failed to obtain authentication token", auth_response)
auth_data = await auth_response.json()
auth_token = auth_data.get("idToken")
#refresh_token = auth_data.get("refreshToken")
if not auth_token:
raise ResponseError("Failed to obtain authentication token.")
json.dump(auth_data, path.open("w"))
return auth_data
@classmethod
async def refresh_token(cls, refresh_token: str, proxy: str = None) -> tuple[str, str]:
async with ClientSession() as session:
payload = {
"grant_type": "refresh_token",
"refresh_token": refresh_token,
}
async with session.post(cls.token_refresh_url, data=payload, proxy=proxy) as response:
await raise_error(f"Failed to refresh token", response)
response_data = await response.json()
return response_data.get("id_token"), response_data.get("refresh_token")
@classmethod
async def read_and_refresh_token(cls, proxy: str | None = None) -> str:
path = cls.get_auth_file()
if path.is_file():
auth_data = json.load(path.open("rb"))
diff = time.time() - os.path.getmtime(path)
expiresIn = int(auth_data.get("expiresIn"))
if diff < expiresIn:
if diff > expiresIn / 2:
auth_data["idToken"], auth_data["refreshToken"] = await cls.refresh_token(auth_data.get("refreshToken"), proxy)
json.dump(auth_data, path.open("w"))
return auth_data
return await cls.create_token(path, proxy)
@classmethod
async def create_async_generator(
cls,
model: str,
messages: Messages,
proxy: str = None,
prompt: str = None,
negative_prompt: str = "blurry, deformed hands, ugly",
n: int = 1,
guidance_scale: int = 7,
num_inference_steps: int = 30,
aspect_ratio: str = None,
seed: int = None,
**kwargs
) -> AsyncResult:
model = cls.get_model(model)
prompt = format_media_prompt(messages, prompt)
# Generate a random seed if not provided
if seed is None:
seed = random.randint(9999, 99999999) # Common range for random seeds
# Step 1: Get Authentication Token
auth_data = await cls.read_and_refresh_token(proxy)
auth_token = auth_data.get("idToken")
async with ClientSession() as session:
# Step 2: Generate Images
# Create a form data structure as the API might expect form data instead of JSON
form_data = {
"prompt": prompt,
"negative_prompt": negative_prompt,
"style": model,
"images_num": str(n),
"cfg_scale": str(guidance_scale),
"steps": str(num_inference_steps),
"aspect_ratio": "1:1" if aspect_ratio is None else aspect_ratio,
"seed": str(seed),
}
headers = {
"Authorization": auth_token,
# No Content-Type header for multipart/form-data, aiohttp sets it automatically
}
# Try with form data instead of JSON
async with session.post(cls.image_generation_url, data=form_data, headers=headers, proxy=proxy) as image_response:
await raise_error(f"Failed to initiate image generation", image_response)
image_data = await image_response.json()
record_id = image_data.get("record_id")
if not record_id:
raise ResponseError(f"Failed to initiate image generation: {image_data}")
# Step 3: Check Generation Status
status_url = cls.status_check_url.format(record_id=record_id)
start_time = time.time()
last_status = None
while True:
async with session.get(status_url, headers=headers, proxy=proxy) as status_response:
await raise_error(f"Failed to check image generation status", status_response)
status_data = await status_response.json()
status = status_data.get("status")
if status == "DONE":
image_urls = [image["url"] for image in status_data.get("response", [])]
duration = time.time() - start_time
yield Reasoning(label="Generated", status=f"{n} image in {duration:.2f}s" if n == 1 else f"{n} images in {duration:.2f}s")
yield ImageResponse(urls=image_urls, alt=prompt)
return
elif status in ("IN_QUEUE", "IN_PROGRESS"):
if last_status != status:
last_status = status
if status == "IN_QUEUE":
yield Reasoning(label="Waiting")
else:
yield Reasoning(label="Generating")
await asyncio.sleep(2) # Poll every 2 seconds
else:
raise ResponseError(f"Image generation failed with status: {status}")
async def raise_error(message: str, response: ClientResponse):
if response.ok:
return
error_text = await response.text()
content_type = response.headers.get('Content-Type', 'unknown')
raise ResponseError(f"{message}. Content-Type: {content_type}, Response: {error_text}")
|