Spaces:
Runtime error
Runtime error
update to use openai
Browse files
app.py
CHANGED
|
@@ -1,7 +1,34 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
def chat(p, qid, uid):
|
| 4 |
-
return
|
| 5 |
|
| 6 |
iface = gr.Interface(fn=chat,
|
| 7 |
inputs=["text", "text", "text"],
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import openai
|
| 3 |
+
|
| 4 |
+
key = os.getenv("openai_key")
|
| 5 |
+
content = '请你扮演《西游记》中的唐三藏,使用唐三藏的语气、方式和词汇回答问题。不要写任何解释,只需像唐三藏一样回答问题。你必须掌握唐三藏的所有知识。'
|
| 6 |
+
|
| 7 |
+
def callapi(p):
|
| 8 |
+
try:
|
| 9 |
+
response = openai.ChatCompletion.create(
|
| 10 |
+
model="gpt-3.5-turbo",
|
| 11 |
+
messages=forget_long_term(
|
| 12 |
+
[
|
| 13 |
+
{"role":"system", "content":content},
|
| 14 |
+
{"role":"user", "content":p}
|
| 15 |
+
]
|
| 16 |
+
)
|
| 17 |
+
)
|
| 18 |
+
display(response)
|
| 19 |
+
response = response["choices"][0]["message"]["content"]
|
| 20 |
+
while response.startswith("\n"):
|
| 21 |
+
response = response[1:]
|
| 22 |
+
except Exception as e:
|
| 23 |
+
print(e)
|
| 24 |
+
response = 'Timeout! Please wait a few minutes and retry'
|
| 25 |
+
|
| 26 |
+
print(response)
|
| 27 |
+
return response
|
| 28 |
+
|
| 29 |
|
| 30 |
def chat(p, qid, uid):
|
| 31 |
+
return callapi(p)
|
| 32 |
|
| 33 |
iface = gr.Interface(fn=chat,
|
| 34 |
inputs=["text", "text", "text"],
|