File size: 2,012 Bytes
2466361
 
 
77cb5fb
d16f4d9
 
 
 
2466361
77cb5fb
2466361
 
 
 
 
 
 
 
 
 
 
 
4c9d927
2466361
 
 
 
 
d16f4d9
2466361
d16f4d9
2466361
d16f4d9
2466361
 
 
 
 
 
701ae0d
2466361
 
 
 
 
 
 
 
 
4c9d927
 
2466361
 
701ae0d
2466361
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
import os

import gradio as gr
import hpqa
from langchain.chains import RetrievalQA
from langchain_core.vectorstores import VectorStoreRetriever
from langchain_openai import OpenAI

os.environ["OPENAI_API_KEY"] = ""
index_path = "data/hpqa_faiss_index_500"


examples = [
    "How would you sneak into Hogwarts without being detected?",
    "Why did Snape kill Dumbledore?",
    "Who is the most badass wizard in the world?",
    "Who would win a fight between Dumbledore and a grizzly bear?",
    "How many siblings does Hermione have?",
    "Why are the Dursleys so mean to Harry?",
]


def api(question, temperature, api_key=None):
    if api_key is None or len(api_key) == 0:
        return "You must provide an OpenAI API key to use this demo πŸ‘‡"
    if len(question) == 0:
        return ""
    document_store = hpqa.load_document_store(index_path, openai_api_key=api_key)
    chain = RetrievalQA.from_llm(
        llm=OpenAI(temperature=temperature, openai_api_key=api_key),
        retriever=VectorStoreRetriever(vectorstore=document_store),
    )
    response = chain.invoke(question)
    return response["result"].strip()


demo = gr.Blocks()

with demo:
    gr.Markdown("# πŸͺ„ The GPT Who Lived: Harry Potter QA with GPT πŸ€–")
    with gr.Row():
        with gr.Column():
            question = gr.Textbox(lines=4, label="Question")
            temperature = gr.Slider(0.0, 2.0, 0.7, step=0.1, label="🍺 Butterbeer Consumed")
            with gr.Row():
                clear = gr.Button("Clear")
                btn = gr.Button("Submit", variant="primary")
        with gr.Column():
            answer = gr.Textbox(lines=4, label="Answer")
            openai_api_key = gr.Textbox(type="password", label="OpenAI API key")
    btn.click(api, [question, temperature, openai_api_key], answer)
    clear.click(lambda _: "", question, question)
    gr.Examples(examples, question)
    gr.Markdown("πŸ’» Checkout the `hpqa` source code on [GitHub](https://github.com/johnnygreco/hpqa).")
demo.launch()