Improve code style

best
Heiner Lohaus 6 months ago
parent 25895eb637
commit 139f68af4f

@ -35,9 +35,9 @@ async def delete_conversation(session: ClientSession, conversation: Conversation
"source": "cib", "source": "cib",
"optionsSets": ["autosave"] "optionsSets": ["autosave"]
} }
async with session.post(url, json=json, proxy=proxy) as response: try:
try: async with session.post(url, json=json, proxy=proxy) as response:
response = await response.json() response = await response.json()
return response["result"]["value"] == "Success" return response["result"]["value"] == "Success"
except: except:
return False return False

@ -1,5 +1,3 @@
import asyncio import asyncio
import time, json, os import time, json, os
from aiohttp import ClientSession from aiohttp import ClientSession
@ -9,10 +7,12 @@ from typing import Generator
from ...webdriver import WebDriver, get_driver_cookies, get_browser from ...webdriver import WebDriver, get_driver_cookies, get_browser
from ...Provider.helper import get_event_loop from ...Provider.helper import get_event_loop
from ...base_provider import ProviderType
from ...Provider.create_images import CreateImagesProvider
BING_URL = "https://www.bing.com" BING_URL = "https://www.bing.com"
def wait_for_login(driver: WebDriver, timeout: int = 1200): def wait_for_login(driver: WebDriver, timeout: int = 1200) -> Generator:
driver.get(f"{BING_URL}/") driver.get(f"{BING_URL}/")
value = driver.get_cookie("_U") value = driver.get_cookie("_U")
if value: if value:
@ -29,7 +29,7 @@ def wait_for_login(driver: WebDriver, timeout: int = 1200):
return return
time.sleep(0.1) time.sleep(0.1)
def create_session(cookies: dict): def create_session(cookies: dict) -> ClientSession:
headers = { headers = {
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7", "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
"accept-encoding": "gzip, deflate, br", "accept-encoding": "gzip, deflate, br",
@ -51,7 +51,7 @@ def create_session(cookies: dict):
headers["cookie"] = "; ".join(f"{k}={v}" for k, v in cookies.items()) headers["cookie"] = "; ".join(f"{k}={v}" for k, v in cookies.items())
return ClientSession(headers=headers) return ClientSession(headers=headers)
async def create_images(session: ClientSession, prompt: str, proxy: str = None, timeout: int = 200): async def create_images(session: ClientSession, prompt: str, proxy: str = None, timeout: int = 200) -> list:
url_encoded_prompt = quote(prompt) url_encoded_prompt = quote(prompt)
payload = f"q={url_encoded_prompt}&rt=4&FORM=GENCRE" payload = f"q={url_encoded_prompt}&rt=4&FORM=GENCRE"
url = f"{BING_URL}/images/create?q={url_encoded_prompt}&rt=4&FORM=GENCRE" url = f"{BING_URL}/images/create?q={url_encoded_prompt}&rt=4&FORM=GENCRE"
@ -111,7 +111,10 @@ async def create_images(session: ClientSession, prompt: str, proxy: str = None,
def format_images_markdown(images: list, prompt: str) -> str: def format_images_markdown(images: list, prompt: str) -> str:
images = [f"[![#{idx+1} {prompt}]({image}?w=200&h=200)]({image})" for idx, image in enumerate(images)] images = [f"[![#{idx+1} {prompt}]({image}?w=200&h=200)]({image})" for idx, image in enumerate(images)]
return f"\n\n<img data-prompt=\"{prompt}\">\n<!-- generated images start -->\n" + ("\n".join(images)) + "\n<!-- generated images end -->\n\n" images = "\n".join(images)
start_flag = "<!-- generated images start -->\n"
end_flag = "<!-- generated images end -->\n"
return f"\n\n<img data-prompt=\"{prompt}\">\n{start_flag}{images}\n{end_flag}\n"
def get_images(text: str) -> list: def get_images(text: str) -> list:
html_soup = BeautifulSoup(text, "html.parser") html_soup = BeautifulSoup(text, "html.parser")
@ -143,4 +146,7 @@ def create_completion(prompt: str, proxy: str = None) -> Generator:
images = loop.run_until_complete(run_session()) images = loop.run_until_complete(run_session())
yield format_images_markdown(images, prompt) yield format_images_markdown(images, prompt)
finally: finally:
driver.quit() driver.quit()
def patch_provider(provider: ProviderType) -> CreateImagesProvider:
return CreateImagesProvider(provider, create_completion)

@ -66,7 +66,7 @@ async def upload_image(
) )
return result return result
except Exception as e: except Exception as e:
raise RuntimeError(f"Add image failed: {e}") raise RuntimeError(f"Upload image failed: {e}")
def build_image_upload_api_payload(image_bin: str, tone: str): def build_image_upload_api_payload(image_bin: str, tone: str):
@ -101,82 +101,62 @@ def build_image_upload_api_payload(image_bin: str, tone: str):
return data, boundary return data, boundary
def is_data_uri_an_image(data_uri: str): def is_data_uri_an_image(data_uri: str):
try: # Check if the data URI starts with 'data:image' and contains an image format (e.g., jpeg, png, gif)
# Check if the data URI starts with 'data:image' and contains an image format (e.g., jpeg, png, gif) if not re.match(r'data:image/(\w+);base64,', data_uri):
if not re.match(r'data:image/(\w+);base64,', data_uri): raise ValueError("Invalid data URI image.")
raise ValueError("Invalid data URI image.") # Extract the image format from the data URI
# Extract the image format from the data URI image_format = re.match(r'data:image/(\w+);base64,', data_uri).group(1)
image_format = re.match(r'data:image/(\w+);base64,', data_uri).group(1) # Check if the image format is one of the allowed formats (jpg, jpeg, png, gif)
# Check if the image format is one of the allowed formats (jpg, jpeg, png, gif) if image_format.lower() not in ['jpeg', 'jpg', 'png', 'gif']:
if image_format.lower() not in ['jpeg', 'jpg', 'png', 'gif']: raise ValueError("Invalid image format (from mime file type).")
raise ValueError("Invalid image format (from mime file type).")
except Exception as e:
raise e
def is_accepted_format(binary_data: bytes) -> bool: def is_accepted_format(binary_data: bytes) -> bool:
try: if binary_data.startswith(b'\xFF\xD8\xFF'):
check = False pass # It's a JPEG image
if binary_data.startswith(b'\xFF\xD8\xFF'): elif binary_data.startswith(b'\x89PNG\r\n\x1a\n'):
check = True # It's a JPEG image pass # It's a PNG image
elif binary_data.startswith(b'\x89PNG\r\n\x1a\n'): elif binary_data.startswith(b'GIF87a') or binary_data.startswith(b'GIF89a'):
check = True # It's a PNG image pass # It's a GIF image
elif binary_data.startswith(b'GIF87a') or binary_data.startswith(b'GIF89a'): elif binary_data.startswith(b'\x89JFIF') or binary_data.startswith(b'JFIF\x00'):
check = True # It's a GIF image pass # It's a JPEG image
elif binary_data.startswith(b'\x89JFIF') or binary_data.startswith(b'JFIF\x00'): elif binary_data.startswith(b'\xFF\xD8'):
check = True # It's a JPEG image pass # It's a JPEG image
elif binary_data.startswith(b'\xFF\xD8'): elif binary_data.startswith(b'RIFF') and binary_data[8:12] == b'WEBP':
check = True # It's a JPEG image pass # It's a WebP image
elif binary_data.startswith(b'RIFF') and binary_data[8:12] == b'WEBP': else:
check = True # It's a WebP image raise ValueError("Invalid image format (from magic code).")
# else we raise ValueError
if not check:
raise ValueError("Invalid image format (from magic code).")
except Exception as e:
raise e
def extract_data_uri(data_uri: str) -> bytes: def extract_data_uri(data_uri: str) -> bytes:
try: data = data_uri.split(",")[1]
data = data_uri.split(",")[1] data = base64.b64decode(data)
data = base64.b64decode(data) return data
return data
except Exception as e:
raise e
def get_orientation(data: bytes) -> int: def get_orientation(data: bytes) -> int:
try: if data[:2] != b'\xFF\xD8':
if data[:2] != b'\xFF\xD8': raise Exception('NotJpeg')
raise Exception('NotJpeg') with Image.open(data) as img:
with Image.open(data) as img: exif_data = img._getexif()
exif_data = img._getexif() if exif_data is not None:
if exif_data is not None: orientation = exif_data.get(274) # 274 corresponds to the orientation tag in EXIF
orientation = exif_data.get(274) # 274 corresponds to the orientation tag in EXIF if orientation is not None:
if orientation is not None: return orientation
return orientation
except Exception:
pass
def process_image(orientation: int, img: Image.Image, new_width: int, new_height: int) -> Image.Image: def process_image(orientation: int, img: Image.Image, new_width: int, new_height: int) -> Image.Image:
try: # Initialize the canvas
# Initialize the canvas new_img = Image.new("RGB", (new_width, new_height), color="#FFFFFF")
new_img = Image.new("RGB", (new_width, new_height), color="#FFFFFF") if orientation:
if orientation: if orientation > 4:
if orientation > 4: img = img.transpose(Image.FLIP_LEFT_RIGHT)
img = img.transpose(Image.FLIP_LEFT_RIGHT) if orientation in [3, 4]:
if orientation in [3, 4]: img = img.transpose(Image.ROTATE_180)
img = img.transpose(Image.ROTATE_180) if orientation in [5, 6]:
if orientation in [5, 6]: img = img.transpose(Image.ROTATE_270)
img = img.transpose(Image.ROTATE_270) if orientation in [7, 8]:
if orientation in [7, 8]: img = img.transpose(Image.ROTATE_90)
img = img.transpose(Image.ROTATE_90) new_img.paste(img, (0, 0))
new_img.paste(img, (0, 0)) return new_img
return new_img
except Exception as e:
raise e
def compress_image_to_base64(img, compression_rate) -> str: def compress_image_to_base64(image: Image.Image, compression_rate: float) -> str:
try: output_buffer = io.BytesIO()
output_buffer = io.BytesIO() image.save(output_buffer, format="JPEG", quality=int(compression_rate * 100))
img.save(output_buffer, format="JPEG", quality=int(compression_rate * 100)) return base64.b64encode(output_buffer.getvalue()).decode('utf-8')
return base64.b64encode(output_buffer.getvalue()).decode('utf-8')
except Exception as e:
raise e

@ -1,19 +1,14 @@
import logging import logging
import g4f import json
from flask import request, Flask
from g4f import debug, version, models
from g4f import _all_models, get_last_provider, ChatCompletion
from g4f.Provider import __providers__ from g4f.Provider import __providers__
from g4f.Provider.bing.create_images import patch_provider
from .internet import get_search_message
import json
from flask import request, Flask
from .internet import get_search_message
from g4f import debug, version
from g4f.base_provider import ProviderType
debug.logging = True debug.logging = True
def patch_provider(provider: ProviderType):
from g4f.Provider import CreateImagesProvider
from g4f.Provider.bing.create_images import create_completion
return CreateImagesProvider(provider, create_completion)
class Backend_Api: class Backend_Api:
def __init__(self, app: Flask) -> None: def __init__(self, app: Flask) -> None:
self.app: Flask = app self.app: Flask = app
@ -50,7 +45,7 @@ class Backend_Api:
return 'ok', 200 return 'ok', 200
def models(self): def models(self):
return g4f._all_models return _all_models
def providers(self): def providers(self):
return [ return [
@ -74,7 +69,7 @@ class Backend_Api:
if request.json.get('internet_access'): if request.json.get('internet_access'):
messages[-1]["content"] = get_search_message(messages[-1]["content"]) messages[-1]["content"] = get_search_message(messages[-1]["content"])
model = request.json.get('model') model = request.json.get('model')
model = model if model else g4f.models.default model = model if model else models.default
provider = request.json.get('provider', '').replace('g4f.Provider.', '') provider = request.json.get('provider', '').replace('g4f.Provider.', '')
provider = provider if provider and provider != "Auto" else None provider = provider if provider and provider != "Auto" else None
patch = patch_provider if request.json.get('patch_provider') else None patch = patch_provider if request.json.get('patch_provider') else None
@ -82,7 +77,7 @@ class Backend_Api:
def try_response(): def try_response():
try: try:
first = True first = True
for chunk in g4f.ChatCompletion.create( for chunk in ChatCompletion.create(
model=model, model=model,
provider=provider, provider=provider,
messages=messages, messages=messages,
@ -94,7 +89,7 @@ class Backend_Api:
first = False first = False
yield json.dumps({ yield json.dumps({
'type' : 'provider', 'type' : 'provider',
'provider': g4f.get_last_provider(True) 'provider': get_last_provider(True)
}) + "\n" }) + "\n"
yield json.dumps({ yield json.dumps({
'type' : 'content', 'type' : 'content',

@ -145,5 +145,5 @@ User request:
""" """
return message return message
except Exception as e: except Exception as e:
print("Couldn't search DuckDuckGo:", e) print("Couldn't do web search:", e)
return prompt return prompt

Loading…
Cancel
Save