Millakisan commited on
Commit
3741ac2
·
1 Parent(s): 70bb3e8

First Commit - Create app.py file

Browse files
Files changed (1) hide show
  1. src/app.py +35 -0
src/app.py CHANGED
@@ -1 +1,36 @@
1
  #
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  #
2
+ from telegram import Update
3
+ from telegram.ext import Updater, CommandHandler, CallbackContext, MessageHandler, Filters
4
+ import requests
5
+ from telegram import ChatAction
6
+ import os
7
+
8
+ def hello(update: Update, context: CallbackContext) -> None:
9
+ intro_text = """
10
+ 🤖 Greetings human! \n
11
+ 🤗 I'm a bot hosted on Hugging Face Spaces. \n
12
+ 🦾 I can query the mighty GPT-J-6B model and send you a response here. Try me.\n
13
+ ✉️ Send me a text to start and I shall generate a response to complete your text!\n\n
14
+ ‼️ PS: Responses are not my own (everything's from GPT-J-6B). I'm not conscious (yet).\n
15
+ Blog post: https://dicksonneoh.com/portfolio/deploy_gpt_hf_models_on_telegram/
16
+ """
17
+ update.message.reply_text(intro_text)
18
+
19
+ def get_gpt_response(text):
20
+ r = requests.post(
21
+ url="https://hf.space/embed/dnth/gpt-j-6B/+/api/predict/",
22
+ json={"data": [text]},
23
+ )
24
+ response = r.json()
25
+ return response["data"][0]
26
+
27
+ def respond_to_user(update: Update, context: CallbackContext):
28
+ update.message.chat.send_action(action=ChatAction.TYPING)
29
+ response_text = get_gpt_response(update.message.text)
30
+ update.message.reply_text(response_text)
31
+
32
+ updater = Updater('YOUR-TOKEN')
33
+ updater.dispatcher.add_handler(CommandHandler("start", hello))
34
+ updater.dispatcher.add_handler(MessageHandler(Filters.text & ~Filters.command, respond_to_user))
35
+ updater.start_polling()
36
+ updater.idle()