File size: 2,658 Bytes
f1a168b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import datetime
import pickle

import gradio as gr
import langchain
from langchain.llms import HuggingFaceHub

from chain import get_new_chain

api_token = os.environ["HF_TOKEN"]


def get_faiss_store():
    with open("docs.pkl", "rb") as f:
        faiss_store = pickle.load(f)
        return faiss_store

def load_model():

    print(langchain.__file__)

    vectorstore = get_faiss_store()

    flan_ul = HuggingFaceHub(repo_id="google/flan-ul2", 
                                model_kwargs={"temperature":0.1, "max_new_tokens":200},
                                huggingfacehub_api_token=api_token)

    qa_chain = get_new_chain(vectorstore, flan_ul)
    return qa_chain


def chat(inp, agent):
    result = []
    if agent is None:
        result.append((inp, "Please wait for model to load (3-5 seconds)"))
        return result
    print("\n==== date/time: " + str(datetime.datetime.now()) + " ====")
    print("inp: " + inp)
    result = []
    output = agent({"question": inp})
    answer = output["answer"]
    result.append((inp, answer))
    print(result)
    return result


block = gr.Blocks(css=".gradio-container {background-color: lightgray}")

with block:
    with gr.Row():
        gr.Markdown("<h3><center>PotterChat</center></h3><p>Ask questions about the Harry Potter Books, Powered by Flan-UL2</p>")

    chatbot = gr.Chatbot()

    with gr.Row():
        message = gr.Textbox(
            label="What's your question?",
            placeholder="Who was Harry's godfather?",
            lines=1,
        )
        submit = gr.Button(value="Send", variant="secondary").style(full_width=False)

    gr.Examples(
        examples=[
            "Which house in Hogwarts was Harry in?",
            "Who were Harry's best friends?",
            "Who taught Potions at Hogwarts?",
        ],
        inputs=message,
    )

    gr.HTML(
        """
    This simple application uses Langchain, an open-source LLM, and FAISS to do Q&A over the Harry Potter books."""
    )

    gr.HTML(
        "<center>Powered by <a href='huggingface.co'>Hugging Face 🤗</a> and <a href='https://github.com/hwchase17/langchain'>LangChain 🦜️🔗</a></center>"
    )

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

    block.load(load_model, inputs=None, outputs=[agent_state])

    # submit.click(chat, inputs=[message, state, agent_state], outputs=[chatbot, state])
    # message.submit(chat, inputs=[message, state, agent_state], outputs=[chatbot, state])
    submit.click(chat, inputs=[message, agent_state], outputs=[chatbot])
    message.submit(chat, inputs=[message, agent_state], outputs=[chatbot])

block.launch(debug=True)