File size: 2,212 Bytes
adcef5f
 
 
 
 
 
 
b580698
 
 
 
 
 
 
 
cefba8c
b580698
 
1921d36
b580698
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
487fc4b
b580698
 
487fc4b
b580698
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
##############################################################
#
# AAI-520 Final Project - Team 3
# University of San Diego, Shiley Marcos School of Engineering
# Copyright 2023 Tyler Foreman, Ahmed Ahmed, Tursun Alkman
#
##############################################################
import re
import json
import gradio as gr
import random 
import time 
import requests

from transformers import BertModel, BertTokenizerFast, AdamW


API_URL = "https://api-inference.huggingface.co/models/t4ai/distilbert-finetuned-t3-qa"
headers = {}

def query_model(payload):
	response = requests.post(API_URL, headers=headers, json=payload)
	return response.json()


# contruct UI using Gradio
_booted = False
with gr.Blocks() as demo:
    
    with gr.Row():
        with gr.Column(scale=1):
            context = gr.Textbox(label="Document Text", lines=25)
        with gr.Column(scale=2):
            chatbot = gr.Chatbot(label="T3Soft Bot", value=[(None, "Welcome! I am your QA assistant."), (None, "Please paste your document content in the panel to the left."), (None, "Then submit questions below!")])
            msg = gr.Textbox(label="Ask your question")
            clear = gr.ClearButton([msg, chatbot])
            _chatbot = chatbot
    
    def respond(message, context, chat_history):
        
        if(len(context) == 0):
            bot_message = "Hm, I don't see any document text, please paste in the box on the left."
        else:    
            query_bot = query_model({"inputs": {"question": message, "context": context}})
            if(len(query_bot) and ("answer" in query_bot)) and (query_bot['score'] > 0.1):
                bot_message = query_bot['answer']
            else:
                bot_message = random.choice(["I'm having trouble with this question, please try rewording and make sure it is relevant to the document.", "Hm, I'm having trouble finding the answer to that.  Can you reword the question?", "Sorry, I can't find the answer to this question."])
        
        
        chat_history.append((message, bot_message))
        time.sleep(2)
        return "", context, chat_history

    msg.submit(respond, [msg, context, chatbot], [msg, context, chatbot])

demo.launch()