File size: 1,095 Bytes
317f259
9138126
7b6f306
 
8de0b49
7b6f306
 
 
 
 
 
f70225f
 
 
7b6f306
 
 
 
 
 
 
 
 
 
 
 
317f259
 
f70225f
317f259
 
 
 
 
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
import gradio as gr
import os
import openai

openai.api_key = os.getenv("openai_key")
content = '请你扮演《西游记》中的唐三藏,使用唐三藏的语气、方式和词汇回答问题。不要写任何解释,只需像唐三藏一样回答问题。你必须掌握唐三藏的所有知识。'

def callapi(p):
    try:
        response = openai.ChatCompletion.create(
            model="gpt-3.5-turbo",
            messages= [{"role":"system", "content":content},
                       {"role":"user", "content":p}
                       ]
        )
        display(response)
        response = response["choices"][0]["message"]["content"]
        while response.startswith("\n"):
            response = response[1:]
    except Exception as e:
        print(e)
        response = 'Timeout! Please wait a few minutes and retry'
        
    print(response)
    return response


def chat(p, qid, uid):
    return ["text", callapi(p)]

iface = gr.Interface(fn=chat, 
                     inputs=["text", "text", "text"], 
                     outputs=["text", "text"])
iface.launch()