-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
97 lines (80 loc) · 3.09 KB
/
main.py
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
# the os module helps us access environment variables
# i.e., our API keys
import os
from flask import Flask
from threading import Thread
# these modules are for querying the Hugging Face model
import json
import requests
# the Discord Python API
import discord
# this is my Hugging Face profile link
API_URL = 'https://api-inference.huggingface.co/models/Sammith/'
class MyClient(discord.Client):
def __init__(self, model_name):
super().__init__()
self.api_endpoint = API_URL + model_name
# retrieve the secret API token from the system environment
huggingface_token = os.environ['HUGGINGFACE_TOKEN']
# format the header in our request to Hugging Face
self.request_headers = {
'Authorization': 'Bearer {}'.format(huggingface_token)
}
def query(self, payload):
"""
make request to the Hugging Face model API
"""
data = json.dumps(payload)
response = requests.request('POST',
self.api_endpoint,
headers=self.request_headers,
data=data)
ret = json.loads(response.content.decode('utf-8'))
return ret
async def on_ready(self):
# print out information when the bot wakes up
print('Logged in as')
print(self.user.name)
print(self.user.id)
print('------')
# send a request to the model without caring about the response
# just so that the model wakes up and starts loading
self.query({'inputs': {'text': 'Hello!'}})
async def on_message(self, message):
"""
this function is called whenever the bot sees a message in a channel
"""
# ignore the message if it comes from the bot itself
if message.author.id == self.user.id:
return
if message.content.startswith('$m'):
# form query payload with the content of the message
payload = {'inputs': {'text': message.content[2:]}}
# while the bot is waiting on a response from the model
# set the its status as typing for user-friendliness
async with message.channel.typing():
response = self.query(payload)
bot_response = response.get('generated_text', None)
# we may get ill-formed response if the model hasn't fully loaded
# or has timed out
if not bot_response:
bot_response = 'explain it to me like i am 5 year old(booting up, wait a few seconds)'
# send the model's response to the Discord channel
await message.channel.send("$d "+bot_response)
app = Flask('')
@app.route('/')
def home():
return "Hello. I am alive!"
def run():
app.run(host='0.0.0.0',port=8080)
def keep_alive():
t = Thread(target=run)
t.start()
def main():
# DialoGPT-medium-joshua is my model name
client = MyClient('DialoGPT-small-miachael')
# retrieve the secret API token from the system environment
keep_alive()
client.run(os.environ['DISCORD_TOKEN'])
if __name__ == '__main__':
main()