Skip to content

Commit

Permalink
Refactor codebase
Browse files Browse the repository at this point in the history
  • Loading branch information
ihumaunkabir committed Jul 24, 2024
1 parent aa236bc commit f157a1c
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 25 deletions.
Binary file modified .DS_Store
Binary file not shown.
Binary file modified __pycache__/commands.cpython-311.pyc
Binary file not shown.
Binary file modified __pycache__/config.cpython-311.pyc
Binary file not shown.
Binary file modified __pycache__/handler.cpython-311.pyc
Binary file not shown.
Binary file modified __pycache__/processchat.cpython-311.pyc
Binary file not shown.
4 changes: 2 additions & 2 deletions commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
from telegram.ext import ContextTypes

async def start_command(update: Update, context: ContextTypes.DEFAULT_TYPE):
await update.message.reply_text('Welcome the bot, start your conversation by uploading file, image or send text.')
await update.message.reply_text('Hello Chottobondhu, ask me anything to start conversation!')

async def help_command(update: Update, context: ContextTypes.DEFAULT_TYPE):
await update.message.reply_text('You can have datapoints from a pdf/image easily at your hands.')
await update.message.reply_text('You need to ask through messages, please wait a bit for response.')

async def info_command(update: Update, context: ContextTypes.DEFAULT_TYPE):
await update.message.reply_text('Get information at your convenience.')
30 changes: 17 additions & 13 deletions handler.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
import json
import requests
from config import APIURL, AUTH, DATAPOINT, VLLM, PROJECTID
from config import APIURL, VLLM, BEARER


def handle_resp(text: str) -> str:
text = text.lower()
if 'hello' in text:
return 'Hello'
elif 'thank' in text:
return 'great.'
headers = {"Authorization": BEARER}
respurl = requests.post(APIURL, timeout=120,headers=headers, data=json.dumps({"stream":False,"prompt": text,"model":VLLM, "temperature":0.4}))
# print(f'API response is: {respurl.text}')
json_obj = json.loads(respurl.text)
resptext = json_obj['response']

print(respurl.status_code)
if respurl.status_code == 200:
return resptext
else:
return 'I do not understand.'
return 'Unfortunately I am offline, thanks you.'

def handle_file(path):
print(path)
# http = urllib3.PoolManager()
Expand All @@ -21,9 +25,9 @@ def handle_file(path):
# json_obj = json.loads(string)
# resptext = "Jokes: " + json_obj['punchline'] + "\n" + "Type: " + json_obj['type'] + " "

respurl = requests.post(APIURL, headers={"Content-Type":"application/json","Authorization": AUTH}, data=json.dumps({"params":{"file_url": path,"data_points": DATAPOINT,"llm_choice":VLLM},"project": PROJECTID}))
print(respurl.text)
json_obj = json.loads(respurl.text)
resptext = json_obj['output']['data']
# respurl = requests.post(APIURL, headers={"Content-Type":"application/json","Authorization": AUTH}, data=json.dumps({"params":{"file_url": path,"data_points": DATAPOINT,"llm_choice":VLLM},"project": PROJECTID}))
# print(respurl.text)
# json_obj = json.loads(respurl.text)
# resptext = json_obj['output']['data']

return resptext
# return resptext
12 changes: 6 additions & 6 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
from config import TOKEN
from config import BOT_TOKEN
from errorhandler import error
from commands import start_command, help_command, info_command
from processchat import handle_message, process_file, process_image
from telegram.ext import Application, CommandHandler, MessageHandler, filters
from telegram.ext import Application, CommandHandler, MessageHandler, filters # type: ignore


if __name__ == '__main__':
print('Bot started...')
app = Application.builder().token(TOKEN).build()
app = Application.builder().token(BOT_TOKEN).build()
app.add_handler(CommandHandler('start', start_command))
app.add_handler(CommandHandler('help', help_command))
app.add_handler(CommandHandler('info', info_command))
app.add_handler(MessageHandler(filters.PHOTO, process_image))
app.add_handler(MessageHandler(filters.Document.PDF, process_file))
# app.add_handler(CommandHandler('info', info_command))
# app.add_handler(MessageHandler(filters.PHOTO, process_image))
# app.add_handler(MessageHandler(filters.Document.PDF, process_file))
app.add_handler(MessageHandler(filters.TEXT, handle_message))
app.add_error_handler(error)

Expand Down
6 changes: 2 additions & 4 deletions processchat.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from config import BOT_USER
from handler import handle_file, handle_resp
from telegram import Update
from telegram.ext import ContextTypes
from telegram import Update # type: ignore
from telegram.ext import ContextTypes # type: ignore


async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE):
Expand All @@ -13,8 +13,6 @@ async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE):
if BOT_USER in text:
new_text: str = text.replace(BOT_USER, '').strip()
response: str = handle_resp(new_text)
else:
return
else:
response: str = handle_resp(text)

Expand Down

0 comments on commit f157a1c

Please sign in to comment.