""" deploy-as-bot\gradio_chatbot.py A system, method for deploying to Gradio. Gradio is a basic "deploy" interface which allows for other users to test your model from a web URL. It also enables some basic functionality like user flagging for weird responses. Note that the URL is displayed once the script is run. Set the working directory to */deploy-as-bot in terminal before running. """ import os import sys from os.path import dirname sys.path.append(dirname(dirname(os.path.abspath(__file__)))) import gradio as gr import logging import argparse import time import warnings from pathlib import Path from cleantext import clean from transformers import pipeline from datetime import datetime from ai_single_response import query_gpt_model #from gradio.networking import get_state, set_state from flask import Flask, request, session, jsonify, abort, send_file, render_template, redirect import nltk nltk.download('stopwords') warnings.filterwarnings(action="ignore", message=".*gradient_checkpointing*") logging.basicConfig() cwd = Path.cwd() my_cwd = str(cwd.resolve()) # string so it can be passed to os.path() objects def gramformer_correct(corrector, qphrase: str): """ gramformer_correct - correct a string using a text2textgen pipeline model from transformers Args: corrector (transformers.pipeline): [transformers pipeline object, already created w/ relevant model] qphrase (str): [text to be corrected] Returns: [str]: [corrected text] """ try: corrected = corrector( clean(qphrase), return_text=True, clean_up_tokenization_spaces=True ) return corrected[0]["generated_text"] except: print("NOTE - failed to correct with gramformer") return clean(qphrase) def ask_gpt(message: str, sender: str = ""): """ ask_gpt - queries the relevant model with a prompt message and (optional) speaker name Args: message (str): prompt message to respond to sender (str, optional): speaker aka who said the message. Defaults to "". Returns: [str]: [model response as a string] """ st = time.time() prompt = clean(message) # clean user input prompt = prompt.strip() # get rid of any extra whitespace if len(prompt) > 200: prompt = prompt[-200:] # truncate sender = clean(sender.strip()) if len(sender) > 2: try: prompt_speaker = clean(sender) except: # there was some issue getting that info, whatever prompt_speaker = None else: prompt_speaker = None resp = query_gpt_model( folder_path=model_loc, prompt_msg=prompt, speaker=prompt_speaker, kparam=150, temp=0.75, top_p=0.65, # optimize this with hyperparam search ) bot_resp = gramformer_correct(corrector, qphrase=resp["out_text"]) rt = round(time.time() - st, 2) print(f"took {rt} sec to respond") return bot_resp def chat(first_and_last_name, message): """ chat - helper function that makes the whole gradio thing work. Args: first_and_last_name (str or None): [speaker of the prompt, if provided] message (str): [description] Returns: [str]: [returns an html string to display] """ history = [] response = ask_gpt(message, sender=first_and_last_name) history.append((f"{first_and_last_name}: " + message, " GPT-Model: " + response)) #+ " [end] ")) #html = "