Skip to content

Commit

Permalink
Support synthesize in Openai generator (#2394)
Browse files Browse the repository at this point in the history
* Improve download of generated images, serve images in the api

* Add support for conversation handling in the api

* Add orginal prompt to image response

* Add download images option in gui, fix loading model list in Airforce

* Support speech synthesize in Openai generator
  • Loading branch information
hlohaus authored Nov 21, 2024
1 parent ffb4b0d commit eae317a
Show file tree
Hide file tree
Showing 8 changed files with 259 additions and 76 deletions.
64 changes: 46 additions & 18 deletions g4f/Provider/needs_auth/OpenaiChat.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import base64
import time
import requests
from aiohttp import ClientWebSocketResponse
from copy import copy

try:
Expand All @@ -28,7 +27,7 @@
from ...requests.aiohttp import StreamSession
from ...image import ImageResponse, ImageRequest, to_image, to_bytes, is_accepted_format
from ...errors import MissingAuthError, ResponseError
from ...providers.response import BaseConversation
from ...providers.response import BaseConversation, FinishReason, SynthesizeData
from ..helper import format_cookies
from ..openai.har_file import get_request_config, NoValidHarFileError
from ..openai.har_file import RequestConfig, arkReq, arkose_url, start_url, conversation_url, backend_url, backend_anon_url
Expand Down Expand Up @@ -367,19 +366,13 @@ async def create_async_generator(
Raises:
RuntimeError: If an error occurs during processing.
"""
await cls.login(proxy)

async with StreamSession(
proxy=proxy,
impersonate="chrome",
timeout=timeout
) as session:
if cls._expires is not None and cls._expires < time.time():
cls._headers = cls._api_key = None
try:
await get_request_config(proxy)
cls._create_request_args(RequestConfig.cookies, RequestConfig.headers)
cls._set_api_key(RequestConfig.access_token)
except NoValidHarFileError as e:
await cls.nodriver_auth(proxy)
try:
image_request = await cls.upload_image(session, cls._headers, image, image_name) if image else None
except Exception as e:
Expand Down Expand Up @@ -469,18 +462,25 @@ async def create_async_generator(
await asyncio.sleep(5)
continue
await raise_for_status(response)
async for chunk in cls.iter_messages_chunk(response.iter_lines(), session, conversation):
if return_conversation:
history_disabled = False
return_conversation = False
yield conversation
yield chunk
if return_conversation:
history_disabled = False
yield conversation
async for line in response.iter_lines():
async for chunk in cls.iter_messages_line(session, line, conversation):
yield chunk
if not history_disabled:
yield SynthesizeData(cls.__name__, {
"conversation_id": conversation.conversation_id,
"message_id": conversation.message_id,
"voice": "maple",
})
if auto_continue and conversation.finish_reason == "max_tokens":
conversation.finish_reason = None
action = "continue"
await asyncio.sleep(5)
else:
break
yield FinishReason(conversation.finish_reason)
if history_disabled and auto_continue:
await cls.delete_conversation(session, cls._headers, conversation.conversation_id)

Expand Down Expand Up @@ -541,10 +541,38 @@ async def iter_messages_line(cls, session: StreamSession, line: bytes, fields: C
if "error" in line and line.get("error"):
raise RuntimeError(line.get("error"))

@classmethod
async def synthesize(cls, params: dict) -> AsyncIterator[bytes]:
await cls.login()
async with StreamSession(
impersonate="chrome",
timeout=900
) as session:
async with session.get(
f"{cls.url}/backend-api/synthesize",
params=params,
headers=cls._headers
) as response:
await raise_for_status(response)
async for chunk in response.iter_content():
yield chunk

@classmethod
async def login(cls, proxy: str = None):
if cls._expires is not None and cls._expires < time.time():
cls._headers = cls._api_key = None
try:
await get_request_config(proxy)
cls._create_request_args(RequestConfig.cookies, RequestConfig.headers)
cls._set_api_key(RequestConfig.access_token)
except NoValidHarFileError:
if has_nodriver:
await cls.nodriver_auth(proxy)
else:
raise

@classmethod
async def nodriver_auth(cls, proxy: str = None):
if not has_nodriver:
return
if has_platformdirs:
user_data_dir = user_config_dir("g4f-nodriver")
else:
Expand Down
6 changes: 5 additions & 1 deletion g4f/client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from ..image import ImageResponse, copy_images, images_dir
from ..typing import Messages, Image, ImageType
from ..providers.types import ProviderType
from ..providers.response import ResponseType, FinishReason, BaseConversation
from ..providers.response import ResponseType, FinishReason, BaseConversation, SynthesizeData
from ..errors import NoImageResponseError, ModelNotFoundError
from ..providers.retry_provider import IterListProvider
from ..providers.base_provider import get_running_loop
Expand Down Expand Up @@ -60,6 +60,8 @@ def iter_response(
elif isinstance(chunk, BaseConversation):
yield chunk
continue
elif isinstance(chunk, SynthesizeData):
continue

chunk = str(chunk)
content += chunk
Expand Down Expand Up @@ -121,6 +123,8 @@ async def async_iter_response(
elif isinstance(chunk, BaseConversation):
yield chunk
continue
elif isinstance(chunk, SynthesizeData):
continue

chunk = str(chunk)
content += chunk
Expand Down
24 changes: 20 additions & 4 deletions g4f/gui/client/static/css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,6 @@ body {
flex-direction: column;
gap: var(--section-gap);
padding: var(--inner-gap) var(--section-gap);
padding-bottom: 0;
}

.message.print {
Expand All @@ -271,7 +270,11 @@ body {
}

.message.regenerate {
opacity: 0.75;
background-color: var(--colour-6);
}

.white .message.regenerate {
background-color: var(--colour-4);
}

.message:last-child {
Expand Down Expand Up @@ -407,6 +410,7 @@ body {

.message .count .fa-clipboard.clicked,
.message .count .fa-print.clicked,
.message .count .fa-rotate.clicked,
.message .count .fa-volume-high.active {
color: var(--accent);
}
Expand All @@ -430,6 +434,15 @@ body {
font-size: 12px;
}

.message audio {
display: none;
max-width: 400px;
}

.message audio.show {
display: block;
}

.count_total {
font-size: 12px;
padding-left: 25px;
Expand Down Expand Up @@ -1159,7 +1172,10 @@ a:-webkit-any-link {
.message .user {
display: none;
}
.message.regenerate {
opacity: 1;
body {
height: auto;
}
.box {
backdrop-filter: none;
}
}
Loading

0 comments on commit eae317a

Please sign in to comment.