File size: 2,519 Bytes
0190e25
ea36e00
77a48be
 
 
 
45f1f60
096b9ec
45f1f60
93457a9
 
 
 
 
45f1f60
0190e25
e9acb87
 
 
0190e25
 
 
45f1f60
096b9ec
 
 
 
 
77a48be
096b9ec
 
 
45f1f60
0190e25
 
 
 
45f1f60
ffcbf6b
 
 
 
 
 
096b9ec
 
 
45f1f60
0190e25
 
 
ffcbf6b
0190e25
 
93457a9
ffcbf6b
 
 
 
 
 
 
 
 
 
93457a9
ffcbf6b
0190e25
 
ffcbf6b
0190e25
 
ffcbf6b
93457a9
0190e25
 
 
 
45f1f60
0190e25
 
 
 
e9acb87
5c184a9
0190e25
 
 
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
# import for typing
from langchain.chains import RetrievalQAWithSourcesChain

# gradio
import gradio as gr

global qa 
from qa import qa


#####
#
# Gradio fns
####

def create_gradio_interface(qa:RetrievalQAWithSourcesChain):
    """
    Create a gradio interface for the QA model
    """
    def add_text(history, text):
        history = history + [(text, None)]
        return history, ""

    def bot(history):
        response = infer(history[-1][0], history)
        sources = [doc.metadata.get("source") for doc in response['source_documents']]
        src_list = '\n'.join(sources)
        print_this = response['answer'] + "\n\n\n Sources: \n\n\n" + src_list


        history[-1][1] = print_this #response['answer']
        return history

    def infer(question, history):
        query =  question
        result = qa({"query": query, "history": history, "question": question})
        return result

    def vote(data: gr.LikeData):
        if data.liked:
            print("You upvoted this response: ")
        else:
            print("You downvoted this response: ")

    css="""
    #col-container {max-width: 700px; margin-left: auto; margin-right: auto;}
    """

    title = """
    <div style="text-align: center;max-width: 1920px;">
        <h1>Chat with your Documentation</h1>
        <p style="text-align: center;">This is a privately hosten Docs AI Buddy ;)</p>
    </div>
    """

    head_style = """
    <style>
    @media (min-width: 1536px)
    {
        .gradio-container {
            min-width: var(--size-full) !important;
        }
    }
    </style>
    """

    with gr.Blocks(title="DocsBuddy AI πŸ€΅πŸ»β€β™‚οΈ", head=head_style) as demo:
        with gr.Column(min_width=900, elem_id="col-container"):
            gr.HTML(title)      
            chatbot = gr.Chatbot([], elem_id="chatbot", label="DocuBuddy πŸ€΅πŸ»β€β™‚οΈ")
            #with gr.Row():
            #    clear = gr.Button("Clear")
            chatbot.like(vote, None, None)

            with gr.Row():
                question = gr.Textbox(label="Question", placeholder="Type your question and hit Enter ")
            with gr.Row():
                clear = gr.ClearButton([chatbot, question])

        question.submit(add_text, [chatbot, question], [chatbot, question], queue=False).then(
            bot, chatbot, chatbot
        )
        #clear.click(lambda: None, None, chatbot, queue=False)
    return demo

if __name__ == "__main__":
    demo = create_gradio_interface(qa)
    demo.queue().launch()