arampacha commited on
Commit
49eede3
1 Parent(s): a787ef3
Files changed (2) hide show
  1. README.md +1 -1
  2. app.py +103 -0
README.md CHANGED
@@ -1,6 +1,6 @@
1
  ---
2
  title: Chat With Simpsons
3
- emoji: 📉
4
  colorFrom: green
5
  colorTo: gray
6
  sdk: streamlit
1
  ---
2
  title: Chat With Simpsons
3
+ emoji: 🍩
4
  colorFrom: green
5
  colorTo: gray
6
  sdk: streamlit
app.py ADDED
@@ -0,0 +1,103 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import json
3
+ import requests
4
+
5
+ import time
6
+
7
+
8
+ API_TOKEN = "api_kqhWITTFmwSnwjsssxKtRxwSDOuPzHphHD"
9
+ headers = {"Authorization": f"Bearer {API_TOKEN}"}
10
+ API_URL = "https://api-inference.huggingface.co/models/arampacha/DialoGPT-medium-simpsons"
11
+
12
+
13
+ def query(payload):
14
+ data = json.dumps(payload)
15
+ response = requests.request("POST", API_URL, headers=headers, data=data)
16
+ return json.loads(response.content.decode("utf-8"))
17
+
18
+ def fake_query(payload):
19
+ user_input = payload["inputs"]["text"]
20
+ time.sleep(1)
21
+ res = {
22
+ "generated_text": user_input[::-1],
23
+ "conversation":{
24
+ "past_user_inputs": st.session_state.past_user_inputs + [user_input],
25
+ "generated_responses": st.session_state.generated_responses + [user_input[::-1]],
26
+ },
27
+ }
28
+ return res
29
+
30
+ parameters = {
31
+ "min_length":None,
32
+ "max_length":50,
33
+ "top_p":0.92,
34
+ "repetition_penalty":None,
35
+ }
36
+
37
+ # options = {}
38
+
39
+ def on_input():
40
+ # st.write("Input changed")
41
+ if st.session_state.count > 0:
42
+ user_input = st.session_state.user_input
43
+ st.session_state.full_text += f"_User_ >>> {user_input}\n\n"
44
+ dialog_output.markdown(st.session_state.full_text)
45
+ st.session_state.user_input = ""
46
+
47
+ payload = {
48
+ "inputs": {
49
+ "text": user_input,
50
+ "past_user_inputs": st.session_state.past_user_inputs,
51
+ "generated_responses": st.session_state.generated_responses,
52
+ },
53
+ "parameters": parameters,
54
+ }
55
+ # result = fake_query(payload)
56
+ result = query(payload)
57
+ st.session_state.update(result["conversation"])
58
+ st.session_state.full_text += f'_Chatbot_ > {result["generated_text"]}\n\n'
59
+ st.session_state.count += 1
60
+
61
+
62
+
63
+ # init session state
64
+ if "past_user_inputs" not in st.session_state:
65
+ st.session_state["past_user_inputs"] = []
66
+ if "generated_responses" not in st.session_state:
67
+ st.session_state["generated_responses"] = []
68
+ if "full_text" not in st.session_state:
69
+ st.session_state["full_text"] = ""
70
+ if "user_input" not in st.session_state:
71
+ st.session_state["user_input"] = ""
72
+ if "count" not in st.session_state:
73
+ st.session_state["count"] = 0
74
+
75
+ # body
76
+ st.title("Chat with Simpsons")
77
+
78
+ st.image(
79
+ "https://raw.githubusercontent.com/arampacha/chat-with-simpsons/main/the-simpsons.png",
80
+ caption="(c) 20th Century Fox Television",
81
+ )
82
+ if st.session_state.count == 0:
83
+ st.write("Start dialog by inputing some text:")
84
+
85
+ dialog_output = st.empty()
86
+
87
+ if st.session_state.count > 0:
88
+ dialog_output.markdown(st.session_state.full_text)
89
+
90
+ user_input = st.text_input(
91
+ "> User: ",
92
+ # value="Hey Homer! How is it going?",
93
+ on_change=on_input(),
94
+ key="user_input",
95
+ )
96
+
97
+ dialog_text = st.session_state.full_text
98
+ dialog_output.markdown(dialog_text)
99
+
100
+ def restart():
101
+ st.session_state.clear()
102
+
103
+ st.button("Restart", on_click=st.session_state.clear)