|
import os |
|
from typing import Optional, Tuple |
|
|
|
import cfg |
|
import gradio as gr |
|
import pandas as pd |
|
from cfg import setup_buster |
|
|
|
from buster.completers import Completion |
|
from buster.utils import extract_zip |
|
|
|
|
|
if os.getenv("OPENAI_API_KEY") is None: |
|
print("Warning: No openai key detected. You can set it with 'export OPENAI_API_KEY=sk-...'.") |
|
|
|
|
|
ChatHistory = list[list[Optional[str], Optional[str]]] |
|
|
|
extract_zip("deeplake_store.zip", "deeplake_store") |
|
|
|
buster = setup_buster(cfg.buster_cfg) |
|
|
|
|
|
def add_user_question(user_question: str, chat_history: Optional[ChatHistory] = None) -> ChatHistory: |
|
"""Adds a user's question to the chat history. |
|
|
|
If no history is provided, the first element of the history will be the user conversation. |
|
""" |
|
if chat_history is None: |
|
chat_history = [] |
|
chat_history.append([user_question, None]) |
|
return chat_history |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def chat(chat_history: ChatHistory) -> Tuple[ChatHistory, Completion]: |
|
"""Answer a user's question using retrieval augmented generation.""" |
|
|
|
|
|
user_input = chat_history[-1][0] |
|
|
|
|
|
completion = buster.process_input(user_input) |
|
|
|
|
|
chat_history[-1][1] = "" |
|
for token in completion.answer_generator: |
|
chat_history[-1][1] += token |
|
|
|
yield chat_history, completion |
|
|
|
|
|
demo = gr.Blocks() |
|
|
|
with demo: |
|
with gr.Row(): |
|
gr.Markdown("<h3><center>Parrot π¦: A Question-Answering Bot for π« MOOSE documentation </center></h3>") |
|
|
|
|
|
chatbot = gr.Chatbot() |
|
|
|
with gr.Row(): |
|
question_textbox = gr.Textbox( |
|
label="What's your question?", |
|
placeholder="Type your question here...", |
|
lines=1, |
|
) |
|
send_button = gr.Button(value="Send", variant="secondary") |
|
|
|
examples = gr.Examples( |
|
examples=[ |
|
"How can I use PorousFlow?", |
|
"How do I use displaced mesh?", |
|
"Why is my application not converging?", |
|
], |
|
inputs=question_textbox, |
|
) |
|
|
|
gr.Markdown("This application uses GPT to search the docs for relevant info and answer questions.") |
|
gr.Markdown("Find the full MOOSE documentation [here](https://mooseframework.inl.gov/index.html).") |
|
gr.HTML("οΈ<center> Created with @aikubo") |
|
|
|
response = gr.State() |
|
|
|
|
|
gr.on( |
|
triggers=[send_button.click, question_textbox.submit], |
|
fn=add_user_question, |
|
inputs=[question_textbox], |
|
outputs=[chatbot] |
|
).then( |
|
chat, |
|
inputs=[chatbot], |
|
outputs=[chatbot, response] |
|
) |
|
|
|
|
|
|
|
demo.queue() |
|
demo.launch(debug=True, share=True) |
|
|