Commit 
							
							·
						
						ad03ba1
	
1
								Parent(s):
							
							6dc8b5a
								
Update app.py
Browse files
    	
        app.py
    CHANGED
    
    | 
         @@ -10,36 +10,27 @@ with open("vectorstore.pkl", "rb") as f: 
     | 
|
| 10 | 
         
             
                vectorstore = pickle.load(f)
         
     | 
| 11 | 
         | 
| 12 | 
         | 
| 13 | 
         
            -
            def set_openai_api_key(api_key: str):
         
     | 
| 14 | 
         
            -
                """Set the api key and return chain.
         
     | 
| 15 | 
         
            -
                If no api_key, then None is returned.
         
     | 
| 16 | 
         
            -
                """
         
     | 
| 17 | 
         
            -
                if api_key:
         
     | 
| 18 | 
         
            -
                    os.environ["OPENAI_API_KEY"] = api_key
         
     | 
| 19 | 
         
            -
                    chain = get_chain(vectorstore)
         
     | 
| 20 | 
         
            -
                    os.environ["OPENAI_API_KEY"] = ""
         
     | 
| 21 | 
         
            -
                    return chain
         
     | 
| 22 | 
         
            -
             
     | 
| 23 | 
         | 
| 24 | 
         
             
            class ChatWrapper:
         
     | 
| 25 | 
         | 
| 26 | 
         
             
                def __init__(self):
         
     | 
| 27 | 
         
             
                    self.lock = Lock()
         
     | 
| 28 | 
         
            -
             
     | 
| 29 | 
         
             
                def __call__(
         
     | 
| 30 | 
         
            -
                    self,  
     | 
| 31 | 
         
             
                ):
         
     | 
| 32 | 
         
             
                    """Execute the chat functionality."""
         
     | 
| 33 | 
         
             
                    self.lock.acquire()
         
     | 
| 34 | 
         
             
                    try:
         
     | 
| 35 | 
         
             
                        history = history or []
         
     | 
| 
         | 
|
| 36 | 
         
             
                        # If chain is None, that is because no API key was provided.
         
     | 
| 37 | 
         
             
                        if chain is None:
         
     | 
| 38 | 
         
             
                            history.append((inp, "Please paste your OpenAI key to use"))
         
     | 
| 39 | 
         
             
                            return history, history
         
     | 
| 40 | 
         
             
                        # Set OpenAI key
         
     | 
| 41 | 
         
             
                        import openai
         
     | 
| 42 | 
         
            -
                         
     | 
| 
         | 
|
| 43 | 
         
             
                        # Run chain and append input.
         
     | 
| 44 | 
         
             
                        output = chain({"question": inp, "chat_history": history})["answer"]
         
     | 
| 45 | 
         
             
                        history.append((inp, output))
         
     | 
| 
         @@ -64,13 +55,6 @@ with block: 
     | 
|
| 64 | 
         
             
                    gr.Markdown(
         
     | 
| 65 | 
         
             
                        "<h2><center>The Bot Forge</h2></center><h3> <center></center></h3>")
         
     | 
| 66 | 
         | 
| 67 | 
         
            -
                    openai_api_key_textbox = gr.Textbox(
         
     | 
| 68 | 
         
            -
                        placeholder="Paste your OpenAI API key (sk-...)",
         
     | 
| 69 | 
         
            -
                        show_label=False,
         
     | 
| 70 | 
         
            -
                        lines=1,
         
     | 
| 71 | 
         
            -
                        type="password",
         
     | 
| 72 | 
         
            -
                    )
         
     | 
| 73 | 
         
            -
             
     | 
| 74 | 
         
             
                chatbot = gr.Chatbot()
         
     | 
| 75 | 
         | 
| 76 | 
         
             
                with gr.Row():
         
     | 
| 
         | 
|
| 10 | 
         
             
                vectorstore = pickle.load(f)
         
     | 
| 11 | 
         | 
| 12 | 
         | 
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 13 | 
         | 
| 14 | 
         
             
            class ChatWrapper:
         
     | 
| 15 | 
         | 
| 16 | 
         
             
                def __init__(self):
         
     | 
| 17 | 
         
             
                    self.lock = Lock()
         
     | 
| 
         | 
|
| 18 | 
         
             
                def __call__(
         
     | 
| 19 | 
         
            +
                    self, inp: str, history: Optional[Tuple[str, str]],chain
         
     | 
| 20 | 
         
             
                ):
         
     | 
| 21 | 
         
             
                    """Execute the chat functionality."""
         
     | 
| 22 | 
         
             
                    self.lock.acquire()
         
     | 
| 23 | 
         
             
                    try:
         
     | 
| 24 | 
         
             
                        history = history or []
         
     | 
| 25 | 
         
            +
                        chain = get_chain(vectorstore)
         
     | 
| 26 | 
         
             
                        # If chain is None, that is because no API key was provided.
         
     | 
| 27 | 
         
             
                        if chain is None:
         
     | 
| 28 | 
         
             
                            history.append((inp, "Please paste your OpenAI key to use"))
         
     | 
| 29 | 
         
             
                            return history, history
         
     | 
| 30 | 
         
             
                        # Set OpenAI key
         
     | 
| 31 | 
         
             
                        import openai
         
     | 
| 32 | 
         
            +
                        
         
     | 
| 33 | 
         
            +
                        openai.api_key = os.environ["OPENAI_API_KEY"]
         
     | 
| 34 | 
         
             
                        # Run chain and append input.
         
     | 
| 35 | 
         
             
                        output = chain({"question": inp, "chat_history": history})["answer"]
         
     | 
| 36 | 
         
             
                        history.append((inp, output))
         
     | 
| 
         | 
|
| 55 | 
         
             
                    gr.Markdown(
         
     | 
| 56 | 
         
             
                        "<h2><center>The Bot Forge</h2></center><h3> <center></center></h3>")
         
     | 
| 57 | 
         | 
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 58 | 
         
             
                chatbot = gr.Chatbot()
         
     | 
| 59 | 
         | 
| 60 | 
         
             
                with gr.Row():
         
     |