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

Commit 9a2788a

Browse files
committed
Refactor docs
1 parent 6658617 commit 9a2788a

File tree

6 files changed

+81
-88
lines changed

6 files changed

+81
-88
lines changed

README.md

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,6 @@ if __name__ == "__main__":
183183
> * copy the value from the _U
184184
185185
```python
186-
import asyncio
187186
import os
188187
import shutil
189188
from pathlib import Path
@@ -212,23 +211,13 @@ def test_generate_image_sync():
212211
image_list = sync_gen.get_images("tree")
213212
print(image_list)
214213

215-
216-
# Generate image list async
217-
async def test_generate_image_async():
218-
image_list = await async_gen.get_images("tree")
219-
print(image_list)
220-
221-
222214
if __name__ == "__main__":
223215
# Make dir to save image
224216
Path("test_output").mkdir(exist_ok=True)
225217
# Save image
226218
test_save_images_sync()
227219
# Generate image sync
228220
test_generate_image_sync()
229-
# Generate image async
230-
loop = asyncio.get_event_loop()
231-
loop.run_until_complete(test_generate_image_async())
232221
# Remove dir
233222
shutil.rmtree(test_output_dir)
234223
```
@@ -240,7 +229,7 @@ if __name__ == "__main__":
240229
<details open>
241230

242231
> * Q: RuntimeError: This event loop is already running
243-
> * If you are using Jupyter, pls use nest_asyncio.apply()
232+
> * A: If you are using Jupyter, pls use nest_asyncio.apply()
244233
245234
> * Q: Exception: UnauthorizedRequest: Cannot retrieve user status.
246235
> * A: Renew your cookie file.

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

0 commit comments

Comments
 (0)