import os from typing import Optional, Tuple import gradio as gr import pickle from query_data import get_chain from threading import Lock from ingest_data import get_vectorstore 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: vs = get_vectorstore("vectorstore.pkl") with open(vs, "rb") as f: vectorstore = pickle.load(f) history = history or [] chain = get_chain(vectorstore) # If chain is None, that is because no API key was provided. if chain is None: history.append((inp, "Please paste your OpenAI key to use")) return history, history # Set OpenAI key import openai openai.api_key = os.environ["OPENAI_API_KEY"] # Run chain and append input. 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 = "