Skip to content

Commit

Permalink
Merge pull request #162 from Integration-Automation/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
JE-Chen authored Jul 17, 2024
2 parents 32f5ba9 + 93ac34a commit 3cb17b0
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 70 deletions.
2 changes: 1 addition & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ CODE_OF_CONDUCT.md
CONTRIBUTING.md
dev.toml
pyproject.toml
stable.toml
pyproject.toml
requirements.txt
9 changes: 6 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM ubuntu:latest
FROM ubuntu:24.10
LABEL authors="JE-Chen"
ARG DEBIAN_FRONTEND=noninteractive
# Copy
Expand All @@ -16,9 +16,12 @@ COPY /Dockerfiles/Flask/main_flask.py /ReEdgeGPT_Flask
# Workdir
WORKDIR /ReEdgeGPT_Flask
# Install dependency
RUN apt update -y && apt upgrade -y && \
RUN apt update -y && \
apt upgrade -y && \
apt-get install -y python3 python3-pip python3-venv && \
python3 -m venv venv && . venv/bin/activate && \
apt-get clean && \
python3 -m venv venv && \
. venv/bin/activate && \
pip install -r docker-requirements.txt
# Os path
ENV PATH="/venv/bin:$PATH"
Expand Down
2 changes: 0 additions & 2 deletions Dockerfiles/Flask/re_edge_gpt_blueprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,6 @@ async def chat():
message_type=message_type
)
return jsonify(response)
except Exception as error:
raise error
finally:
if chatbot is not None:
await chatbot.close()
Expand Down
2 changes: 1 addition & 1 deletion dev.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "re_edge_gpt_dev"
version = "0.0.52"
version = "0.0.54"
authors = [
{ name = "JE-Chen", email = "jechenmailman@gmail.com" },
]
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "re_edge_gpt"
version = "0.0.44"
version = "0.0.45"
authors = [
{ name = "JE-Chen", email = "jechenmailman@gmail.com" },
]
Expand Down
13 changes: 11 additions & 2 deletions re_edge_gpt/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
from prompt_toolkit.completion import WordCompleter
from prompt_toolkit.history import InMemoryHistory
from prompt_toolkit.key_binding import KeyBindings
from rich.live import Live
from rich.markdown import Markdown

from re_edge_gpt.chat.re_edge_gpt import Chatbot

Expand Down Expand Up @@ -67,6 +65,17 @@ async def get_input_async(
async def async_main(args: argparse.Namespace) -> None:
"""
Main function
options:
enter-once
search-result
no-stream
rich
proxy: Proxy service
wss-link: websocket link to connect to ChatHub
style: Copilot Chat style
prompt: Chat prompt
cookie-file: cookie file path
locale: locale ISO Language Codes
"""
print("Initializing...")
print("Enter `alt+enter` or `escape+enter` to send a message")
Expand Down
58 changes: 8 additions & 50 deletions re_edge_gpt/image/image_genearation.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@
import random
import sys
import time
from functools import partial
from typing import Dict
from typing import List
from typing import Union

import httpx
import regex
import requests
from requests.utils import requote_uri

from re_edge_gpt.chat.proxy import get_proxy
from re_edge_gpt.utils.exception.exception_message import sending_message, error_being_reviewed_prompt, \
error_blocked_prompt, \
error_unsupported_lang, error_timeout, error_noresults, error_no_images, download_message, error_image_create_failed
from re_edge_gpt.utils.exception.exceptions import UnSupportLanguage, PromptBlocked, ImageCreateFailed, NoResultsFound, \
AuthCookieError, LimitExceeded, InappropriateContentType, ResponseError
from re_edge_gpt.utils.exception.exceptions import UnSupportLanguage, PromptBlocked, ImageCreateFailed, \
NoResultsFound, AuthCookieError, LimitExceeded, InappropriateContentType, ResponseError

FORWARDED_IP = f"1.0.0.{random.randint(0, 255)}"

Expand All @@ -42,33 +42,20 @@
}


def debug(debug_file, text_var):
"""helper function for debug"""
with open(f"{debug_file}", "a", encoding="utf-8") as f:
f.write(str(text_var))
f.write("\n")


def check_response(response, debug_file: Union[str, None] = None):
def check_response(response):
# check for content waring message
if "this prompt is being reviewed" in response.text.lower():
if debug_file:
debug(f"ERROR: {error_being_reviewed_prompt}")
raise UnSupportLanguage(
error_being_reviewed_prompt,
)
if "this prompt has been blocked" in response.text.lower():
if debug_file:
debug(f"ERROR: {error_blocked_prompt}")
raise PromptBlocked(
error_blocked_prompt,
)
if (
"we're working hard to offer image creator in more languages"
in response.text.lower()
):
if debug_file:
debug(f"ERROR: {error_unsupported_lang}")
raise UnSupportLanguage(error_unsupported_lang)


Expand All @@ -86,7 +73,6 @@ class ImageGen:
def __init__(
self,
auth_cookie: str,
debug_file: Union[str, None] = None,
quiet: bool = False,
all_cookies: List[Dict] = None,
proxy: str = None,
Expand All @@ -107,9 +93,6 @@ def __init__(
for cookie in all_cookies:
self.session.cookies.set(cookie["name"], cookie["value"])
self.quiet = quiet
self.debug_file = debug_file
if self.debug_file:
self.debug = partial(debug, self.debug_file)

def get_images(self, prompt: str, timeout: int = 200, max_generate_time_sec: int = 60) -> Union[list, None]:
"""
Expand All @@ -121,19 +104,16 @@ def get_images(self, prompt: str, timeout: int = 200, max_generate_time_sec: int
"""
if not self.quiet:
print(sending_message)
if self.debug_file:
self.debug(sending_message)
url_encoded_prompt = requests.utils.quote(prompt)
url_encoded_prompt = requote_uri(prompt)
payload = f"q={url_encoded_prompt}&qs=ds"
# https://www.bing.com/images/create?q=<PROMPT>&rt=3&FORM=GENCRE
url = f"{BING_URL}/images/create?q={url_encoded_prompt}&rt=4&FORM=GUH2CR"
response = self.session.post(
url,
allow_redirects=False,
data=payload,
timeout=timeout,
)
check_response(response, self.debug_file)
check_response(response)
if response.status_code != 302:
# if rt4 fails, try rt3
url = f"{BING_URL}/images/create?q={url_encoded_prompt}&rt=3&FORM=GUH2CR"
Expand All @@ -147,30 +127,23 @@ def get_images(self, prompt: str, timeout: int = 200, max_generate_time_sec: int
# https://www.bing.com/images/create/async/results/{ID}?q={PROMPT}
polling_url = f"{BING_URL}/images/create/async/results/{request_id}?q={url_encoded_prompt}"
# Poll for results
if self.debug_file:
self.debug("Polling and waiting for result")
if not self.quiet:
print("Waiting for results...")
start_wait = time.time()
time_sec = 0
while True:
if int(time.time() - start_wait) > 200:
if self.debug_file:
self.debug(f"ERROR: {error_timeout}")
raise TimeoutError(error_timeout)
if not self.quiet:
print(".", end="", flush=True)
response = self.session.get(polling_url)
if response.status_code != 200:
if self.debug_file:
self.debug(f"ERROR: {error_noresults}")
raise NoResultsFound(error_noresults)
if not response.text or response.text.find("errorMessage") != -1:
time.sleep(1)
time_sec = time_sec + 1
if time_sec >= max_generate_time_sec:
raise TimeoutError("Out of generate time")
continue
else:
break
# Use regex to search for src=""
Expand Down Expand Up @@ -208,8 +181,6 @@ def save_images(
file_name: str
download_count: int
"""
if self.debug_file:
self.debug(download_message)
if not self.quiet:
print(download_message)
with contextlib.suppress(FileExistsError):
Expand Down Expand Up @@ -256,7 +227,6 @@ class ImageGenAsync:
def __init__(
self,
auth_cookie: str = None,
debug_file: Union[str, None] = None,
quiet: bool = False,
all_cookies: List[Dict] = None,
proxy: str = None
Expand All @@ -277,9 +247,6 @@ def __init__(
{cookie["name"]: cookie["value"]},
)
self.quiet = quiet
self.debug_file = debug_file
if self.debug_file:
self.debug = partial(debug, self.debug_file)

async def __aenter__(self):
return self
Expand All @@ -297,19 +264,16 @@ async def get_images(self, prompt: str, timeout: int = 200, max_generate_time_se
"""
if not self.quiet:
print("Sending request...")
url_encoded_prompt = requests.utils.quote(prompt)
# https://www.bing.com/images/create?q=<PROMPT>&rt=3&FORM=GENCRE
url_encoded_prompt = requote_uri(prompt)
url = f"{BING_URL}/images/create?q={url_encoded_prompt}&rt=4&FORM=GUH2CR"
# payload = f"q={url_encoded_prompt}&qs=ds"
response = await self.session.post(
url,
follow_redirects=False,
data={"q": url_encoded_prompt, "qs": "ds"},
timeout=timeout
)
check_response(response, self.debug_file)
check_response(response)
if response.status_code != 302:
# if rt4 fails, try rt3
url = f"{BING_URL}/images/create?q={url_encoded_prompt}&rt=3&FORM=GUH2CR"
response = await self.session.post(
url,
Expand All @@ -322,7 +286,6 @@ async def get_images(self, prompt: str, timeout: int = 200, max_generate_time_se
redirect_url = response.headers["Location"].replace("&nfy=1", "")
request_id = redirect_url.split("id=")[-1]
await self.session.get(f"{BING_URL}{redirect_url}")
# https://www.bing.com/images/create/async/results/{ID}?q={PROMPT}
polling_url = f"{BING_URL}/images/create/async/results/{request_id}?q={url_encoded_prompt}"
# Poll for results
if not self.quiet:
Expand All @@ -343,7 +306,6 @@ async def get_images(self, prompt: str, timeout: int = 200, max_generate_time_se
time_sec = time_sec + 1
if time_sec >= max_generate_time_sec:
raise TimeoutError("Out of generate time")
continue
# Use regex to search for src=""
image_links = regex.findall(r'src="([^"]+)"', content)
# Remove size limit
Expand All @@ -368,13 +330,11 @@ async def get_images(self, prompt: str, timeout: int = 200, max_generate_time_se
async def async_image_gen(
prompt: str,
u_cookie=None,
debug_file=None,
quiet=False,
all_cookies=None,
):
async with ImageGenAsync(
u_cookie,
debug_file=debug_file,
quiet=quiet,
all_cookies=all_cookies,
) as image_generator:
Expand Down Expand Up @@ -450,7 +410,6 @@ def main():
# Create image generator
image_generator = ImageGen(
auth_cookie=args.U,
debug_file=args.debug_file,
quiet=args.quiet,
all_cookies=cookie_json,
)
Expand All @@ -464,7 +423,6 @@ def main():
async_image_gen(
prompt=args.prompt,
u_cookie=args.U,
debug_file=args.debug_file,
quiet=args.quiet,
all_cookies=cookie_json,
),
Expand Down
10 changes: 5 additions & 5 deletions re_edge_gpt/image/upload_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ async def upload_image_url(image_url: str, conversation_id: str, cookies: dict,
data = aiohttp.FormData()
data.add_field('knowledgeRequest', json.dumps(new_payload), content_type="application/json")
async with session.post(url, data=data, proxy=proxy) as resp:
if not resp.status == 200:
raise Exception("Upload image failed")
if resp.status != 200:
raise ConnectionError("Upload image failed")
return (await resp.json())["blobId"]


Expand All @@ -58,12 +58,12 @@ async def upload_image(conversation_id: str, cookies: dict, filename: str = None
elif base64_image is not None:
image_base64 = base64_image
else:
raise Exception('no image provided')
raise ConnectionRefusedError('no image provided')

data = aiohttp.FormData()
data.add_field('knowledgeRequest', json.dumps(new_payload), content_type="application/json")
data.add_field('imageBase64', image_base64, content_type="application/octet-stream")
async with session.post(url, data=data, proxy=proxy) as resp:
if not resp.status == 200:
raise Exception("Upload image failed")
if resp.status != 200:
raise ConnectionError("Upload image failed")
return (await resp.json())["blobId"]
10 changes: 5 additions & 5 deletions re_edge_gpt/plugins/suno.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,27 @@ async def generate_suno_music(cookies: list[dict], iframe_id: str, request_id: s
f"iframeid={iframe_id}&requestid={request_id}"
response = await session.get(generate_url)
if response.status != 200:
raise Exception("Generate suno music failed")
raise ConnectionError("Generate suno music failed")
skey = re.findall('skey=(.*?)&amp;', await response.text())
if skey:
skey = skey[0]
else:
raise Exception("Generate suno music failed cause by: skey")
raise ConnectionError("Generate suno music failed cause by: skey")
real_generate_url = f"https://www.bing.com/videos/api/custom/music?skey={skey}" \
f"&safesearch=Moderate&vdpp=suno&requestid={request_id}" \
f"&ig={secrets.token_hex(32).upper()}&iid=vsn&sfx=1"
async with aiohttp.ClientSession(
headers={"Referer": real_generate_url}, cookies=new_cookies
) as real_session:
for i in range(0, 20, 1):
while True:
await asyncio.sleep(3)
response = await real_session.get(real_generate_url)
suno_response: dict = json.loads(await response.text())
suno_response = {"RawResponse": json.loads(suno_response.get("RawResponse"))}
if suno_response.get("RawResponse").get("status") == "running":
continue
if suno_response.get("RawResponse").get("status") != "complete":
raise Exception("Generate suno music failed cause by: status error")
raise ConnectionError("Generate suno music failed cause by: status error")
suno_response = suno_response.get("RawResponse")
return {
"Image": f"https://th.bing.com/th?&id={suno_response.get('imageKey')}",
Expand All @@ -50,4 +50,4 @@ async def generate_suno_music(cookies: list[dict], iframe_id: str, request_id: s
"Title": f"https://th.bing.com/th?&id={suno_response.get('gptPrompt')}",
"Lyrics": f"https://th.bing.com/th?&id={suno_response.get('lyrics')}",
}
raise Exception("Generate suno music failed")
raise ConnectionError("Generate suno music failed")

0 comments on commit 3cb17b0

Please sign in to comment.