File size: 2,777 Bytes
db05c27
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ff6a7f5
 
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
from gpt_index import GPTListIndex
import gradio as gr
import openai
import os


def openaiapikeyvaliditycheck(openaikey):
    """
    Function to check validity of openai key
    """
    # Set the API key
    openai.api_key = openaikey
    # Test the API key by making a request to the OpenAI API
    try:
        response = openai.Model.list()
        return "Valid OpenAI API key"
    except openai.OpenAIError:
        apikeylink = "https://beta.openai.com/account/api-keys"
        return f"Incorrect OpenAI API key provided: {openaikey}. You can find your OpenAI API key here - {apikeylink}"


def docques(index_type, query, apikey):
    """
    Function to create index and query
    """

    # Basic Checks
    if not index_type:
        return "Please Select source type"
    if not query:
        return "Please enter your query."
    if not apikey:
        return "Please enter your openai apikey."

    openaiapikeyvality = openaiapikeyvaliditycheck(apikey)

    if openaiapikeyvality != "Valid OpenAI API key":
        return openaiapikeyvality

    # Store openai key in environment
    os.environ['OPENAI_API_KEY'] = apikey

    index = ""

    if index_type == "Como validar seu produto antes mesmo de criá-lo":
        index = GPTListIndex.load_from_disk('index_audiolong.json')
    elif index_type == "Conheça a Robotizia - Criação de conteúdos com Inteligência Artificial, uma startup da ReportFlex":
        index = GPTListIndex.load_from_disk('index_audioshort.json')

    # Query based on index
    response = index.query(query, response_mode="tree_summarize")

    return response


def cleartext(query, output):
    """
    Function to clear text
    """
    return ["", ""]


with gr.Blocks() as demo:
    gr.Markdown(
        """
    <h1><center><b>VideoQues</center></h1>
    
    """)
    with gr.Row():
        with gr.Column():
            index_type = gr.Dropdown(["Como validar seu produto antes mesmo de criá-lo",
                                      "Conheça a Robotizia - Criação de conteúdos com Inteligência Artificial, uma startup da ReportFlex"], label="Select source type")
            apikey = gr.Textbox(lines=2, label="Enter Your OpenAI API Key.")
            query = gr.Textbox(lines=2, label="Enter Your Query.")
            submit_button = gr.Button("Submit")
        with gr.Column():
            ans_output = gr.Textbox(label="Answer.")
            clear_button = gr.Button("Clear")

    # Submit button for submitting query.
    submit_button.click(
        docques, inputs=[index_type, query, apikey], outputs=[ans_output])

    # Clear button for clearing query and answer.
    clear_button.click(cleartext, inputs=[
                       query, ans_output], outputs=[query, ans_output])

demo.launch(debug=True)