2024-10-11 09:37:51 +03:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
import json
|
2024-11-07 22:27:26 +03:00
|
|
|
import random
|
|
|
|
from aiohttp import ClientSession, ClientError
|
2024-11-07 23:05:40 +03:00
|
|
|
import asyncio
|
|
|
|
from itertools import cycle
|
2024-10-11 09:37:51 +03:00
|
|
|
|
|
|
|
from ..typing import AsyncResult, Messages
|
|
|
|
from .base_provider import AsyncGeneratorProvider, ProviderModelMixin
|
|
|
|
from ..image import ImageResponse
|
|
|
|
|
|
|
|
class AIUncensored(AsyncGeneratorProvider, ProviderModelMixin):
|
2024-11-07 22:27:26 +03:00
|
|
|
url = "https://www.aiuncensored.info/ai_uncensored"
|
2024-11-01 23:53:15 +03:00
|
|
|
api_endpoints_text = [
|
|
|
|
"https://twitterclone-i0wr.onrender.com/api/chat",
|
|
|
|
"https://twitterclone-4e8t.onrender.com/api/chat",
|
|
|
|
"https://twitterclone-8wd1.onrender.com/api/chat",
|
|
|
|
]
|
|
|
|
api_endpoints_image = [
|
|
|
|
"https://twitterclone-4e8t.onrender.com/api/image",
|
|
|
|
"https://twitterclone-i0wr.onrender.com/api/image",
|
|
|
|
"https://twitterclone-8wd1.onrender.com/api/image",
|
|
|
|
]
|
2024-10-11 09:37:51 +03:00
|
|
|
working = True
|
|
|
|
supports_stream = True
|
|
|
|
supports_system_message = True
|
|
|
|
supports_message_history = True
|
|
|
|
|
2024-11-01 23:53:15 +03:00
|
|
|
default_model = 'TextGenerations'
|
|
|
|
text_models = [default_model]
|
|
|
|
image_models = ['ImageGenerations']
|
|
|
|
models = [*text_models, *image_models]
|
|
|
|
|
|
|
|
model_aliases = {
|
|
|
|
"flux": "ImageGenerations",
|
2024-10-11 09:37:51 +03:00
|
|
|
}
|
|
|
|
|
2024-11-07 22:27:26 +03:00
|
|
|
@staticmethod
|
|
|
|
def generate_cipher() -> str:
|
2024-11-07 23:05:40 +03:00
|
|
|
"""Generate a cipher in format like '3221229284179118'"""
|
2024-11-07 22:27:26 +03:00
|
|
|
return ''.join([str(random.randint(0, 9)) for _ in range(16)])
|
|
|
|
|
2024-10-11 09:37:51 +03:00
|
|
|
@classmethod
|
|
|
|
def get_model(cls, model: str) -> str:
|
|
|
|
if model in cls.models:
|
|
|
|
return model
|
2024-11-01 23:53:15 +03:00
|
|
|
elif model in cls.model_aliases:
|
|
|
|
return cls.model_aliases[model]
|
2024-10-11 09:37:51 +03:00
|
|
|
else:
|
|
|
|
return cls.default_model
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
async def create_async_generator(
|
|
|
|
cls,
|
|
|
|
model: str,
|
|
|
|
messages: Messages,
|
|
|
|
proxy: str = None,
|
|
|
|
**kwargs
|
|
|
|
) -> AsyncResult:
|
|
|
|
model = cls.get_model(model)
|
|
|
|
|
2024-11-01 23:53:15 +03:00
|
|
|
headers = {
|
|
|
|
'accept': '*/*',
|
|
|
|
'accept-language': 'en-US,en;q=0.9',
|
|
|
|
'cache-control': 'no-cache',
|
|
|
|
'content-type': 'application/json',
|
|
|
|
'origin': 'https://www.aiuncensored.info',
|
|
|
|
'pragma': 'no-cache',
|
|
|
|
'priority': 'u=1, i',
|
|
|
|
'referer': 'https://www.aiuncensored.info/',
|
|
|
|
'sec-ch-ua': '"Not?A_Brand";v="99", "Chromium";v="130"',
|
|
|
|
'sec-ch-ua-mobile': '?0',
|
|
|
|
'sec-ch-ua-platform': '"Linux"',
|
|
|
|
'sec-fetch-dest': 'empty',
|
|
|
|
'sec-fetch-mode': 'cors',
|
|
|
|
'sec-fetch-site': 'cross-site',
|
|
|
|
'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36'
|
|
|
|
}
|
|
|
|
|
|
|
|
async with ClientSession(headers=headers) as session:
|
|
|
|
if model in cls.image_models:
|
|
|
|
prompt = messages[-1]['content']
|
2024-10-11 09:37:51 +03:00
|
|
|
data = {
|
2024-11-01 23:53:15 +03:00
|
|
|
"prompt": prompt,
|
2024-11-07 22:27:26 +03:00
|
|
|
"cipher": cls.generate_cipher()
|
2024-10-11 09:37:51 +03:00
|
|
|
}
|
2024-11-07 22:27:26 +03:00
|
|
|
|
2024-11-07 23:05:40 +03:00
|
|
|
endpoints = cycle(cls.api_endpoints_image)
|
|
|
|
|
|
|
|
while True:
|
|
|
|
endpoint = next(endpoints)
|
|
|
|
try:
|
|
|
|
async with session.post(endpoint, json=data, proxy=proxy, timeout=10) as response:
|
|
|
|
response.raise_for_status()
|
|
|
|
response_data = await response.json()
|
|
|
|
image_url = response_data['image_url']
|
|
|
|
image_response = ImageResponse(images=image_url, alt=prompt)
|
|
|
|
yield image_response
|
|
|
|
return
|
|
|
|
except (ClientError, asyncio.TimeoutError):
|
|
|
|
continue
|
|
|
|
|
2024-11-01 23:53:15 +03:00
|
|
|
elif model in cls.text_models:
|
|
|
|
data = {
|
2024-11-07 22:27:26 +03:00
|
|
|
"messages": messages,
|
|
|
|
"cipher": cls.generate_cipher()
|
2024-11-01 23:53:15 +03:00
|
|
|
}
|
2024-11-07 23:05:40 +03:00
|
|
|
|
|
|
|
endpoints = cycle(cls.api_endpoints_text)
|
|
|
|
|
|
|
|
while True:
|
|
|
|
endpoint = next(endpoints)
|
|
|
|
try:
|
|
|
|
async with session.post(endpoint, json=data, proxy=proxy, timeout=10) as response:
|
|
|
|
response.raise_for_status()
|
|
|
|
full_response = ""
|
|
|
|
async for line in response.content:
|
|
|
|
line = line.decode('utf-8')
|
|
|
|
if line.startswith("data: "):
|
|
|
|
try:
|
|
|
|
json_str = line[6:]
|
|
|
|
if json_str != "[DONE]":
|
|
|
|
data = json.loads(json_str)
|
|
|
|
if "data" in data:
|
|
|
|
full_response += data["data"]
|
|
|
|
yield data["data"]
|
|
|
|
except json.JSONDecodeError:
|
|
|
|
continue
|
|
|
|
return
|
|
|
|
except (ClientError, asyncio.TimeoutError):
|
|
|
|
continue
|