Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

discord_bot.py 2.8 KB

You have to be logged in to leave a comment. Sign In
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
  1. # the os module helps us access environment variables
  2. # i.e., our API keys
  3. import os
  4. # these modules are for querying the Hugging Face model
  5. import json
  6. import requests
  7. from web_app import keep_alive
  8. # the Discord Python API
  9. import discord
  10. # this is my Hugging Face profile link
  11. API_URL = 'https://api-inference.huggingface.co/models/'
  12. class MyClient(discord.Client):
  13. def __init__(self, model_name):
  14. super().__init__()
  15. self.api_endpoint = API_URL + model_name
  16. # retrieve the secret API token from the system environment
  17. huggingface_token = os.environ['HUGGINGFACE_TOKEN']
  18. # format the header in our request to Hugging Face
  19. self.request_headers = {
  20. 'Authorization': 'Bearer {}'.format(huggingface_token)
  21. }
  22. def query(self, payload):
  23. """
  24. make request to the Hugging Face model API
  25. """
  26. data = json.dumps(payload)
  27. response = requests.post(
  28. self.api_endpoint,
  29. headers=self.request_headers,
  30. data=data)
  31. ret = json.loads(response.content.decode('utf-8'))
  32. return ret
  33. async def on_ready(self):
  34. # print out information when the bot wakes up
  35. print('Logged in as')
  36. print(self.user.name)
  37. print(self.user.id)
  38. print('------')
  39. # send a request to the model without caring about the response
  40. # just so that the model wakes up and starts loading
  41. self.query({'inputs': {'text': 'Hello!'}})
  42. async def on_message(self, message):
  43. """
  44. this function is called whenever the bot sees a message in a channel
  45. """
  46. # ignore the message if it comes from the bot itself
  47. if message.author.id == self.user.id:
  48. return
  49. # form query payload with the content of the message
  50. payload = message.content
  51. # while the bot is waiting on a response from the model
  52. # set the its status as typing for user-friendliness
  53. async with message.channel.typing():
  54. response = self.query(payload)
  55. bot_response = response['generated_text']
  56. # we may get ill-formed response if the model hasn't fully loaded
  57. # or has timed out
  58. if not bot_response:
  59. if 'error' in response:
  60. bot_response = '`Error: {}`'.format(response['error'])
  61. else:
  62. bot_response = 'Hmm... something is not right.'
  63. # send the model's response to the Discord channel
  64. await message.channel.send(bot_response)
  65. def main():
  66. # DialoGPT
  67. client = MyClient('kingabzpro/DialoGPT-small-Rick-Bot')
  68. keep_alive()
  69. client.run(os.environ['DISCORD_TOKEN'])
  70. if __name__ == '__main__':
  71. main()
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...