deepparag commited on
Commit
c193c40
1 Parent(s): 0ca4288

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -0
app.py ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import streamlit as st
3
+ from streamlit_chat import message as st_message
4
+ from transformers import AutoTokenizer, AutoModelWithLMHead
5
+ import string
6
+ import random
7
+ import requests
8
+ import os
9
+
10
+ @st.experimental_singleton
11
+ def get_models():
12
+ # it may be necessary for other frameworks to cache the model
13
+ # seems pytorch keeps an internal state of the conversation
14
+ model_name = "deepparag/Aeona"
15
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
16
+ model = AutoModelWithLMHead.from_pretrained(model_name)
17
+ return tokenizer, model
18
+
19
+
20
+ if "history" not in st.session_state:
21
+ st.session_state.history = []
22
+ st.session_state.aimlId="huggingface_space_"+''.join(random.choices(string.ascii_uppercase +
23
+ string.digits, k = 10))
24
+
25
+
26
+
27
+
28
+ def generate_answer():
29
+ user_message = st.session_state.input_text
30
+
31
+ response=requests.post(os.environ['AIML']+ f"?test=test&id={st.session_state.aimlId}&text={st.session_state.input_text}" ).content.decode("utf-8");
32
+ if str(response).find("idk") == -1 and response.find("<oob>")==-1 and response.find("Something is wrong with my")==-1 and str(response).find("AIML") == -1 and str(response).find("Index") == -1 and str(response).find("<html>") == -1:
33
+ message_bot= str(response).replace("<br/>", "\n")
34
+ else:
35
+ tokenizer, model = get_models()
36
+
37
+ inputs = tokenizer(st.session_state.input_text+ tokenizer.eos_token, return_tensors="pt")
38
+ result = model.generate(**inputs, max_length=1000,
39
+ pad_token_id=tokenizer.eos_token_id,
40
+ no_repeat_ngram_size=4,
41
+ do_sample=True,
42
+ top_k=100,
43
+ top_p=0.7,
44
+ temperature=0.8)
45
+ message_bot = tokenizer.decode(
46
+ result[0], skip_special_tokens=True
47
+ )
48
+
49
+ st.session_state.history.append({"message": user_message, "is_user": True})
50
+ st.session_state.history.append({"message": message_bot, "is_user": False})
51
+
52
+ st.title("Talk with the Aeona!")
53
+ st.write("Aeona hopes to become an AI which as human as possible with goal of becoming your friend.")
54
+ st.write("To do this we hope to combine a AI which uses the dialoggpt-2 framework and discord messages")
55
+ st.write("The input will start out by going to an AIML chatbot based on a modified version of ALICE")
56
+ st.write("If the AIML has not valid answer it will proceed to use the AI")
57
+ st.write("The ai right now is focused mainly on discord and you can invite the bot here: https://www.aeona.xyz/")
58
+
59
+ st.text_input("Chat with Aeona", key="input_text", on_change=generate_answer)
60
+
61
+ for chat in st.session_state.history:
62
+ st_message(**chat) # unpacking
63
+
64
+