Skip to content
This repository was archived by the owner on Dec 18, 2024. It is now read-only.
Merged

Dev #31

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 33 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,32 +120,41 @@ options:
Use Async for the best experience, for example:

```python
import asyncio, json

import asyncio
import json
from pathlib import Path
from re_edge_gpt import Chatbot, ConversationStyle

cookies = json.loads(open(str(Path(str(Path.cwd()) + "/bing_cookies.json")), encoding="utf-8").read())
from re_edge_gpt import Chatbot
from re_edge_gpt import ConversationStyle


# If you are using jupyter pls install this package
# from nest_asyncio import apply


async def test_ask() -> None:
cookies = json.loads(open(
str(Path(str(Path.cwd()) + "/bing_cookies.json")), encoding="utf-8").read())
bot = await Chatbot.create(cookies=cookies)
response = await bot.ask(
prompt="find me some information about the new ai released by meta.",
conversation_style=ConversationStyle.balanced,
simplify_response=True,
)
await bot.close()
print(json.dumps(response, indent=2))
assert response

async def main():
bot = await Chatbot.create(cookies=cookies)
response = await bot.ask(prompt="Hello world", conversation_style=ConversationStyle.creative, simplify_response=True)
print(json.dumps(response, indent=2)) # Returns
"""
{
"text": str,
"author": str,
"sources": list[dict],
"sources_text": str,
"suggestions": list[str],
"messages_left": int
}
"""
await bot.close()

if __name__ == "__main__":
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
# If you are using jupyter pls use nest_asyncio apply()
# apply()
try:
loop = asyncio.get_running_loop()
except RuntimeError:
loop = asyncio.get_event_loop()
loop.run_until_complete(test_ask())

```

</details>
Expand Down Expand Up @@ -174,7 +183,6 @@ if __name__ == "__main__":
> * copy the value from the _U

```python
import asyncio
import os
import shutil
from pathlib import Path
Expand Down Expand Up @@ -203,23 +211,13 @@ def test_generate_image_sync():
image_list = sync_gen.get_images("tree")
print(image_list)


# Generate image list async
async def test_generate_image_async():
image_list = await async_gen.get_images("tree")
print(image_list)


if __name__ == "__main__":
# Make dir to save image
Path("test_output").mkdir(exist_ok=True)
# Save image
test_save_images_sync()
# Generate image sync
test_generate_image_sync()
# Generate image async
loop = asyncio.get_event_loop()
loop.run_until_complete(test_generate_image_async())
# Remove dir
shutil.rmtree(test_output_dir)
```
Expand All @@ -230,6 +228,9 @@ if __name__ == "__main__":

<details open>

> * Q: RuntimeError: This event loop is already running
> * A: If you are using Jupyter, pls use nest_asyncio.apply()

> * Q: Exception: UnauthorizedRequest: Cannot retrieve user status.
> * A: Renew your cookie file.

Expand Down
38 changes: 13 additions & 25 deletions docs/source/docs/Eng/chat_example.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,45 +6,33 @@ ReEdgeGPT Chat Example
import asyncio
import json
from pathlib import Path
# If you are using jupyter pls install this package
# from nest_asyncio import apply

from re_edge_gpt import Chatbot
from re_edge_gpt import ConversationStyle


async def test_ask() -> None:
# Read local bing_cookies.json (if you don't have this file please read README)
cookies = json.loads(open(
str(Path(str(Path.cwd()) + "/bing_cookies.json")), encoding="utf-8").read())
# init BOT
bot = await Chatbot.create(cookies=cookies)
# Start chat and get Bing response
response = await bot.ask(
prompt="Hello.",
prompt="find me some information about the new ai released by meta.",
conversation_style=ConversationStyle.balanced,
simplify_response=True,
)
print(json.dumps(response, indent=2))
response = await bot.ask(
prompt="How do I make a cake?",
conversation_style=ConversationStyle.balanced,
simplify_response=True,
)
print(json.dumps(response, indent=2))
response = await bot.ask(
prompt="Can you suggest me an easy recipe for beginners?",
conversation_style=ConversationStyle.balanced,
simplify_response=True,
)
print(json.dumps(response, indent=2))
response = await bot.ask(
prompt="Thanks",
conversation_style=ConversationStyle.balanced,
simplify_response=True,
)
print(json.dumps(response, indent=2))
# Close bot
await bot.close()
print(json.dumps(response, indent=2))
assert response


if __name__ == "__main__":
loop = asyncio.get_event_loop()
# If you are using jupyter pls use nest_asyncio apply()
# apply()
try:
loop = asyncio.get_running_loop()
except RuntimeError:
loop = asyncio.get_event_loop()
loop.run_until_complete(test_ask())

39 changes: 26 additions & 13 deletions docs/source/docs/Eng/generate_image.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,41 @@ ReEdgeGPT Generate Image

.. code-block:: python

import asyncio
import os
import os
import shutil
from pathlib import Path

from re_edge_gpt import ImageGen, ImageGenAsync

# Read auth_cookie if you don't have this file please read README.md
# create a temporary output directory for testing purposes
test_output_dir = "test_output"
# download a test image
test_image_url = "https://picsum.photos/200"
auth_cooker = open("bing_cookies.txt", "r+").read()
sync_gen = ImageGen(auth_cookie=auth_cooker)
async_gen = ImageGenAsync(auth_cookie=auth_cooker)

# Generate image list async
async def test_generate_image_async():
print("Generate Big Ben image")
# Get image links
image_list = await async_gen.get_images("Big Ben")
# Print image link list
print(image_list)

def test_save_images_sync():
sync_gen.save_images([test_image_url], test_output_dir)
sync_gen.save_images([test_image_url], test_output_dir, file_name="test_image")
# check if the image was downloaded and saved correctly
assert os.path.exists(os.path.join(test_output_dir, "test_image_0.jpeg"))
assert os.path.exists(os.path.join(test_output_dir, "0.jpeg"))


# Generate image list sync
def test_generate_image_sync():
image_list = sync_gen.get_images("tree")
print(image_list)

if __name__ == "__main__":
# Generate image async
loop = asyncio.get_event_loop()
loop.run_until_complete(test_generate_image_async())
# Make dir to save image
Path("test_output").mkdir(exist_ok=True)
# Save image
test_save_images_sync()
# Generate image sync
test_generate_image_sync()
# Remove dir
shutil.rmtree(test_output_dir)

2 changes: 2 additions & 0 deletions docs/source/docs/QA.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ Details

.. code-block:: markdown

* Q: RuntimeError: This event loop is already running
* A: If you are using Jupyter, pls use nest_asyncio.apply()
* Q: Exception: UnauthorizedRequest: Cannot retrieve user status.
* A: Renew your cookie file.
* Q: Exception: conversationSignature
Expand Down
38 changes: 13 additions & 25 deletions docs/source/docs/Zh/chat_example.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,45 +6,33 @@ ReEdgeGPT 對話範例
import asyncio
import json
from pathlib import Path
# If you are using jupyter pls install this package
# from nest_asyncio import apply

from re_edge_gpt import Chatbot
from re_edge_gpt import ConversationStyle


async def test_ask() -> None:
# 取得本地 bing_cookies.json (沒有請讀 README)
cookies = json.loads(open(
str(Path(str(Path.cwd()) + "/bing_cookies.json")), encoding="utf-8").read())
# 初始化 BOT
bot = await Chatbot.create(cookies=cookies)
# 開始對話並取得 Bing 回應
response = await bot.ask(
prompt="Hello.",
prompt="find me some information about the new ai released by meta.",
conversation_style=ConversationStyle.balanced,
simplify_response=True,
)
print(json.dumps(response, indent=2))
response = await bot.ask(
prompt="How do I make a cake?",
conversation_style=ConversationStyle.balanced,
simplify_response=True,
)
print(json.dumps(response, indent=2))
response = await bot.ask(
prompt="Can you suggest me an easy recipe for beginners?",
conversation_style=ConversationStyle.balanced,
simplify_response=True,
)
print(json.dumps(response, indent=2))
response = await bot.ask(
prompt="Thanks",
conversation_style=ConversationStyle.balanced,
simplify_response=True,
)
print(json.dumps(response, indent=2))
# 關閉對話
await bot.close()
print(json.dumps(response, indent=2))
assert response


if __name__ == "__main__":
loop = asyncio.get_event_loop()
# If you are using jupyter pls use nest_asyncio apply()
# apply()
try:
loop = asyncio.get_running_loop()
except RuntimeError:
loop = asyncio.get_event_loop()
loop.run_until_complete(test_ask())

39 changes: 26 additions & 13 deletions docs/source/docs/Zh/generate_image.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,41 @@ ReEdgeGPT 生成圖片

.. code-block:: python

import asyncio
import os
import os
import shutil
from pathlib import Path

from re_edge_gpt import ImageGen, ImageGenAsync

# 如果沒有這個檔案請讀 README.md
# create a temporary output directory for testing purposes
test_output_dir = "test_output"
# download a test image
test_image_url = "https://picsum.photos/200"
auth_cooker = open("bing_cookies.txt", "r+").read()
sync_gen = ImageGen(auth_cookie=auth_cooker)
async_gen = ImageGenAsync(auth_cookie=auth_cooker)

# 非同步產生圖片
async def test_generate_image_async():
print("Generate Big Ben image")
# 取得圖片連結 list
image_list = await async_gen.get_images("Big Ben")
# print 圖片連結 list
print(image_list)

def test_save_images_sync():
sync_gen.save_images([test_image_url], test_output_dir)
sync_gen.save_images([test_image_url], test_output_dir, file_name="test_image")
# check if the image was downloaded and saved correctly
assert os.path.exists(os.path.join(test_output_dir, "test_image_0.jpeg"))
assert os.path.exists(os.path.join(test_output_dir, "0.jpeg"))


# Generate image list sync
def test_generate_image_sync():
image_list = sync_gen.get_images("tree")
print(image_list)

if __name__ == "__main__":
# 產生圖片
loop = asyncio.get_event_loop()
loop.run_until_complete(test_generate_image_async())
# Make dir to save image
Path("test_output").mkdir(exist_ok=True)
# Save image
test_save_images_sync()
# Generate image sync
test_generate_image_sync()
# Remove dir
shutil.rmtree(test_output_dir)

4 changes: 0 additions & 4 deletions re_edge_gpt/image_genearation.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
from typing import Union

import httpx
import pkg_resources
import regex
import requests

Expand Down Expand Up @@ -359,8 +358,6 @@ async def get_images(self, prompt: str) -> Union[list, None]:

async def async_image_gen(
prompt: str,
download_count: int,
output_dir: str,
u_cookie=None,
debug_file=None,
quiet=False,
Expand Down Expand Up @@ -425,7 +422,6 @@ def main():
args = parser.parse_args()

if args.version:
print(pkg_resources.get_distribution("BingImageCreator").version)
sys.exit()

# Load auth cookie
Expand Down
9 changes: 8 additions & 1 deletion test/unit_test/test_bot/test_bot_manual.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import asyncio
import json
from pathlib import Path
# If you are using jupyter pls install this package
# from nest_asyncio import apply

from re_edge_gpt import Chatbot
from re_edge_gpt import ConversationStyle
Expand All @@ -21,5 +23,10 @@ async def test_ask() -> None:


if __name__ == "__main__":
loop = asyncio.get_event_loop()
# If you are using jupyter pls use nest_asyncio apply()
# apply()
try:
loop = asyncio.get_running_loop()
except RuntimeError:
loop = asyncio.get_event_loop()
loop.run_until_complete(test_ask())