File size: 3,056 Bytes
dd8f353
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f3b9247
 
679fe00
 
 
dd8f353
 
 
 
 
 
 
679fe00
dd8f353
 
 
 
 
679fe00
dd8f353
 
 
 
 
 
679fe00
 
dd8f353
 
 
 
 
 
 
 
 
 
 
930b1eb
dd8f353
930b1eb
54882b5
dd8f353
9b2b028
 
 
dd8f353
e9ee445
dd8f353
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9b2b028
dd8f353
 
 
 
 
 
 
e39101a
dd8f353
 
 
679fe00
bf06365
dd8f353
679fe00
bf06365
 
 
 
 
 
 
dd8f353
e5fc77d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
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)


def set_openai_api_key(api_key: str):
    """Set the api key and return chain.
    If no api_key, then None is returned.
    """
    print(os.environ["TEST"])
    
          
        
        
        return chain

class ChatWrapper:

    def __init__(self):
        self.lock = Lock()
    def __call__(
        self, api_key: str, inp: str, history: Optional[Tuple[str, str]]
    ):
        """Execute the chat functionality."""
        self.lock.acquire()
        try:
            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 = "<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>Chat about - State-of-the-Union 2023</center></h3>")

        openai_api_key_textbox = gr.Textbox(
            placeholder="Paste your OpenAI API key (sk-...)",
            show_label=False,
            lines=1,
            type="password",
        )

    chatbot = gr.Chatbot()

    with gr.Row():
        message = gr.Textbox(
            label="What's your question?",
            placeholder="Ask questions about the most recent state of the union",
            lines=1,
        )
        submit = gr.Button(value="Send", variant="secondary").style(full_width=False)

    gr.Examples(
        examples=[
            "What did the president say about Kentaji Brown Jackson",
            "Did he mention Stephen Breyer?",
            "What was his stance on Ukraine",
        ],
        inputs=message,
    )

  
    state = gr.State()
    agent_state = gr.State()

    
    message.submit(chat, inputs=[openai_api_key_textbox, 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)