Spaces:
Running
Running
JohnSmith9982
commited on
Commit
•
4a25f41
1
Parent(s):
5cbb476
Update app.py
Browse files
app.py
CHANGED
@@ -9,6 +9,8 @@ import requests
|
|
9 |
import csv
|
10 |
|
11 |
my_api_key = "" # 在这里输入你的 API 密钥
|
|
|
|
|
12 |
initial_prompt = "You are a helpful assistant."
|
13 |
API_URL = "https://api.openai.com/v1/chat/completions"
|
14 |
HISTORY_DIR = "history"
|
@@ -38,30 +40,35 @@ if dockerflag:
|
|
38 |
|
39 |
def parse_text(text):
|
40 |
lines = text.split("\n")
|
|
|
41 |
count = 0
|
|
|
42 |
for i, line in enumerate(lines):
|
43 |
if "```" in line:
|
44 |
count += 1
|
45 |
items = line.split('`')
|
46 |
if count % 2 == 1:
|
47 |
-
lines[i] = f'<pre><code class="{items[-1]}">'
|
|
|
48 |
else:
|
49 |
lines[i] = f'</code></pre>'
|
50 |
else:
|
51 |
if i > 0:
|
52 |
if count % 2 == 1:
|
53 |
line = line.replace("&", "&")
|
54 |
-
line = line.replace("\"", "
|
55 |
-
line = line.replace("\'", "
|
56 |
line = line.replace("<", "<")
|
57 |
line = line.replace(">", ">")
|
58 |
line = line.replace(" ", " ")
|
59 |
-
|
60 |
-
|
61 |
-
return
|
62 |
|
63 |
def predict(inputs, top_p, temperature, openai_api_key, chatbot=[], history=[], system_prompt=initial_prompt, retry=False, summary=False): # repetition_penalty, top_k
|
64 |
|
|
|
|
|
65 |
headers = {
|
66 |
"Content-Type": "application/json",
|
67 |
"Authorization": f"Bearer {openai_api_key}"
|
@@ -135,7 +142,7 @@ def predict(inputs, top_p, temperature, openai_api_key, chatbot=[], history=[],
|
|
135 |
break
|
136 |
except Exception as e:
|
137 |
chatbot.pop()
|
138 |
-
chatbot.append((history[-1], f"
|
139 |
history.pop()
|
140 |
yield chatbot, history
|
141 |
break
|
@@ -231,7 +238,7 @@ description = """<div align=center>
|
|
231 |
with gr.Blocks() as demo:
|
232 |
gr.HTML(title)
|
233 |
keyTxt = gr.Textbox(show_label=True, placeholder=f"在这里输入你的OpenAI API-key...",
|
234 |
-
value=my_api_key, label="API Key", type="password").style(container=True)
|
235 |
chatbot = gr.Chatbot() # .style(color_map=("#1D51EE", "#585A5B"))
|
236 |
history = gr.State([])
|
237 |
promptTemplates = gr.State({})
|
|
|
9 |
import csv
|
10 |
|
11 |
my_api_key = "" # 在这里输入你的 API 密钥
|
12 |
+
HIDE_MY_KEY = False # 如果你想在UI中隐藏你的 API 密钥,将此值设置为 True
|
13 |
+
|
14 |
initial_prompt = "You are a helpful assistant."
|
15 |
API_URL = "https://api.openai.com/v1/chat/completions"
|
16 |
HISTORY_DIR = "history"
|
|
|
40 |
|
41 |
def parse_text(text):
|
42 |
lines = text.split("\n")
|
43 |
+
lines = [line for line in lines if line != ""]
|
44 |
count = 0
|
45 |
+
firstline = False
|
46 |
for i, line in enumerate(lines):
|
47 |
if "```" in line:
|
48 |
count += 1
|
49 |
items = line.split('`')
|
50 |
if count % 2 == 1:
|
51 |
+
lines[i] = f'<pre><code class="{items[-1]}" style="display: block; white-space: pre; padding: 0 1em 1em 1em; color: #fff; background: #000;">'
|
52 |
+
firstline = True
|
53 |
else:
|
54 |
lines[i] = f'</code></pre>'
|
55 |
else:
|
56 |
if i > 0:
|
57 |
if count % 2 == 1:
|
58 |
line = line.replace("&", "&")
|
59 |
+
line = line.replace("\"", "`\"`")
|
60 |
+
line = line.replace("\'", "`\'`")
|
61 |
line = line.replace("<", "<")
|
62 |
line = line.replace(">", ">")
|
63 |
line = line.replace(" ", " ")
|
64 |
+
lines[i] = "<br>"+line
|
65 |
+
text = "".join(lines)
|
66 |
+
return text
|
67 |
|
68 |
def predict(inputs, top_p, temperature, openai_api_key, chatbot=[], history=[], system_prompt=initial_prompt, retry=False, summary=False): # repetition_penalty, top_k
|
69 |
|
70 |
+
print(f"chatbot 1: {chatbot}")
|
71 |
+
|
72 |
headers = {
|
73 |
"Content-Type": "application/json",
|
74 |
"Authorization": f"Bearer {openai_api_key}"
|
|
|
142 |
break
|
143 |
except Exception as e:
|
144 |
chatbot.pop()
|
145 |
+
chatbot.append((history[-1], f"☹️发生了错误<br>返回值:{response.text}<br>异常:{e}"))
|
146 |
history.pop()
|
147 |
yield chatbot, history
|
148 |
break
|
|
|
238 |
with gr.Blocks() as demo:
|
239 |
gr.HTML(title)
|
240 |
keyTxt = gr.Textbox(show_label=True, placeholder=f"在这里输入你的OpenAI API-key...",
|
241 |
+
value=my_api_key, label="API Key", type="password", visible=not HIDE_MY_KEY).style(container=True)
|
242 |
chatbot = gr.Chatbot() # .style(color_map=("#1D51EE", "#585A5B"))
|
243 |
history = gr.State([])
|
244 |
promptTemplates = gr.State({})
|