ConversAI / app.py
techconsptrs's picture
DEBUG
4f91aef
raw
history blame
8.01 kB
from src.pipelines.completePipeline import Pipeline
import gradio as gr
import spaces
import os
# os.system("apt-get update -y")
# os.system("apt-get upgrade -y")
# os.system("apt install poppler-utils -y")
chain = None
pipeline = Pipeline()
@spaces.GPU
def getTextResponse(text: str, inputQuery: str):
global chain
if chain is None:
chain = pipeline.plainText(text = text)
else:
pass
response = chain.invoke(
{
"question": inputQuery
}
)
return response
@spaces.GPU
def getSearchablePdfResponse(path: str, inputQuery: str):
global chain
if chain is None:
chain = pipeline.searchablePdf(path = path)
else:
pass
response = chain.invoke(
{
"question": inputQuery
}
)
return response
@spaces.GPU
def getScannablePdfResponse(path: str, inputQuery: str):
global chain
if chain is None:
chain = pipeline.scannablePdf(path = path)
else:
pass
response = chain.invoke(
{
"question": inputQuery
}
)
return response
def clearFunction():
global chain
chain = None
with gr.Blocks() as textInterface:
with gr.Row():
inputText = gr.Textbox(
label = "Input Text",
placeholder = "Enter you text here"
)
with gr.Row():
question = gr.Textbox(
label = "Question",
placeholder = "Enter your question here"
)
answer = gr.Textbox(
label = "Response",
interactive = False
)
with gr.Row():
submitButton = gr.Button(
value = "Submit",
variant = "primary"
)
clearButton = gr.ClearButton(
components = [inputText, question, answer],
value = "Clear",
variant = "secondary"
)
submitButton.click(
fn = getTextResponse,
inputs = [inputText, question],
outputs = [answer]
)
clearButton.click(
fn = clearFunction
)
with gr.Blocks() as searchablePdf:
with gr.Row():
inputFile = gr.File(
file_types = [".pdf"],
file_count = "single",
label = "Select PDF"
)
with gr.Row():
question = gr.Textbox(
label = "Question",
placeholder = "Enter your question here"
)
answer = gr.Textbox(
label = "Response",
interactive = False
)
with gr.Row():
submitButton = gr.Button(
value = "Submit",
variant = "primary"
)
clearButton = gr.ClearButton(
components = [inputFile, question, answer],
value = "Clear",
variant = "secondary"
)
submitButton.click(
fn = getSearchablePdfResponse,
inputs = [inputFile, question],
outputs = [answer]
)
clearButton.click(
fn = clearFunction
)
with gr.Blocks() as scannablePdf:
with gr.Row():
inputFile = gr.File(
file_types = [".pdf"],
file_count = "single",
label = "Select PDF"
)
with gr.Row():
question = gr.Textbox(
label = "Question",
placeholder = "Enter your question here"
)
answer = gr.Textbox(
label = "Response",
interactive = False
)
with gr.Row():
submitButton = gr.Button(
value = "Submit",
variant = "primary"
)
clearButton = gr.ClearButton(
components = [inputFile, question, answer],
value = "Clear",
variant = "secondary"
)
submitButton.click(
fn = getScannablePdfResponse,
inputs = [inputFile, question],
outputs = [answer]
)
clearButton.click(
fn = clearFunction
)
def getLinksButtonFn(baseUrl: str):
links = pipeline.webCrawler.getLinks(url = baseUrl)
checkboxes = gr.CheckboxGroup(
choices = links,
label = "Fetched Links",
visible = True
)
row2 = gr.Row(visible = True)
row3 = gr.Row(visible = True)
return (
checkboxes,
row2,
row3
)
def getWebsiteResponse(links: list[str], inputQuery: str):
global chain
if chain is None:
print(links)
chain = pipeline.webCrawl(urls = links)
else:
pass
response = chain.invoke(
{
"question": inputQuery
}
)
return response
def clearWebsiteResponse():
global chain
chain = None
checkboxes = gr.CheckboxGroup(
choices = [],
label = "Fetched Links",
visible = False
)
return checkboxes
with gr.Blocks() as websiteCrawler:
with gr.Row():
inputUrl = gr.Textbox(
label = "Base URL",
placeholder = "Enter the Base URL to fetch other links",
scale = 3
)
getLinksButton = gr.Button(
value = "Get Links",
variant = "primary",
scale = 1
)
checkboxes = gr.CheckboxGroup(
choices = [],
label = "Fetched Links",
)
with gr.Row(visible = False) as row2:
question = gr.Textbox(
label = "Question",
placeholder = "Enter your question here"
)
answer = gr.Textbox(
label = "Response",
interactive = False
)
with gr.Row(visible = False) as row3:
submitButton = gr.Button(
value = "Submit",
variant = "primary"
)
clearButton = gr.ClearButton(
components = [question, answer],
value = "Clear",
variant = "secondary"
)
getLinksButton.click(
fn = getLinksButtonFn,
inputs = [inputUrl],
outputs = [checkboxes, row2, row3]
)
submitButton.click(
fn = getWebsiteResponse,
inputs = [checkboxes, question],
outputs = [answer]
)
clearButton.click(
fn = clearWebsiteResponse,
inputs = None,
outputs = [checkboxes]
)
def getYoutubeResponse(links: str, inputQuery: str):
global chain
links = [link.strip() for link in links.split(",")]
if chain is None:
chain = pipeline.youtubeLinks(urls = links)
else:
pass
response = chain.invoke(
{
"question": inputQuery
}
)
return response
with gr.Blocks() as youtubeInterface:
with gr.Row():
inputLinks = gr.Textbox(
label = "Youtube Links",
placeholder = 'Enter comma(,)-separated youtube video links'
)
with gr.Row():
question = gr.Textbox(
label = "Question",
placeholder = "Enter your question here"
)
answer = gr.Textbox(
label = "Response",
interactive = False
)
with gr.Row():
submitButton = gr.Button(
value = "Submit",
variant = "primary"
)
clearButton = gr.ClearButton(
components = [inputLinks, question, answer],
value = "Clear",
variant = "secondary"
)
submitButton.click(
fn = getYoutubeResponse,
inputs = [inputLinks, question],
outputs = [answer]
)
clearButton.click(
fn = clearFunction
)
application = gr.TabbedInterface(
[textInterface, searchablePdf, scannablePdf, websiteCrawler, youtubeInterface],
["Text", "Searchable PDF", "Scannable PDF", "Website Text", "Youtube Transcripts"]
)
application.launch()