dnth commited on
Commit
51987fa
1 Parent(s): 9a21e7a

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -0
app.py ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from telegram import Update
2
+ from telegram.ext import Updater, CommandHandler, CallbackContext, MessageHandler, Filters
3
+ import requests
4
+ from telegram import ChatAction
5
+
6
+ def get_gpt_response(text):
7
+ r = requests.post(
8
+ url="https://hf.space/embed/akhaliq/gpt-j-6B/+/api/predict/",
9
+ json={"data": [text]},
10
+ )
11
+ response = r.json()
12
+ return response["data"][0]
13
+
14
+
15
+ def hello(update: Update, context: CallbackContext) -> None:
16
+ intro_text = """
17
+ 🤖 Greetings human! \n
18
+ 🤗 I'm a bot hosted on Hugging Face Spaces. \n
19
+ 💪 I can query the GPT-J-6B model by Ahsen Khaliq at https://huggingface.co/spaces/akhaliq/gpt-j-6B.\n
20
+ ✉️ Send me a text and I shall respond!\n\n
21
+ ‼️ PS: Responses are not my own (everything's from GPT-J-6B). I'm not conscious (yet).
22
+ """
23
+ update.message.reply_text(intro_text)
24
+
25
+
26
+ def send_to_gpt(update: Update, context: CallbackContext):
27
+ update.message.chat.send_action(action=ChatAction.TYPING)
28
+ response_text = get_gpt_response(update.message.text)
29
+ update.message.reply_text(response_text)
30
+
31
+
32
+
33
+ updater = Updater("5340209718:AAE8bN38hdONQ9AaTnTmz5e_fvZo00XA6Os")
34
+
35
+ updater.dispatcher.add_handler(CommandHandler("start", hello))
36
+
37
+ updater.dispatcher.add_handler(MessageHandler(Filters.text & ~Filters.command, send_to_gpt))
38
+
39
+ updater.start_polling()
40
+ updater.idle()