File size: 3,173 Bytes
22f88f8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import json
import requests

import time


API_TOKEN = st.secrets["hf_api_token"]
headers = {"Authorization": f"Bearer {API_TOKEN}"}
API_URL = "https://api-inference.huggingface.co/models/arampacha/DialoGPT-medium-simpsons"

def query(payload):
    data = json.dumps(payload)
    response = requests.request("POST", API_URL, headers=headers, data=data)
    return json.loads(response.content.decode("utf-8"))

def fake_query(payload):
    user_input = payload["inputs"]["text"]
    time.sleep(1)
    res = {
        "generated_text": user_input[::-1],
        "conversation":{
            "past_user_inputs": st.session_state.past_user_inputs + [user_input],
            "generated_responses": st.session_state.generated_responses + [user_input[::-1]],
        },
    }
    return res

parameters = {
    "min_length":None,
    "max_length":100,
    "top_p":0.92,
    "temperature":1.0,
    "repetition_penalty":None,
}

options = {
    "use_cache":False,
    "wait_for_model":False
}

def on_input():
    # st.write("Input changed")
    if st.session_state.count > 0:
        user_input = st.session_state.user_input
        st.session_state.full_text += f"_User_  >>> {user_input}\n\n"
        dialog_output.markdown(st.session_state.full_text)
        st.session_state.user_input = ""

        payload = {
            "inputs": {
                "text": user_input,
                "past_user_inputs": st.session_state.past_user_inputs,
                "generated_responses": st.session_state.generated_responses,
            },
            "parameters": parameters,
            "options":options,
        }
        # result = fake_query(payload)
        result = query(payload)
        try:
            st.session_state.update(result["conversation"])
            st.session_state.full_text += f'_Chatbot_ > {result["generated_text"]}\n\n'
        except:
            st.write("D'oh! Something went wrong. Try to rerun the app.")
            st.write(result)
    st.session_state.count += 1



# init session state
if "past_user_inputs" not in st.session_state:
    st.session_state["past_user_inputs"] = []
if "generated_responses" not in st.session_state:
    st.session_state["generated_responses"] = []
if "full_text" not in st.session_state:
    st.session_state["full_text"] = ""
if "user_input" not in st.session_state:
    st.session_state["user_input"] = ""
if "count" not in st.session_state:
    st.session_state["count"] = 0

# body
st.title("Chat with Simpsons")

st.image(
    "https://raw.githubusercontent.com/arampacha/chat-with-simpsons/main/the-simpsons.png",
    caption="(c) 20th Century Fox Television",
)
if st.session_state.count == 0:
    st.write("Start dialog by inputing some text:")

dialog_output = st.empty()

if st.session_state.count > 0:
    dialog_output.markdown(st.session_state.full_text)

user_input = st.text_input(
    "> User: ",
    # value="Hey Homer! How is it going?",
    on_change=on_input(),
    key="user_input",
)

dialog_text = st.session_state.full_text
dialog_output.markdown(dialog_text)

def restart():
    st.session_state.clear()
    
st.button("Restart", on_click=st.session_state.clear)