File size: 6,687 Bytes
91525e6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import gradio as gr
import json
import asyncio
import os
import EdgeGPT
from EdgeGPT.EdgeGPT import Chatbot, ConversationStyle
import re
from EdgeGPT.EdgeUtils import Query,Cookie

"""
参考资料:
https://www.bilibili.com/video/BV1KV4y197bX/?spm_id_from=333.1007.tianma.2-2-5.click&vd_source=9dc7baa799a4531df364244d2e87ee01
https://blog.csdn.net/xmh_free/article/details/127210992

EdgeGPT-0.7.1
EdgeGPT-0.10.11


python3 -m pip install EdgeGPT --upgrade

pip install EdgeGPT --upgrade

https://github.com/acheong08/EdgeGPT/blob/0.4.4/src/EdgeGPT.py
"""


# read cookie from local file
# with open('./cookies.json', 'r') as f:
#     cookies = json.load(f)

async def get_model_reply(prompt,style,cookies,context=[]):
    # combines the new question with a previous context

    context = [prompt]
    cookies = json.loads(cookies)
    # given the most recent context (4096 characters)
    # continue the text up to 2048 tokens ~ 8192 charaters

    try:
        try:
            print(f"使用模式:{style}")
            bot =  Chatbot(cookies=cookies)
            print("1")
            raw_data = await bot.ask(prompt, conversation_style=style)
            await bot.close()

            print(raw_data)

        except Exception as e:
            print(f">>>>>> 报错信息:\t ----->  【{e}】")
            if "throttled" in str(e):
                response = "> **错误: 我们很抱歉,但你已经达到了你在24小时内可以向Bing发送消息的最大数量。请稍后再查看!**"
                print(response)
                context += ["1"]
                responses = [(u, b) for u, b in zip(context[::2], context[1::2])]
                return responses, context

            if "CAPTCHA" in str(e):
                response = "> **错误: 账号需要人机验证。请登录聊天页面进行验证**"
                print(response)
                context += ["-2"]
                responses = [(u, b) for u, b in zip(context[::2], context[1::2])]
                return responses, context

            if "access this service" in str(e):
                response = "> **账号过期** >>>>  账号需要重新激活。"
                print(response)
                context += ["-1"]
                responses = [(u, b) for u, b in zip(context[::2], context[1::2])]
                return responses, context

        try:
            response = raw_data["item"]["messages"][1]["text"]
        except:
            response = raw_data["item"]["messages"][1]["adaptiveCards"][0]["body"][0]["text"]
        response = re.sub(r'\^', '', response)
        response = response.rstrip()
        context += [response]
        responses = [(u, b) for u, b in zip(context[::2], context[1::2])]
        return responses, context


    except EdgeGPT.EdgeGPT.NotAllowedToAccess:
        response = " >>>>  账号需要重新激活。"
        print(response)
        context += ["-1"]
        responses = [(u, b) for u, b in zip(context[::2], context[1::2])]
        return responses, context

    except:

        try:
            if raw_data["item"]["throttling"]["numUserMessagesInConversation"] > raw_data["item"]["throttling"][
                "maxNumUserMessagesInConversation"]:
                response = ">>>请重新开启新的对话。"
                print(response)
                context += ["0"]
                responses = [(u, b) for u, b in zip(context[::2], context[1::2])]
                return responses, context

        except:
            if raw_data["item"]["result"]["value"] == "Throttled":
                response = "> **错误: 我们很抱歉,但你已经达到了你在24小时内可以向Bing发送消息的最大数量。请稍后再查看!**"
                print(response)
                context += ["1"]
                responses = [(u, b) for u, b in zip(context[::2], context[1::2])]
                return responses, context

            if raw_data["item"]["result"]["value"] == "CaptchaChallenge":
                response = "> **错误: 账号需要验证码验证登录。**"
                print(response)
                context += ["-2"]
                responses = [(u, b) for u, b in zip(context[::2], context[1::2])]
                return responses, context

# inputs= "你好"
# cookies = list_wo = r"E:\pythonProjectWork\xiaolv\mew-bing\zzceshi的.json"
# a,b = get_model_reply(prompt=inputs,style="precise",cookies = cookies)


with gr.Blocks() as dialog_app:

    with gr.Tab("Cookies"):
        cookies = gr.Textbox(lines=2, label="输入bing.com中的cookies(JSON格式)",)
    with gr.Tab("New Bing Chat GPT4"):
        gr.Markdown("""
        # 连接 new-bing 接口,用的是GPT4接口
        如果回复为 "1" ,说明目前服务比较火爆,建议过段时间再来用;
        如果回复为 "0" , 请刷新网页重来。
        如果回复为 "-1" , 需要重新利用梯子去激活一下聊天功能。
        如果回复为 "-2" , 说明该账号需要登录bing聊天页面进行人机验证。
        
        ### 由于源码的更新,此时传入 [ choices = None ] 才能返回要求的格式。即不选择特定的choices模式
        """)

        chatbot = gr.Chatbot()
        state = gr.State([])
        markdown = gr.Markdown(label="Output")

        with gr.Row():
            inputs = gr.Textbox(
                label="输入问题",
                placeholder="请输入你的文本,确保已经正确填入bing.com中的cookies"
            )
            style = gr.Dropdown(label="回答倾向模式选择", choices=["creative", "balanced", "precise"], multiselect=False,
                                value="precise", type="value")

        inputs.submit(get_model_reply, [inputs, style, cookies,state ], [chatbot, state])
        send = gr.Button("发送请求.....")
        send.click(get_model_reply, [inputs, style, cookies, state], [chatbot, state],api_name="xiaolvgpt")

        gr.Markdown("""
        如果输入后没有返回结果,大概率是你的 cookies 账号未申请new-bing的聊天功能;
        步骤:
        1. 在 Edge 浏览器;
        2. 登录 个人账号 // 申请新账号;
        3. 打开“动力”;
        4. https://cn.bing.com/ 网页 或者 https://www.bing.com/new 看是否有聊天功能;
        5. 新版的 Edge 浏览器右上角有个bing的图标,点击后就可以进行聊天;
        6. 说明可以了,再关掉“动力”,使用国内的网络再次复制cookies。
        """)

# launches the app in a new local port
dialog_app.launch(show_error=True)
# 为网站设置密码防止滥用
# dialog_app.launch(auth=("admin", "pass1234"))