|
""" |
|
Credit to Derek Thomas, [email protected] |
|
""" |
|
|
|
|
|
|
|
|
|
import logging |
|
from time import perf_counter |
|
|
|
import gradio as gr |
|
from jinja2 import Environment, FileSystemLoader |
|
|
|
from gradio_app.backend.ChatGptInteractor import num_tokens_from_messages |
|
from gradio_app.backend.query_llm import generate_hf, generate_openai, construct_openai_messages |
|
from gradio_app.backend.semantic_search import table, embedder |
|
|
|
from settings import * |
|
|
|
|
|
logging.basicConfig(level=logging.INFO) |
|
logger = logging.getLogger(__name__) |
|
|
|
|
|
env = Environment(loader=FileSystemLoader('gradio_app/templates')) |
|
|
|
|
|
context_template = env.get_template('context_template.j2') |
|
context_html_template = env.get_template('context_html_template.j2') |
|
|
|
|
|
examples = [ |
|
'What is BERT?', |
|
'Tell me about BERT deep learning model', |
|
'What is the capital of China?', |
|
'Why is the sky blue?', |
|
'Who won the mens world cup in 2014?', |
|
] |
|
|
|
|
|
def add_text(history, text): |
|
history = [] if history is None else history |
|
history = history + [(text, "")] |
|
return history, gr.Textbox(value="", interactive=False) |
|
|
|
|
|
def bot(history, api_kind): |
|
top_k_rank = 5 |
|
thresh_dist = 1.2 |
|
history[-1][1] = "" |
|
query = history[-1][0] |
|
|
|
if not query: |
|
gr.Warning("Please submit a non-empty string as a prompt") |
|
raise ValueError("Empty string was submitted") |
|
|
|
logger.info('Retrieving documents...') |
|
|
|
document_start = perf_counter() |
|
|
|
query_vec = embedder.encode(query) |
|
documents = table.search(query_vec, vector_column_name=VECTOR_COLUMN_NAME).limit(top_k_rank).to_list() |
|
thresh_dist = max(thresh_dist, min(d['_distance'] for d in documents)) |
|
documents = [d for d in documents if d['_distance'] <= thresh_dist] |
|
documents = [doc[TEXT_COLUMN_NAME] for doc in documents] |
|
|
|
document_time = perf_counter() - document_start |
|
logger.info(f'Finished Retrieving documents in {round(document_time, 2)} seconds...') |
|
|
|
while len(documents) != 0: |
|
context = context_template.render(documents=documents) |
|
context_html = context_html_template.render(documents=documents) |
|
messages = construct_openai_messages(context, history) |
|
num_tokens = num_tokens_from_messages(messages, OPENAI_LLM_NAME) |
|
if num_tokens + 512 < context_lengths[OPENAI_LLM_NAME]: |
|
break |
|
documents.pop() |
|
else: |
|
raise gr.Error('Model context length exceeded, reload the page') |
|
|
|
for part in generate_openai(messages): |
|
history[-1][1] += part |
|
yield history, context_html |
|
else: |
|
print('Finished generation stream.') |
|
|
|
|
|
with gr.Blocks() as demo: |
|
with gr.Row(): |
|
with gr.Column(): |
|
chatbot = gr.Chatbot( |
|
[], |
|
elem_id="chatbot", |
|
avatar_images=('https://aui.atlassian.com/aui/8.8/docs/images/avatar-person.svg', |
|
'https://huggingface.co./datasets/huggingface/brand-assets/resolve/main/hf-logo.svg'), |
|
bubble_full_width=False, |
|
show_copy_button=True, |
|
show_share_button=True, |
|
height=600, |
|
) |
|
|
|
with gr.Row(): |
|
input_textbox = gr.Textbox( |
|
scale=3, |
|
show_label=False, |
|
placeholder="Enter text and press enter", |
|
container=False, |
|
) |
|
txt_btn = gr.Button(value="Submit text", scale=1) |
|
|
|
api_kind = gr.Radio(choices=["HuggingFace", "OpenAI"], value="OpenAI", label='Backend') |
|
|
|
|
|
gr.Examples(examples, input_textbox) |
|
|
|
with gr.Column(): |
|
context_html = gr.HTML() |
|
|
|
|
|
txt_msg = txt_btn.click( |
|
add_text, [chatbot, input_textbox], [chatbot, input_textbox], queue=False |
|
).then( |
|
bot, [chatbot, api_kind], [chatbot, context_html] |
|
) |
|
|
|
|
|
txt_msg.then(lambda: gr.Textbox(interactive=True), None, [input_textbox], queue=False) |
|
|
|
|
|
txt_msg = input_textbox.submit(add_text, [chatbot, input_textbox], [chatbot, input_textbox], queue=False).then( |
|
bot, [chatbot, api_kind], [chatbot, context_html]) |
|
|
|
|
|
txt_msg.then(lambda: gr.Textbox(interactive=True), None, [input_textbox], queue=False) |
|
|
|
demo.queue() |
|
demo.launch(debug=True) |
|
|