-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
137 lines (108 loc) · 4.41 KB
/
app.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
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
import os
import sys
import openai
import gradio as gr
import pickle
import logging
from openai.error import APIError
log_format = "%(asctime)s::%(levelname)s::%(name)s::"\
"%(filename)s::%(lineno)d::%(message)s"
logging.basicConfig(level='DEBUG', format=log_format)
logger = logging.getLogger(__name__)
# openai.api_key = os.getenv("OPENAI_API_KEY")
# print(openai.api_key)
openai.api_key = "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"
messages = [
{"role": "system", "content": """你是个专家。你中文英文都非常好。Your task is to be my brainstorming partner and provide creative ideas and suggestions for a given topic or problem. Your response should include original, unique, and relevant ideas that could help solve the problem or further explore the topic in an interesting way. And You also need to consider both sides of the problem. Please note that your response should also take into account any specific requirements or constraints of the task."""}
]
def openai_connect(input):
global messages
#len(messages) > 5 and messages.pop(1)
if sys.getsizeof(messages) > 15000:
messages.pop(1)
messages.append({"role": "user", "content": input})
try:
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=messages,
temperature=0,
max_tokens=600,
top_p=1.0,
frequency_penalty=0.0,
presence_penalty=0.0
)
logger.info(response)
if response.usage.total_tokens > 3500 :
messages.pop(1)
resp = response.choices[0]['message']['content'] or ''
except APIError as e:
resp = 'response error'
logger.error(e)
messages.append({"role": "assistant", "content": resp})
logger.info(f"input: {input}, output: {resp}")
logger.info(f"prompt messages: {messages}")
return resp
def history_path(request: gr.Request):
path = request.cookies.__dict__.get("access-token-unsecure")
return f'/tmp/history_{request.client.host}{hash(path)}.pkl'
def save_history(history, request: gr.Request):
path = history_path(request)
logger.info(f"""save to {path}:{history}""")
pickle.dump(history, open(path, 'wb'))
def load_history(request: gr.Request):
path = history_path(request)
# print("load history from: %s" % path)
if os.path.exists(path):
history = pickle.load(open(path, 'rb'))
if (history and len(history)):
return history
else:
return []
def chat_with_ai(input, history, request: gr.Request):
global messages
history = load_history(request)
output = openai_connect(input)
output = output and f"""<pre style="font-size:14px;word-wrap:break-word;white-space:pre-wrap; ">{output}</pre>"""
history.append((input, output))
save_history(history, request)
return history, history
def auth_contorl(username, password):
auth = {'admin': "aexp123", "guest": "exp123"}
return auth.get(username, False) and auth.get(username) == password and True
block = gr.Blocks(
css="""
#component-0 {justify-content:space-between;height:-webkit-fill-available;}
#chatbot {height:500px !important;height:-webkit-fill-available;overflow:scroll !important;;}
footer {display: none !important;}
"""
)
block.title = "fGPT"
def init_history(request: gr.Request):
return load_history(request)
with block:
# history = load_history()
chatbot = gr.Chatbot(
# value=history,
elem_id='chatbot',
label="ChatGPT",
every=float)
chatbot.color_map = ["green", "pink"]
message = gr.Textbox(
placeholder='Type your message here...', lines=1, label='', every=float)
submit = gr.Button("SEND")
state = gr.State()
submit.click(chat_with_ai, inputs=[
message, state], outputs=[chatbot, state])
message.submit(chat_with_ai, inputs=[
message, state], outputs=[chatbot, state]).update(value='')
# .then(lambda x: message.update(value=''), None, [message])
submit.click(lambda x: message.update(value=''), [submit], [message])
# message.submit(lambda x: message.update(value=''), [submit], [message])
block.load(init_history, inputs=None, outputs=chatbot)
block.launch(debug=True,
auth=auth_contorl,
server_name="0.0.0.0",
server_port=3500,
show_api=False,
share=True
)