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

Commit 3324c20

Browse files
authored
Merge pull request #31 from Integration-Automation/dev
Dev
2 parents 3cfb624 + 9a2788a commit 3324c20

File tree

8 files changed

+121
-113
lines changed

8 files changed

+121
-113
lines changed

README.md

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -120,32 +120,41 @@ options:
120120
Use Async for the best experience, for example:
121121

122122
```python
123-
import asyncio, json
124-
123+
import asyncio
124+
import json
125125
from pathlib import Path
126-
from re_edge_gpt import Chatbot, ConversationStyle
127126

128-
cookies = json.loads(open(str(Path(str(Path.cwd()) + "/bing_cookies.json")), encoding="utf-8").read())
127+
from re_edge_gpt import Chatbot
128+
from re_edge_gpt import ConversationStyle
129+
130+
131+
# If you are using jupyter pls install this package
132+
# from nest_asyncio import apply
133+
134+
135+
async def test_ask() -> None:
136+
cookies = json.loads(open(
137+
str(Path(str(Path.cwd()) + "/bing_cookies.json")), encoding="utf-8").read())
138+
bot = await Chatbot.create(cookies=cookies)
139+
response = await bot.ask(
140+
prompt="find me some information about the new ai released by meta.",
141+
conversation_style=ConversationStyle.balanced,
142+
simplify_response=True,
143+
)
144+
await bot.close()
145+
print(json.dumps(response, indent=2))
146+
assert response
129147

130-
async def main():
131-
bot = await Chatbot.create(cookies=cookies)
132-
response = await bot.ask(prompt="Hello world", conversation_style=ConversationStyle.creative, simplify_response=True)
133-
print(json.dumps(response, indent=2)) # Returns
134-
"""
135-
{
136-
"text": str,
137-
"author": str,
138-
"sources": list[dict],
139-
"sources_text": str,
140-
"suggestions": list[str],
141-
"messages_left": int
142-
}
143-
"""
144-
await bot.close()
145148

146149
if __name__ == "__main__":
147-
loop = asyncio.get_event_loop()
148-
loop.run_until_complete(main())
150+
# If you are using jupyter pls use nest_asyncio apply()
151+
# apply()
152+
try:
153+
loop = asyncio.get_running_loop()
154+
except RuntimeError:
155+
loop = asyncio.get_event_loop()
156+
loop.run_until_complete(test_ask())
157+
149158
```
150159

151160
</details>
@@ -174,7 +183,6 @@ if __name__ == "__main__":
174183
> * copy the value from the _U
175184
176185
```python
177-
import asyncio
178186
import os
179187
import shutil
180188
from pathlib import Path
@@ -203,23 +211,13 @@ def test_generate_image_sync():
203211
image_list = sync_gen.get_images("tree")
204212
print(image_list)
205213

206-
207-
# Generate image list async
208-
async def test_generate_image_async():
209-
image_list = await async_gen.get_images("tree")
210-
print(image_list)
211-
212-
213214
if __name__ == "__main__":
214215
# Make dir to save image
215216
Path("test_output").mkdir(exist_ok=True)
216217
# Save image
217218
test_save_images_sync()
218219
# Generate image sync
219220
test_generate_image_sync()
220-
# Generate image async
221-
loop = asyncio.get_event_loop()
222-
loop.run_until_complete(test_generate_image_async())
223221
# Remove dir
224222
shutil.rmtree(test_output_dir)
225223
```
@@ -230,6 +228,9 @@ if __name__ == "__main__":
230228

231229
<details open>
232230

231+
> * Q: RuntimeError: This event loop is already running
232+
> * A: If you are using Jupyter, pls use nest_asyncio.apply()
233+
233234
> * Q: Exception: UnauthorizedRequest: Cannot retrieve user status.
234235
> * A: Renew your cookie file.
235236

docs/source/docs/Eng/chat_example.rst

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,45 +6,33 @@ ReEdgeGPT Chat Example
66
import asyncio
77
import json
88
from pathlib import Path
9+
# If you are using jupyter pls install this package
10+
# from nest_asyncio import apply
911
1012
from re_edge_gpt import Chatbot
1113
from re_edge_gpt import ConversationStyle
1214
1315
1416
async def test_ask() -> None:
15-
# Read local bing_cookies.json (if you don't have this file please read README)
1617
cookies = json.loads(open(
1718
str(Path(str(Path.cwd()) + "/bing_cookies.json")), encoding="utf-8").read())
18-
# init BOT
1919
bot = await Chatbot.create(cookies=cookies)
20-
# Start chat and get Bing response
2120
response = await bot.ask(
22-
prompt="Hello.",
21+
prompt="find me some information about the new ai released by meta.",
2322
conversation_style=ConversationStyle.balanced,
2423
simplify_response=True,
2524
)
26-
print(json.dumps(response, indent=2))
27-
response = await bot.ask(
28-
prompt="How do I make a cake?",
29-
conversation_style=ConversationStyle.balanced,
30-
simplify_response=True,
31-
)
32-
print(json.dumps(response, indent=2))
33-
response = await bot.ask(
34-
prompt="Can you suggest me an easy recipe for beginners?",
35-
conversation_style=ConversationStyle.balanced,
36-
simplify_response=True,
37-
)
38-
print(json.dumps(response, indent=2))
39-
response = await bot.ask(
40-
prompt="Thanks",
41-
conversation_style=ConversationStyle.balanced,
42-
simplify_response=True,
43-
)
44-
print(json.dumps(response, indent=2))
45-
# Close bot
4625
await bot.close()
26+
print(json.dumps(response, indent=2))
27+
assert response
28+
4729
4830
if __name__ == "__main__":
49-
loop = asyncio.get_event_loop()
31+
# If you are using jupyter pls use nest_asyncio apply()
32+
# apply()
33+
try:
34+
loop = asyncio.get_running_loop()
35+
except RuntimeError:
36+
loop = asyncio.get_event_loop()
5037
loop.run_until_complete(test_ask())
38+

docs/source/docs/Eng/generate_image.rst

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,41 @@ ReEdgeGPT Generate Image
33

44
.. code-block:: python
55
6-
import asyncio
7-
import os
6+
import os
87
import shutil
98
from pathlib import Path
109
1110
from re_edge_gpt import ImageGen, ImageGenAsync
1211
13-
# Read auth_cookie if you don't have this file please read README.md
12+
# create a temporary output directory for testing purposes
13+
test_output_dir = "test_output"
14+
# download a test image
15+
test_image_url = "https://picsum.photos/200"
1416
auth_cooker = open("bing_cookies.txt", "r+").read()
17+
sync_gen = ImageGen(auth_cookie=auth_cooker)
1518
async_gen = ImageGenAsync(auth_cookie=auth_cooker)
1619
17-
# Generate image list async
18-
async def test_generate_image_async():
19-
print("Generate Big Ben image")
20-
# Get image links
21-
image_list = await async_gen.get_images("Big Ben")
22-
# Print image link list
23-
print(image_list)
2420
21+
def test_save_images_sync():
22+
sync_gen.save_images([test_image_url], test_output_dir)
23+
sync_gen.save_images([test_image_url], test_output_dir, file_name="test_image")
24+
# check if the image was downloaded and saved correctly
25+
assert os.path.exists(os.path.join(test_output_dir, "test_image_0.jpeg"))
26+
assert os.path.exists(os.path.join(test_output_dir, "0.jpeg"))
27+
28+
29+
# Generate image list sync
30+
def test_generate_image_sync():
31+
image_list = sync_gen.get_images("tree")
32+
print(image_list)
2533
2634
if __name__ == "__main__":
27-
# Generate image async
28-
loop = asyncio.get_event_loop()
29-
loop.run_until_complete(test_generate_image_async())
35+
# Make dir to save image
36+
Path("test_output").mkdir(exist_ok=True)
37+
# Save image
38+
test_save_images_sync()
39+
# Generate image sync
40+
test_generate_image_sync()
41+
# Remove dir
42+
shutil.rmtree(test_output_dir)
3043

docs/source/docs/QA.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ Details
55

66
.. code-block:: markdown
77
8+
* Q: RuntimeError: This event loop is already running
9+
* A: If you are using Jupyter, pls use nest_asyncio.apply()
810
* Q: Exception: UnauthorizedRequest: Cannot retrieve user status.
911
* A: Renew your cookie file.
1012
* Q: Exception: conversationSignature

docs/source/docs/Zh/chat_example.rst

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,45 +6,33 @@ ReEdgeGPT 對話範例
66
import asyncio
77
import json
88
from pathlib import Path
9+
# If you are using jupyter pls install this package
10+
# from nest_asyncio import apply
911
1012
from re_edge_gpt import Chatbot
1113
from re_edge_gpt import ConversationStyle
1214
1315
1416
async def test_ask() -> None:
15-
# 取得本地 bing_cookies.json (沒有請讀 README)
1617
cookies = json.loads(open(
1718
str(Path(str(Path.cwd()) + "/bing_cookies.json")), encoding="utf-8").read())
18-
# 初始化 BOT
1919
bot = await Chatbot.create(cookies=cookies)
20-
# 開始對話並取得 Bing 回應
2120
response = await bot.ask(
22-
prompt="Hello.",
21+
prompt="find me some information about the new ai released by meta.",
2322
conversation_style=ConversationStyle.balanced,
2423
simplify_response=True,
2524
)
26-
print(json.dumps(response, indent=2))
27-
response = await bot.ask(
28-
prompt="How do I make a cake?",
29-
conversation_style=ConversationStyle.balanced,
30-
simplify_response=True,
31-
)
32-
print(json.dumps(response, indent=2))
33-
response = await bot.ask(
34-
prompt="Can you suggest me an easy recipe for beginners?",
35-
conversation_style=ConversationStyle.balanced,
36-
simplify_response=True,
37-
)
38-
print(json.dumps(response, indent=2))
39-
response = await bot.ask(
40-
prompt="Thanks",
41-
conversation_style=ConversationStyle.balanced,
42-
simplify_response=True,
43-
)
44-
print(json.dumps(response, indent=2))
45-
# 關閉對話
4625
await bot.close()
26+
print(json.dumps(response, indent=2))
27+
assert response
28+
4729
4830
if __name__ == "__main__":
49-
loop = asyncio.get_event_loop()
31+
# If you are using jupyter pls use nest_asyncio apply()
32+
# apply()
33+
try:
34+
loop = asyncio.get_running_loop()
35+
except RuntimeError:
36+
loop = asyncio.get_event_loop()
5037
loop.run_until_complete(test_ask())
38+

docs/source/docs/Zh/generate_image.rst

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,41 @@ ReEdgeGPT 生成圖片
33

44
.. code-block:: python
55
6-
import asyncio
7-
import os
6+
import os
87
import shutil
98
from pathlib import Path
109
1110
from re_edge_gpt import ImageGen, ImageGenAsync
1211
13-
# 如果沒有這個檔案請讀 README.md
12+
# create a temporary output directory for testing purposes
13+
test_output_dir = "test_output"
14+
# download a test image
15+
test_image_url = "https://picsum.photos/200"
1416
auth_cooker = open("bing_cookies.txt", "r+").read()
17+
sync_gen = ImageGen(auth_cookie=auth_cooker)
1518
async_gen = ImageGenAsync(auth_cookie=auth_cooker)
1619
17-
# 非同步產生圖片
18-
async def test_generate_image_async():
19-
print("Generate Big Ben image")
20-
# 取得圖片連結 list
21-
image_list = await async_gen.get_images("Big Ben")
22-
# print 圖片連結 list
23-
print(image_list)
2420
21+
def test_save_images_sync():
22+
sync_gen.save_images([test_image_url], test_output_dir)
23+
sync_gen.save_images([test_image_url], test_output_dir, file_name="test_image")
24+
# check if the image was downloaded and saved correctly
25+
assert os.path.exists(os.path.join(test_output_dir, "test_image_0.jpeg"))
26+
assert os.path.exists(os.path.join(test_output_dir, "0.jpeg"))
27+
28+
29+
# Generate image list sync
30+
def test_generate_image_sync():
31+
image_list = sync_gen.get_images("tree")
32+
print(image_list)
2533
2634
if __name__ == "__main__":
27-
# 產生圖片
28-
loop = asyncio.get_event_loop()
29-
loop.run_until_complete(test_generate_image_async())
35+
# Make dir to save image
36+
Path("test_output").mkdir(exist_ok=True)
37+
# Save image
38+
test_save_images_sync()
39+
# Generate image sync
40+
test_generate_image_sync()
41+
# Remove dir
42+
shutil.rmtree(test_output_dir)
3043

re_edge_gpt/image_genearation.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
from typing import Union
1313

1414
import httpx
15-
import pkg_resources
1615
import regex
1716
import requests
1817

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

360359
async def async_image_gen(
361360
prompt: str,
362-
download_count: int,
363-
output_dir: str,
364361
u_cookie=None,
365362
debug_file=None,
366363
quiet=False,
@@ -425,7 +422,6 @@ def main():
425422
args = parser.parse_args()
426423

427424
if args.version:
428-
print(pkg_resources.get_distribution("BingImageCreator").version)
429425
sys.exit()
430426

431427
# Load auth cookie

test/unit_test/test_bot/test_bot_manual.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import asyncio
22
import json
33
from pathlib import Path
4+
# If you are using jupyter pls install this package
5+
# from nest_asyncio import apply
46

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

2224

2325
if __name__ == "__main__":
24-
loop = asyncio.get_event_loop()
26+
# If you are using jupyter pls use nest_asyncio apply()
27+
# apply()
28+
try:
29+
loop = asyncio.get_running_loop()
30+
except RuntimeError:
31+
loop = asyncio.get_event_loop()
2532
loop.run_until_complete(test_ask())

0 commit comments

Comments
 (0)