File size: 9,249 Bytes
1802405
7e24b41
 
4f91aef
7e24b41
1802405
 
 
7e24b41
4f91aef
1802405
 
 
 
 
 
 
 
 
 
 
7e24b41
 
1802405
 
7e24b41
 
4f91aef
1802405
 
 
 
 
 
 
 
 
 
 
7e24b41
 
1802405
 
7e24b41
 
4f91aef
1802405
 
 
 
 
 
 
 
 
 
 
7e24b41
 
1802405
 
7e24b41
 
1802405
 
7e24b41
 
 
1802405
7e24b41
 
 
1802405
 
7e24b41
 
 
1802405
 
7e24b41
 
1802405
 
7e24b41
 
1802405
7e24b41
1802405
 
 
7e24b41
1802405
 
 
7e24b41
1802405
7e24b41
 
 
1802405
 
 
7e24b41
 
1802405
 
7e24b41
1802405
7e24b41
1802405
 
 
7e24b41
1802405
 
 
7e24b41
1802405
7e24b41
 
1802405
7e24b41
1802405
 
7e24b41
1802405
7e24b41
1802405
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7e24b41
1802405
 
 
 
7e24b41
1802405
 
 
7e24b41
1802405
 
 
7e24b41
 
1802405
 
7e24b41
 
1802405
 
7e24b41
1802405
 
7e24b41
 
1802405
7e24b41
 
 
1802405
 
 
 
 
 
 
 
 
 
 
7e24b41
1802405
 
 
7e24b41
1802405
 
 
 
7e24b41
d14c53b
1802405
 
 
 
 
 
 
 
 
 
 
7e24b41
1802405
7e24b41
1802405
 
7e24b41
 
1802405
7e24b41
 
 
1802405
 
7e24b41
 
1802405
 
7e24b41
1802405
7e24b41
1802405
 
 
7e24b41
1802405
 
 
7e24b41
1802405
7e24b41
 
 
 
 
1802405
7e24b41
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# Import necessary libraries and modules
from src.pipelines.completePipeline import Pipeline
import gradio as gr
import spaces

# Initialize global variables
chain = None  # Holds the current processing chain
pipeline = Pipeline()  # Instantiate the processing pipeline

@spaces.GPU
def getTextResponse(text: str, inputQuery: str) -> str:
    """

    Generate a response based on the input text and query.



    Args:

        text (str): The input text to process.

        inputQuery (str): The question to be answered.



    Returns:

        str: The response generated from the input text.

    """
    global chain
    if chain is None:
        chain = pipeline.plainText(text=text)  # Create a new processing chain for plain text
    response = chain.invoke({"question": inputQuery})  # Process the query
    return response

@spaces.GPU
def getSearchablePdfResponse(path: str, inputQuery: str) -> str:
    """

    Generate a response based on a searchable PDF and query.



    Args:

        path (str): Path to the searchable PDF.

        inputQuery (str): The question to be answered.



    Returns:

        str: The response generated from the searchable PDF.

    """
    global chain
    if chain is None:
        chain = pipeline.searchablePdf(path=path)  # Create a new processing chain for the PDF
    response = chain.invoke({"question": inputQuery})
    return response

@spaces.GPU
def getScannablePdfResponse(path: str, inputQuery: str) -> str:
    """

    Generate a response based on a scannable PDF and query.



    Args:

        path (str): Path to the scannable PDF.

        inputQuery (str): The question to be answered.



    Returns:

        str: The response generated from the scannable PDF.

    """
    global chain
    if chain is None:
        chain = pipeline.scannablePdf(path=path)  # Create a new processing chain for the scannable PDF
    response = chain.invoke({"question": inputQuery})
    return response

def clearFunction() -> None:
    """Reset the processing chain to prepare for new queries."""
    global chain
    chain = None

# User interface for text input
with gr.Blocks() as textInterface:
    with gr.Row():
        inputText = gr.Textbox(
            label="Input Text",
            placeholder="Enter your text here"
        )
    with gr.Row():
        question = gr.Textbox(
            label="Question",
            placeholder="Enter your question here"
        )
        answer = gr.Textbox(
            label="Response",
            interactive=False  # Make the response field read-only
        )
    with gr.Row():
        submitButton = gr.Button(value="Submit", variant="primary")
        clearButton = gr.ClearButton(
            components=[inputText, question, answer],
            value="Clear",
            variant="secondary"
        )
    # Define actions for buttons
    submitButton.click(fn=getTextResponse, inputs=[inputText, question], outputs=[answer])
    clearButton.click(fn=clearFunction)

# User interface for searchable PDF input
with gr.Blocks() as searchablePdf:
    with gr.Row():
        inputFile = gr.File(
            file_types=[".pdf"],  # Restrict file types to PDFs
            file_count="single",   # Allow only one PDF file selection
            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"
        )
    # Define actions for buttons
    submitButton.click(fn=getSearchablePdfResponse, inputs=[inputFile, question], outputs=[answer])
    clearButton.click(fn=clearFunction)

# User interface for scannable PDF input
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"
        )
    # Define actions for buttons
    submitButton.click(fn=getScannablePdfResponse, inputs=[inputFile, question], outputs=[answer])
    clearButton.click(fn=clearFunction)

def getLinksButtonFn(baseUrl: str) -> tuple:
    """

    Fetch links from the specified base URL.



    Args:

        baseUrl (str): The base URL from which to fetch links.



    Returns:

        tuple: A tuple containing a CheckboxGroup of fetched links and two rows for the UI.

    """
    links = pipeline.webCrawler.getLinks(url=baseUrl)  # Fetch links using the web crawler
    checkboxes = gr.CheckboxGroup(choices=links, label="Fetched Links", visible=True)
    row2 = gr.Row(visible=True)
    row3 = gr.Row(visible=True)
    return checkboxes, row2, row3

@spaces.GPU
def getWebsiteResponse(links: list[str], inputQuery: str) -> str:
    """

    Generate a response based on fetched website links and a query.



    Args:

        links (list[str]): List of links to process.

        inputQuery (str): The question to be answered.



    Returns:

        str: The response generated from the website links.

    """
    global chain
    if chain is None:
        chain = pipeline.webCrawl(urls=links)  # Create a new processing chain for web crawling
    response = chain.invoke({"question": inputQuery})
    return response

def clearWebsiteResponse() -> gr.CheckboxGroup:
    """Clear the website response and reset the checkboxes."""
    global chain
    chain = None  # Reset the chain
    checkboxes = gr.CheckboxGroup(choices=[], label="Fetched Links", visible=False)
    return checkboxes

# User interface for website crawling
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"
        )
    # Define actions for buttons
    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])

@spaces.GPU
def getYoutubeResponse(links: str, inputQuery: str) -> str:
    """

    Generate a response based on YouTube video links and a query.



    Args:

        links (str): Comma-separated YouTube video links.

        inputQuery (str): The question to be answered.



    Returns:

        str: The response generated from the YouTube videos.

    """
    global chain
    links = [link.strip() for link in links.split(",")]  # Split and clean the links
    if chain is None:
        chain = pipeline.youtubeLinks(urls=links)  # Create a new processing chain for YouTube links
    response = chain.invoke({"question": inputQuery})
    return response

# User interface for YouTube links
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"
        )
    # Define actions for buttons
    submitButton.click(fn=getYoutubeResponse, inputs=[inputLinks, question], outputs=[answer])
    clearButton.click(fn=clearFunction)

# Create a tabbed interface for the different functionalities
application = gr.TabbedInterface(
    [textInterface, searchablePdf, scannablePdf, websiteCrawler, youtubeInterface],
    ["Text", "Searchable PDF", "Scannable PDF", "Website Text", "Youtube Transcripts"]
)

# Launch the Gradio application
application.launch()