videoques / app.py
ravithejads's picture
Update app.py
ff6a7f5
raw
history blame
2.78 kB
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)