Merge pull request #2876 from hlohaus/30Mar

Fix generating images. Add headers / styling from @kqlio67 in UI
This commit is contained in:
H Lohaus 2025-04-01 19:34:20 +02:00 committed by GitHub
commit b220056af7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
15 changed files with 214 additions and 96 deletions

View file

@ -187,7 +187,7 @@ class ARTA(AsyncGeneratorProvider, ProviderModelMixin):
image_urls = [image["url"] for image in status_data.get("response", [])]
duration = time.time() - start_time
yield Reasoning(label="Generated", status=f"{n} image(s) in {duration:.2f}s")
yield ImageResponse(images=image_urls, alt=prompt)
yield ImageResponse(urls=image_urls, alt=prompt)
return
elif status in ("IN_QUEUE", "IN_PROGRESS"):
if last_status != status:

View file

@ -639,7 +639,7 @@ class Blackbox(AsyncGeneratorProvider, ProviderModelMixin):
image_url_match = re.search(r'!\[.*?\]\((.*?)\)', full_response_text)
if image_url_match:
image_url = image_url_match.group(1)
yield ImageResponse(images=[image_url], alt=format_image_prompt(messages, prompt))
yield ImageResponse(urls=[image_url], alt=format_image_prompt(messages, prompt))
return
# Handle conversation history once, in one place

View file

@ -78,7 +78,7 @@ class ImageLabs(AsyncGeneratorProvider, ProviderModelMixin):
if progress_data.get('status') == 'Done' or progress_data.get('final_image_url'):
# Yield ImageResponse with the final image URL
yield ImageResponse(
images=[progress_data.get('final_image_url')],
urls=[progress_data.get('final_image_url')],
alt=prompt
)
break

View file

@ -12,7 +12,7 @@ from .helper import filter_none, format_image_prompt
from .base_provider import AsyncGeneratorProvider, ProviderModelMixin
from ..typing import AsyncResult, Messages, MediaListType
from ..image import is_data_an_audio
from ..errors import ModelNotFoundError
from ..errors import ModelNotFoundError, ResponseError
from ..requests.raise_for_status import raise_for_status
from ..requests.aiohttp import get_connector
from ..image.copy_images import save_response_media
@ -279,7 +279,7 @@ class PollinationsAI(AsyncGeneratorProvider, ProviderModelMixin):
**kwargs
) -> AsyncResult:
if not cache and seed is None:
seed = random.randint(9999, 99999999)
seed = random.randint(0, 2**32)
json_mode = False
if response_format and response_format.get("type") == "json_object":
json_mode = True
@ -318,31 +318,30 @@ class PollinationsAI(AsyncGeneratorProvider, ProviderModelMixin):
if line[6:].startswith(b"[DONE]"):
break
result = json.loads(line[6:])
if "usage" in result:
yield Usage(**result["usage"])
choices = result.get("choices", [{}])
choice = choices.pop() if choices else {}
content = choice.get("delta", {}).get("content")
if content:
yield content
if "usage" in result:
yield Usage(**result["usage"])
finish_reason = choice.get("finish_reason")
if finish_reason:
yield FinishReason(finish_reason)
return
result = await response.json()
choice = result["choices"][0]
message = choice.get("message", {})
content = message.get("content", "")
if "tool_calls" in message:
yield ToolCalls(message["tool_calls"])
if content:
yield content
if "choices" in result:
choice = result["choices"][0]
message = choice.get("message", {})
content = message.get("content", "")
if content:
yield content
if "tool_calls" in message:
yield ToolCalls(message["tool_calls"])
else:
raise ResponseError(result)
if "usage" in result:
yield Usage(**result["usage"])
finish_reason = choice.get("finish_reason")
if finish_reason:
yield FinishReason(finish_reason)

View file

@ -124,7 +124,7 @@ class Websim(AsyncGeneratorProvider, ProviderModelMixin):
response_json = json.loads(response_text)
image_url = response_json.get("url")
if image_url:
yield ImageResponse(images=[image_url], alt=used_prompt)
yield ImageResponse(urls=[image_url], alt=used_prompt)
@classmethod
async def _handle_chat_request(

View file

@ -71,5 +71,5 @@ class Voodoohop_Flux1Schnell(AsyncGeneratorProvider, ProviderModelMixin):
elif event_type == b'complete':
json_data = json.loads(data)
image_url = json_data[0]['url']
yield ImageResponse(images=[image_url], alt=prompt)
yield ImageResponse(urls=[image_url], alt=prompt)
return

View file

@ -83,7 +83,6 @@ class Grok(AsyncAuthedProvider, ProviderModelMixin):
model: str,
messages: Messages,
auth_result: AuthResult,
cookies: Cookies = None,
return_conversation: bool = False,
conversation: Conversation = None,
**kwargs
@ -130,7 +129,7 @@ class Grok(AsyncAuthedProvider, ProviderModelMixin):
yield token
generated_images = response_data.get("modelResponse", {}).get("generatedImageUrls", None)
if generated_images:
yield ImageResponse([f'{cls.assets_url}/{image}' for image in generated_images], "", {"cookies": cookies, "headers": headers})
yield ImageResponse([f'{cls.assets_url}/{image}' for image in generated_images], "", {"cookies": auth_result.cookies, "headers": auth_result.headers})
title = result.get("title", {}).get("newTitle", "")
if title:
yield TitleGeneration(title)

View file

@ -74,7 +74,7 @@ class AiChats(AsyncGeneratorProvider, ProviderModelMixin):
base64_image = base64.b64encode(image_data).decode('utf-8')
base64_url = f"data:image/png;base64,{base64_image}"
yield ImageResponse(base64_url, prompt)
yield ImageResponse(image_url, prompt)
else:
yield f"Error: No image URL found in the response. Full response: {response_json}"
else:

View file

@ -170,7 +170,7 @@ class Airforce(AsyncGeneratorProvider, ProviderModelMixin):
async with session.get(cls.api_endpoint_imagine2, params=params, proxy=proxy) as response:
if response.status == 200:
image_url = str(response.url)
yield ImageResponse(images=image_url, alt=prompt)
yield ImageResponse(urls=image_url, alt=prompt)
else:
error_text = await response.text()
raise RuntimeError(f"Image generation failed: {response.status} - {error_text}")

View file

@ -77,7 +77,7 @@ def get_cookies(domain_name: str, raise_requirements_error: bool = True, single_
Returns:
Dict[str, str]: A dictionary of cookie names and values.
"""
if cache_result and domain_name in CookiesConfig.cookies:
if domain_name in CookiesConfig.cookies:
return CookiesConfig.cookies[domain_name]
cookies = load_cookies_from_browsers(domain_name, raise_requirements_error, single_browser)

View file

@ -75,6 +75,12 @@
</script>
<div class="gradient"></div>
<div class="sidebar shown">
<div class="sidebar-header">
<div class="sidebar-logo">G4F Chat</div>
<div class="mobile-sidebar-toggle">
<i class="fa-solid fa-bars"></i>
</div>
</div>
<div class="top">
<button class="new_convo" onclick="new_conversation()">
<i class="fa-regular fa-plus"></i>
@ -104,8 +110,13 @@
</div>
<div class="settings hidden">
<div class="paper">
<h3>Settings</h3>
<div class="field">
<div class="settings-top-bar">
<button class="settings-back-button" onclick="open_settings();">
<i class="fa-solid fa-arrow-left"></i>
</button>
<span>Settings</span>
</div>
<div class="field">
<span class="label">Enable Dark Mode</span>
<input type="checkbox" id="darkMode" checked />
<label for="darkMode" class="toogle" title=""></label>
@ -221,8 +232,14 @@
</div>
</div>
<div class="chat-container">
<div class="chat-header box">
G4F Chat
<div class="chat-top-panel">
<div class="mobile-sidebar-toggle">
<i class="fa-solid fa-bars"></i>
</div>
<div class="convo-title">New Conversation</div>
<button class="new_convo_icon" onclick="new_conversation()">
<i class="fa-regular fa-plus"></i>
</button>
</div>
<textarea id="chatPrompt" class="box" placeholder="System prompt"></textarea>
<button class="slide-header">
@ -337,9 +354,13 @@
</div>
</div>
</div>
<div class="log hidden"></div>
<div class="mobile-sidebar">
<i class="fa-solid fa-bars"></i>
</div>
<div class="log hidden">
<div class="log-top-bar">
<button class="log-back-button" onclick="open_settings();">
<i class="fa-solid fa-arrow-left"></i>
</button>
<span>Logs</span>
</div>
</div>
</body>
</html>

View file

@ -57,15 +57,14 @@ html,
body {
scroll-behavior: smooth;
overflow: hidden;
max-height: 100%;
max-width: 100%;
}
body {
background: var(--background);
color: var(--colour-3);
height: 100vh;
display: flex;
height: 100vh;
max-height: 100vh;
}
body:not(.white) a:link,
@ -936,7 +935,7 @@ input.model:hover
.slide-header {
position: absolute;
top: 0;
top: 58px;
z-index: 1;
padding: 10px;
border: none;
@ -1101,26 +1100,35 @@ ul {
visibility: hidden;
}
.mobile-sidebar {
position: fixed;
z-index: 1000;
top: 10px;
right: 10px;
width: 40px;
height: 40px;
background-color: var(--blur-bg);
border: 1px solid var(--blur-border);
border-radius: 10px;
backdrop-filter: blur(20px);
-webkit-backdrop-filter: blur(20px);
cursor: pointer;
justify-content: center;
.sidebar-header {
padding: 12px 0;
margin-bottom: 12px;
display: flex;
justify-content: space-between;
align-items: center;
transition: all 0.3s ease;
padding: 10px 12px;
padding: 0 0 10px 0;
border-bottom: 1px solid var(--blur-border);
}
.mobile-sidebar i {
.sidebar-logo {
font-size: 16px;
font-weight: 500;
}
.mobile-sidebar-toggle {
display: flex;
justify-content: center;
align-items: center;
width: 36px;
height: 36px;
background-color: var(--blur-bg);
border: 1px solid var(--blur-border);
border-radius: 8px;
cursor: pointer;
transition: all 0.3s ease;
}
.mobile-sidebar-toggle i {
transition: 0.33s;
}
@ -1308,6 +1316,7 @@ form textarea {
.field a {
text-wrap: nowrap;
padding: 4px;
}
.field .fa-xmark {
@ -1328,7 +1337,9 @@ form textarea {
.settings, .log, form {
overflow: auto;
width: 100%;
display: flex;
flex-direction: column;
flex: 1;
}
.log {
@ -1508,13 +1519,59 @@ form textarea {
flex-direction: column;
overflow: hidden;
}
.chat-header {
padding: 10px 28px;
font-weight: 500;
white-space: nowrap;
text-overflow: ellipsis;
overflow: hidden;
.chat-top-panel {
padding: 12px 15px;
display: flex;
justify-content: space-between;
align-items: center;
}
.chat-logo {
font-size: 16px;
}
.chat-top-panel .convo-title {
margin: 0 10px;
font-size: 14px;
text-align: center;
flex: 1;
}
.chat-top-panel .mobile-sidebar-toggle {
display: flex;
justify-content: center;
align-items: center;
width: 36px;
height: 36px;
background-color: var(--blur-bg);
border: 1px solid var(--blur-border);
border-radius: 8px;
cursor: pointer;
}
@media only screen and (min-width: 40em) {
.chat-top-panel .mobile-sidebar-toggle {
display: none;
}
}
.chat-top-panel .new_convo_icon {
display: flex;
justify-content: center;
align-items: center;
width: 36px;
height: 36px;
background-color: var(--blur-bg);
border: 1px solid var(--blur-border);
border-radius: 8px;
cursor: pointer;
color: var(--colour-3);
font-size: 16px;
}
.chat-top-panel .mobile-sidebar-toggle:hover,
.chat-top-panel .new_convo_icon:hover {
background-color: var(--button-hover);
}
@media only screen and (min-width: 40em) {
.sidebar {
width: 300px;
@ -1564,4 +1621,44 @@ form textarea {
border-style: solid;
box-shadow: none;
background-color: var(--button-hover);
}
.settings-top-bar, .log-top-bar {
display: flex;
align-items: center;
padding: 15px 0;
margin-bottom: 15px;
border-bottom: 1px solid var(--blur-border);
gap: 12px; /* Space between button and title */
}
.settings-back-button, .log-back-button {
display: flex;
align-items: center;
justify-content: center;
background: none;
border: none;
color: var(--colour-3);
font-size: 18px;
cursor: pointer;
width: 40px;
height: 40px;
border-radius: 8px;
}
.settings-back-button:hover, .log-back-button:hover {
background-color: var(--blur-border);
}
.settings-title, .log-title {
font-size: 20px;
font-weight: 500;
color: var(--colour-3);
user-select: none; /* Prevent text selection */
}
.settings-top-bar span, .log-top-bar span {
font-weight: 600;
font-size: 18px;
margin-left: 8px;
}

View file

@ -5,7 +5,7 @@ const box_conversations = document.querySelector(`.top`);
const stop_generating = document.querySelector(`.stop_generating`);
const regenerate_button = document.querySelector(`.regenerate`);
const sidebar = document.querySelector(".sidebar");
const sidebar_button = document.querySelector(".mobile-sidebar");
const sidebar_buttons = document.querySelectorAll(".mobile-sidebar-toggle");
const sendButton = document.getElementById("sendButton");
const addButton = document.getElementById("addButton");
const imageInput = document.querySelector(".image-label");
@ -570,6 +570,7 @@ const handle_ask = async (do_ask_gpt = true, message = null) => {
}
} else {
await safe_load_conversation(window.conversation_id, true);
await load_conversations();
}
};
@ -1360,10 +1361,12 @@ const set_conversation = async (conversation_id) => {
};
const new_conversation = async () => {
history.pushState({}, null, `/chat/`);
if (!/\/chat\/(share|\?|$)/.test(window.location.href)) {
history.pushState({}, null, `/chat/`);
}
window.conversation_id = generateUUID();
document.title = window.title || document.title;
document.querySelector(".chat-header").innerText = "New Conversation - G4F";
document.querySelector(".chat-top-panel .convo-title").innerText = "New Conversation";
await clear_conversation();
if (chatPrompt) {
@ -1429,16 +1432,16 @@ const load_conversation = async (conversation, scroll=true) => {
let messages = conversation?.items || [];
console.debug("Conversation:", conversation.id)
let title = conversation.new_title || conversation.title;
title = title ? `${title} - G4F` : window.title;
let conversation_title = conversation.new_title || conversation.title;
title = conversation_title ? `${conversation_title} - G4F` : window.title;
if (title) {
document.title = title;
}
const chatHeader = document.querySelector(".chat-header");
const chatHeader = document.querySelector(".chat-top-panel .convo-title");
if (window.share_id && conversation.id == window.start_id) {
chatHeader.innerHTML = '<i class="fa-solid fa-qrcode"></i> ' + escapeHtml(title);
chatHeader.innerHTML = '<i class="fa-solid fa-qrcode"></i> ' + escapeHtml(conversation_title);
} else {
chatHeader.innerText = title;
chatHeader.innerText = conversation_title;
}
if (chatPrompt) {
@ -1860,7 +1863,7 @@ async function hide_sidebar(remove_shown=false) {
if (remove_shown) {
sidebar.classList.remove("shown");
}
sidebar_button.classList.remove("rotated");
sidebar_buttons.forEach((el)=>el.classList.remove("rotated"))
settings.classList.add("hidden");
chat.classList.remove("hidden");
log_storage.classList.add("hidden");
@ -1878,18 +1881,16 @@ async function hide_settings() {
window.addEventListener('popstate', hide_sidebar, false);
sidebar_button.addEventListener("click", async () => {
if (sidebar.classList.contains("shown") || sidebar_button.classList.contains("rotated")) {
await hide_sidebar();
sidebar_buttons.forEach((el)=>el.addEventListener("click", async () => {
if (sidebar.classList.contains("shown") || el.classList.contains("rotated")) {
await hide_sidebar(true);
chat.classList.remove("hidden");
sidebar.classList.remove("shown");
sidebar_button.classList.remove("rotated");
} else {
await show_menu();
chat.classList.add("hidden");
}
window.scrollTo(0, 0);
});
}));
function add_url_to_history(url) {
if (!window?.pywebview) {
@ -1899,7 +1900,7 @@ function add_url_to_history(url) {
async function show_menu() {
sidebar.classList.add("shown");
sidebar_button.classList.add("rotated");
sidebar_buttons.forEach((el)=>el.classList.add("rotated"))
await hide_settings();
add_url_to_history("/chat/menu/");
}
@ -2137,7 +2138,7 @@ window.addEventListener('load', async function() {
if (!appStorage.getItem(`conversation:${window.conversation_id}`) || conversation.id == window.conversation_id) {
// Copy conversation from share
if (conversation.id != window.conversation_id) {
conversation.id = window.conversation_id;
window.conversation_id = conversation.id;
conversation.updated = Date.now();
window.share_id = null;
}
@ -2210,7 +2211,8 @@ async function on_load() {
count_input();
if (/\/settings\//.test(window.location.href)) {
open_settings();
} else if (/\/chat\/share/.test(window.location.href)) {
} else if (/\/chat\/(share|\?|$)/.test(window.location.href)) {
chatPrompt.value = document.getElementById("systemPrompt")?.value || "";
chatPrompt.value = document.getElementById("systemPrompt")?.value || "";
let chat_url = new URL(window.location.href)
let chat_params = new URLSearchParams(chat_url.search);
@ -2218,11 +2220,9 @@ async function on_load() {
userInput.value = chat_params.get("prompt");
userInput.style.height = userInput.scrollHeight + "px";
userInput.focus();
//await handle_ask();
} else {
new_conversation();
}
} else if (/\/chat\/[?$]/.test(window.location.href)) {
chatPrompt.value = document.getElementById("systemPrompt")?.value || "";
say_hello();
} else {
//load_conversation(window.conversation_id);
}
@ -2475,8 +2475,7 @@ async function on_api() {
});
document.querySelector(".slide-header")?.addEventListener("click", () => {
const checked = slide_systemPrompt_icon.classList.contains("fa-angles-up");
document.querySelector(".chat-header").classList[checked ? "add": "remove"]("hidden");
chatPrompt.classList[checked || hide_systemPrompt.checked ? "add": "remove"]("hidden");
chatPrompt.classList[checked ? "add": "remove"]("hidden");
slide_systemPrompt_icon.classList[checked ? "remove": "add"]("fa-angles-up");
slide_systemPrompt_icon.classList[checked ? "add": "remove"]("fa-angles-down");
});

View file

@ -18,6 +18,7 @@ from hashlib import sha256
from ...client.service import convert_to_provider
from ...providers.asyncio import to_sync_generator
from ...providers.response import FinishReason
from ...client.helper import filter_markdown
from ...tools.files import supports_filename, get_streaming, get_bucket_dir, get_buckets
from ...tools.run_tools import iter_run_tools
@ -79,16 +80,12 @@ class Backend_Api(Api):
@app.route('/backend-api/v2/models', methods=['GET'])
def jsonify_models(**kwargs):
response = get_demo_models() if app.demo else self.get_models(**kwargs)
if isinstance(response, list):
return jsonify(response)
return response
return jsonify(response)
@app.route('/backend-api/v2/models/<provider>', methods=['GET'])
def jsonify_provider_models(**kwargs):
response = self.get_provider_models(**kwargs)
if isinstance(response, list):
return jsonify(response)
return response
return jsonify(response)
@app.route('/backend-api/v2/providers', methods=['GET'])
def jsonify_providers(**kwargs):
@ -259,9 +256,11 @@ class Backend_Api(Api):
else:
response = iter_run_tools(ChatCompletion.create, **parameters)
cache_dir.mkdir(parents=True, exist_ok=True)
copy_response = [chunk for chunk in response]
with cache_file.open("w") as f:
for chunk in response:
for chunk in copy_response:
f.write(str(chunk))
response = copy_response
else:
response = iter_run_tools(ChatCompletion.create, **parameters)
@ -269,7 +268,9 @@ class Backend_Api(Api):
return Response(filter_markdown("".join([str(chunk) for chunk in response]), do_filter_markdown), mimetype='text/plain')
def cast_str():
for chunk in response:
if not isinstance(chunk, Exception):
if isinstance(chunk, FinishReason):
yield f"[{chunk.reason}]" if chunk.reason != "stop" else ""
elif not isinstance(chunk, Exception):
yield str(chunk)
return Response(cast_str(), mimetype='text/plain')
except Exception as e:

View file

@ -424,7 +424,10 @@ class AsyncAuthedProvider(AsyncGeneratorProvider, AuthFileMixin):
def write_cache_file(cls, cache_file: Path, auth_result: AuthResult = None):
if auth_result is not None:
cache_file.parent.mkdir(parents=True, exist_ok=True)
cache_file.write_text(json.dumps(auth_result.get_dict()))
try:
cache_file.write_text(json.dumps(auth_result.get_dict()))
except TypeError:
raise RuntimeError(f"Failed to save: {auth_result.get_dict()}")
elif cache_file.exists():
cache_file.unlink()
@ -489,5 +492,4 @@ class AsyncAuthedProvider(AsyncGeneratorProvider, AuthFileMixin):
cache_file = None
yield chunk
finally:
if cache_file is not None:
cls.write_cache_file(cache_file, auth_result)
cls.write_cache_file(cache_file, auth_result)