CoolFace
Apppublic

looliipop/Course_Advisor

sourceHugging Faceupdated 3y agoView on Hugging Face
0likes
app.py64 linesDownload Raw Back to root
1from telegram.ext import Updater, MessageHandler, Filters2import telegram3import openai4from moviepy.editor import AudioFileClip5import better_profanity as profanity_filter6import streamlit as st7 8openai.api_key = "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"9TELEGRAM_API_TOKEN = "6570541136:AAFBFbAyPlraUgaKCFsD4QXHML29ZgdlfTU"10 11messages = [{"role": "system", "content": "You are bot for Thapar Institute of Engineering and Technology (TIET) to assist students. You are always concise and polite in its answers. You reply in negative to the topics which does not concern TIET. You do not engage in general topics that are unrelated to thapar."} , {"role": "user", "content":"Tell me about cats"} ,  {"role": "assistant", "content":"I apologize, but I can only provide information related to Thapar University. Please ask a question about the university."}]12 13def is_content_safe(content):14    # Use better_profanity to check content safety15    censored_content = profanity_filter.profanity.censor(content)16    return censored_content == content17 18def text_message(update, context):19    user_text = update.message.text20    # Check if the user's text is safe21    if not is_content_safe(user_text):22        update.message.reply_text("Your message contains inappropriate content and cannot be processed.")23        return24 25    messages.append({"role": "user", "content": user_text})26    response = openai.ChatCompletion.create(27        model="gpt-3.5-turbo",28        messages=messages29    )30    ChatGPT_reply = response["choices"][0]["message"]["content"]31    update.message.reply_text(text=f"*[Bot]:* {ChatGPT_reply}", parse_mode=telegram.ParseMode.MARKDOWN)32    messages.append({"role": "assistant", "content": ChatGPT_reply})33 34def voice_message(update, context):35    update.message.reply_text("Processing your voice message....)")36    voice_file = context.bot.getFile(update.message.voice.file_id)37    voice_file.download("voice_message.ogg")38    audio_clip = AudioFileClip("voice_message.ogg")39    audio_clip.write_audiofile("voice_message.mp3")40    audio_file = open("voice_message.mp3", "rb")41    transcript = openai.Audio.transcribe("whisper-1", audio_file).text42 43    # Check if the user's transcript is safe44    if not is_content_safe(transcript):45        update.message.reply_text("Your voice message contains inappropriate content and cannot be processed.")46        return47 48    update.message.reply_text(text=f"*You :* _{transcript}_", parse_mode=telegram.ParseMode.MARKDOWN)49    messages.append({"role": "user", "content": transcript})50    response = openai.ChatCompletion.create(51        model="gpt-3.5-turbo",52        messages=messages53    )54    ChatGPT_reply = response["choices"][0]["message"]["content"]55    update.message.reply_text(text=f"*TIET Bot :* {ChatGPT_reply}", parse_mode=telegram.ParseMode.MARKDOWN)56    messages.append({"role": "assistant", "content": ChatGPT_reply})57 58updater = Updater(TELEGRAM_API_TOKEN, use_context=True)59dispatcher = updater.dispatcher60dispatcher.add_handler(MessageHandler(Filters.text & (~Filters.command), text_message))61dispatcher.add_handler(MessageHandler(Filters.voice, voice_message))62updater.start_polling()63updater.idle()64