-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
20 lines (20 loc) · 1.74 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import requests,flask,sys;app=flask.Flask(__name__);all_text='';talk=None
class API(object):
def __init__(self, access_key,endpoint="https://api.intellivoid.net/coffeehouse"):
if isinstance(access_key, API):self.access_key = access_key.access_key;self.endpoint = access_key.endpoint
else:self.access_key = access_key;self.endpoint = endpoint
def _send(self, path, access_key=True, **payload):payload["access_key"] = self.access_key;exec('global final;final='+requests.post("{}/{}".format(self.endpoint, path),payload).text.replace('true','True').replace('false','False'));return final["results"]
class Session:
def __init__(self, data, client):self._client = client;self.id = data["session_id"];self.language = data["language"];self.available = data["available"];self.expires = data["expires"]
def think(self, text):return self._client.think(self.id, text)
def __str__(self):return self.id
class AI(API):
def __init__(self, *args, **kwargs):super().__init__(*args, **kwargs)
def create_session(self, language="en"):return Session(self._send("v1/lydia/session/create",target_language=language), self)
def get_session(self, session_id):return Session(self._send("v1/lydia/session/get",session_id=session_id), self)
def think(self, session_id, text):return self._send("v1/lydia/session/think",session_id=session_id,input=text)["output"]
@app.route('/')
def hello():global talk;talk=AI(sys.argv[1]).create_session();return flask.render_template('index.html')
@app.route('/', methods=['POST'])
def index_post():global all_text;text=flask.request.form['text'];output=talk.think(text);all_text+='You said: '+text+'\n\n'+'Bot said: '+output+'\n\n';return flask.render_template('index.html',text=all_text)
if __name__ == '__main__':app.run()