File size: 1,779 Bytes
7372a64
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import requests
import json

API_KEY="4d95a8e9615d73939d5f0b8997bc4614eaf96f854a5ab503f5d781e1c5521c73"
url = "https://api.tigerbot.com/v1/chat/completions"

headers = {
    'Authorization': 'Bearer ' + API_KEY
}
payload = {
        "model": "tigerbot-13b-chat",
        "query": "",
        "session":[],
        "internet": True
    }
def getAnsBytiger(t):
    payload['query'] = t
    response = requests.post(url, headers=headers, json=payload)
    res = response.json()
    res = res["result"]
    payload['session'].append({"human": t, "assistant": res})
    return res


if 'prompts' not in st.session_state:
    st.session_state['prompts'] = []
    
if 'generated' not in st.session_state:
    st.session_state['generated'] = []

if 'past' not in st.session_state:
    st.session_state['past'] = []

def end_click():
    st.session_state['prompts'] = []
    st.session_state['past'] = []
    st.session_state['generated'] = []
    st.session_state['user'] = ""
def chat_click():
    if st.session_state['user'] != '':
        chat_input = st.session_state['user']
        output = getAnsBytiger(chat_input)
        st.session_state['past'].append(chat_input)
        st.session_state['generated'].append(output)
        st.session_state['prompts'].append(output)
        st.session_state['user'] = ""
st.image("./logo.jpg", use_column_width='auto')
st.title("珠宝贝专属聊天机器人")

user_input = st.text_input("输入:", key="user")
chat_button = st.button("发送", on_click=chat_click)
end_button = st.button("新聊天", on_click=end_click)

if st.session_state['generated']:
    for i in range(0, len(st.session_state['generated']), 1):
        message(st.session_state['past'][i], is_user=True)
        message(st.session_state['generated'][i], key=str(i))