-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbing.py
91 lines (76 loc) · 3.09 KB
/
bing.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import os
import sys
import json
from pathlib import Path
import argparse
import asyncio
from EdgeGPT import Chatbot, ConversationStyle
async def novelist(args: argparse.Namespace) -> None:
if args.style == "creative":
generate_list = {"creative": ConversationStyle.creative}
elif args.style == "balanced":
generate_list = {"balanced": ConversationStyle.balanced}
elif args.style == "precise":
generate_list = {"precise": ConversationStyle.precise}
else:
generate_list = {"creative": ConversationStyle.creative, "balanced": ConversationStyle.balanced, "precise": ConversationStyle.precise}
# load prompts
with open(args.input, 'r') as f:
prompts = [line.strip() for line in f.readlines() if line.strip()]
# load existing data and generate
print(f"prompts load success and start to generating...")
f = open(args.output, 'w', encoding='utf-8')
for prompt in prompts:
print(f"Prompt in Use: {prompt}")
f.write(prompt + "\n\n----------------------------------\n")
for key, conversation_style in generate_list.items():
f.write(f"style: {key}\n\n")
print(f"try style: {key}")
bot = await Chatbot.create(proxy=args.proxy, cookies=args.cookies)
result = await bot.ask(prompt=prompt, conversation_style=conversation_style, wss_link=args.wss_link)
if result:
text = result['item']['messages'][1]['text']
print(f"generate success: {text}")
f.write(f"{text}\n\n")
else:
print("generate failed, try again...")
f.write("failed\n\n")
continue
print("cool down for 5 seconds...")
await bot.close()
await asyncio.sleep(5)
f.close()
print("finished.")
def main() -> None:
parser = argparse.ArgumentParser()
parser.add_argument('--input', default="input.txt", help="Select the prompt file")
parser.add_argument('--output', default="result.txt", help="Select the output file")
# parser.add_argument('--repeat', default=1, help="Select how many times to generate for each example", type=int)
parser.add_argument('--cookie', default="exported-cookies.json", help="Select the cookie file")
parser.add_argument(
"--proxy",
help="Proxy URL (e.g. socks5://127.0.0.1:1080)",
type=str,
)
parser.add_argument(
"--wss-link",
help="WSS URL(e.g. wss://sydney.bing.com/sydney/ChatHub)",
type=str,
default="wss://sydney.bing.com/sydney/ChatHub",
)
parser.add_argument(
"--style",
choices=["creative", "balanced", "precise"],
default=None,
)
args = parser.parse_args()
if not os.path.exists(args.input):
raise FileNotFoundError(f"{args.input} not found")
try:
args.cookies = json.loads(Path(args.cookie).read_text(encoding="utf-8"))
except OSError as exc:
print(f"Could not open cookie file: {exc}", file=sys.stderr)
sys.exit(1)
asyncio.run(novelist(args))
if __name__ == "__main__":
main()