|
import os |
|
from typing import Optional, Tuple |
|
|
|
import gradio as gr |
|
import pickle |
|
from query_data import get_chain |
|
from threading import Lock |
|
|
|
with open("vectorstore.pkl", "rb") as f: |
|
vectorstore = pickle.load(f) |
|
|
|
|
|
|
|
class ChatWrapper: |
|
|
|
def __init__(self): |
|
self.lock = Lock() |
|
def __call__( |
|
self, inp: str, history: Optional[Tuple[str, str]],chain |
|
): |
|
"""Execute the chat functionality.""" |
|
self.lock.acquire() |
|
try: |
|
history = history or [] |
|
chain = get_chain(vectorstore) |
|
|
|
if chain is None: |
|
history.append((inp, "Please paste your OpenAI key to use")) |
|
return history, history |
|
|
|
import openai |
|
|
|
openai.api_key = os.environ["OPENAI_API_KEY"] |
|
|
|
output = chain({"question": inp, "chat_history": history})["answer"] |
|
history.append((inp, output)) |
|
except Exception as e: |
|
raise e |
|
finally: |
|
self.lock.release() |
|
return history, history |
|
|
|
|
|
chat = ChatWrapper() |
|
|
|
block = gr.Blocks(css=".gradio-container {background-color: #04A7E3}") |
|
|
|
image_url = "https://www.thebotforge.io/wp-content/uploads/2022/01/the-bot-forge-logo-100px.png" |
|
html = "<center><img src='%s' width='300'></center>" % image_url |
|
with block: |
|
with gr.Row(): |
|
gr.HTML(html) |
|
|
|
with gr.Row(): |
|
gr.Markdown( |
|
"<h2><center>The Bot Forge</h2></center><h3> <center></center></h3>") |
|
|
|
chatbot = gr.Chatbot() |
|
|
|
with gr.Row(): |
|
message = gr.Textbox( |
|
label="What's your question?", |
|
placeholder="Ask questions about the conversational AI and The Bot Forge", |
|
lines=1, |
|
) |
|
submit = gr.Button(value="Send", variant="secondary").style(full_width=False) |
|
|
|
gr.Examples( |
|
examples=[ |
|
"what is a chatbot", |
|
"what is conversation design", |
|
"does the bot forge have expertise in this", |
|
"how much does it cost to create a chatbot", |
|
"what are some of the channels you can deploy to?", |
|
"explain nlu", |
|
"does the bot forge have experience using dialogflow", |
|
"can a chatbot be used as a sales assistant" |
|
|
|
], |
|
inputs=message, |
|
) |
|
|
|
state = gr.State() |
|
agent_state = gr.State() |
|
|
|
submit.click(chat, inputs=[message, state, agent_state], outputs=[chatbot, state]) |
|
|
|
|
|
def echo(name, request: gr.Request): |
|
if request: |
|
print("Request headers dictionary:", request.headers) |
|
print("IP address:", request.client.host) |
|
print("Body", request.body) |
|
return name |
|
|
|
|
|
block.launch(debug=True) |
|
|