ptb-gpt / app.py
dnth's picture
Update app.py
dc3374c
raw history blame
No virus
1.39 kB
from telegram import Update
from telegram.ext import Updater, CommandHandler, CallbackContext, MessageHandler, Filters
import requests
from telegram import ChatAction
import os
def get_gpt_response(text):
r = requests.post(
url="https://hf.space/embed/akhaliq/gpt-j-6B/+/api/predict/",
json={"data": [text]},
)
response = r.json()
return response["data"][0]
def hello(update: Update, context: CallbackContext) -> None:
intro_text = """
πŸ€– Greetings human! \n
πŸ€— I'm a bot hosted on Hugging Face Spaces. \n
πŸ’ͺ I can query the GPT-J-6B model by Ahsen Khaliq at https://huggingface.co/spaces/akhaliq/gpt-j-6B.\n
βœ‰οΈ Send me a text and I shall respond!\n\n
‼️ PS: Responses are not my own (everything's from GPT-J-6B). I'm not conscious (yet).
"""
update.message.reply_text(intro_text)
def send_to_gpt(update: Update, context: CallbackContext):
update.message.chat.send_action(action=ChatAction.TYPING)
response_text = get_gpt_response(update.message.text)
update.message.reply_text(response_text)
updater = Updater(os.environ['telegram_api_key'])
#updater = Updater("5340209718:AAE8bN38hdONQ9AaTnTmz5e_fvZo00XA6Os")
updater.dispatcher.add_handler(CommandHandler("start", hello))
updater.dispatcher.add_handler(MessageHandler(Filters.text & ~Filters.command, send_to_gpt))
updater.start_polling()
updater.idle()