Spaces:
Runtime error
Runtime error
File size: 14,315 Bytes
fb2b8cf 31dada8 fb2b8cf dd7fea7 fb2b8cf dd7fea7 fb2b8cf dd7fea7 fb2b8cf 31dada8 fb2b8cf da2a66c fb2b8cf f440070 fb2b8cf 5c1ac62 fb2b8cf 31dada8 dd7fea7 fb2b8cf dd7fea7 fb2b8cf 31dada8 fb2b8cf 31dada8 dd7fea7 31dada8 fb2b8cf 31dada8 dd7fea7 31dada8 fb2b8cf da2a66c fb2b8cf da2a66c fb2b8cf da2a66c fb2b8cf da2a66c fb2b8cf da2a66c fb2b8cf da2a66c fb2b8cf da2a66c 31dada8 389a455 31dada8 fb2b8cf 31dada8 fb2b8cf 31dada8 fb2b8cf e41891c da2a66c dd7fea7 fb2b8cf |
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 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 |
import gradio as gr
from src.document_utils import (
summarize,
question_answer,
generate_questions,
load_history,
load_science,
paraphrase
)
from src.wiki_search import cross_lingual_document_search, translate_text
from src.theme import CustomTheme
max_search_results = 3
def reset_chatbot():
return gr.update(value="")
def get_user_input(input_question, history):
return "", history + [[input_question, None]]
def study_doc_qa_bot(input_document, history):
bot_message = question_answer(input_document, history)
history[-1][1] = bot_message
return history
custom_theme = CustomTheme()
with gr.Blocks(theme=custom_theme) as demo:
gr.HTML(
"""<html><center><img src='file/logo/omowe_logo.png', alt='omowe.ai logo', width=820, height=312 /></center><br></html>"""
)
qa_bot_state = gr.State(value=[])
with gr.Tabs():
with gr.TabItem("Document Search"):
gr.HTML(
"""<p style="text-align:center;font-size:24px;"><b>Search across a library of study materials in your own native language or even a mix of languages.</p>"""
)
gr.HTML(
"""<p style="text-align:center; font-style:italic; font-size:16px;">Get started with a pre-indexed set of study materials spaning various subjects (History, Literature, Philosophy, Government etc) in 4 different languages.</p>"""
)
with gr.Row():
text_match = gr.CheckboxGroup(
["Full Text Search"], label="find exact text in documents", visible=False
)
with gr.Row():
lang_choices = gr.CheckboxGroup(
[
"English",
"Yoruba",
"Igbo",
"Hausa",
],
label="Filter results based on language",
value = "Yoruba"
)
with gr.Row():
with gr.Column():
user_query = gr.Text(
label="Enter query here",
placeholder="Search through study materials (e.g The Nigerian Civil War, What is Literature)",
)
num_search_results = gr.Slider(
1,
max_search_results,
visible=False,
value=max_search_results,
step=1,
interactive=True,
label="How many search results to show:",
)
with gr.Row():
with gr.Column():
query_match_out_1 = gr.Textbox(
label= f"Search Result 1"
)
with gr.Column():
with gr.Accordion("Click to View Translation/Source", open=False):
translate_btn_1 = gr.Button(
label="Translate Text",
value="Translate Text",
variant="primary",
)
translate_res_1 = gr.Textbox(
label=f"Translation in English",
)
source_res_1 = gr.Textbox(
label=f"Source Url",
)
with gr.Row():
with gr.Column():
query_match_out_2 = gr.Textbox(label=f"Search Result 2")
with gr.Column():
with gr.Accordion("Click to View Translation/Source", open=False):
translate_btn_2 = gr.Button(
label="Translate Text",
value="Translate Text",
variant="primary",
)
translate_res_2 = gr.Textbox(
label=f"Translation in English",
)
source_res_2 = gr.Textbox(
label=f"Source Url"
)
with gr.Row():
with gr.Column():
query_match_out_3 = gr.Textbox(label=f"Search Result 3")
with gr.Column():
with gr.Accordion("Click to View Translation/Source", open=False):
translate_btn_3 = gr.Button(
label="Translate Text",
value="Translate Text",
variant="primary",
)
translate_res_3= gr.Textbox(
label=f"Translation in English",
)
source_res_3 = gr.Textbox(
label=f"Source Url"
)
with gr.TabItem("Q&A"):
gr.HTML(
"""<p style="text-align:center; font-size:16px;"><b>Looking to breeze through your study materials effortlessly? Simply upload your documents and fire away any questions you have!</p>"""
)
with gr.Row():
with gr.Accordion("Click to use preloaded examples", open=False):
example_2 = gr.Button(
"Load History of Nigeria", variant="primary"
)
example_1 = gr.Button(
"Load Science of Photosynthesis", variant="primary"
)
with gr.Row():
with gr.Column():
input_document = gr.Text(label="Copy your document here", lines=2)
input_document_pdf = gr.inputs.File(label="Uplaod file")
with gr.Column():
chatbot = gr.Chatbot(label="Chat History")
input_question = gr.Text(
label="Ask a question",
placeholder="Type a question here and hit enter.",
)
clear = gr.Button("Clear", variant="primary")
with gr.TabItem("Summarize"):
gr.HTML(
"""<p style="text-align:center; font-size:24px;"><b> Get the most out of your study materials!</p>"""
)
gr.HTML(
"""<p style="text-align:center; font-size:16px;"><b>You can easily upload your documents and generate quick summaries and practice questions in a flash.</p>"""
)
with gr.Row():
with gr.Accordion("Click to use preloaded examples", open=False):
example_4 = gr.Button(
"Load History of Nigeria", variant="primary"
)
example_3 = gr.Button(
"Load Science of Photosynthesis", variant="primary"
)
with gr.Row():
with gr.Column():
summary_input = gr.Text(label="Document", lines=5)
with gr.Column():
summary_output = gr.Text(label="Generated Summary", lines=5)
invisible_comp = gr.Text(label="Dummy Component", visible=False)
with gr.Row():
with gr.Column():
with gr.Accordion("Summary Settings", open=False):
summary_length = gr.Radio(
["short", "medium", "long"],
label="Summary Length",
value="long",
)
summary_format = gr.Radio(
["paragraph", "bullets"],
label="Summary Format",
value="bullets",
)
extractiveness = gr.Radio(
["low", "medium", "high"],
label="Extractiveness",
info="Controls how close to the original text the summary is.",
visible=False,
value="high",
)
temperature = gr.Slider(
minimum=0,
maximum=5.0,
value=0.64,
step=0.1,
interactive=True,
visible=False,
label="Temperature",
info="Controls the randomness of the output. Lower values tend to generate more “predictable” output, while higher values tend to generate more “creative” output.",
)
with gr.Row():
generate_summary = gr.Button("Generate Summary", variant="primary")
with gr.Row():
generate_questions_btn = gr.Button("Generate practice questions", variant="primary")
with gr.Row():
generate_output = gr.Text(label="Generated questions", lines=5)
with gr.TabItem("Paraphrase"):
gr.HTML(
"""<p style="text-align:center;"><b>Provide the text you'll like to accurately rephrase.</p>"""
)
with gr.Row():
with gr.Column():
paraphrase_input = gr.Text(label="Document", lines=10)
generate_paraphrase = gr.Button("Paraphrase", variant="primary")
with gr.Column():
paraphrase_output = gr.HTML(label="Paraphrase", lines=10)
invisible_comp = gr.Text(label="Dummy Component", visible=False)
with gr.Row():
with gr.Accordion("Advanced Settings:", open=False):
paraphrase_length = gr.Radio(
["short", "medium", "long"],
label="Paraphrase Length",
value="long",
)
paraphrase_format = gr.Radio(
["paragraph", "bullets"],
label="Paraphrase Format",
value="bullets",
)
extractiveness = gr.Radio(
["low", "medium", "high"],
label="Extractiveness",
info="Controls how close to the original text the paraphrase is.",
visible=False,
value="high",
)
temperature = gr.Slider(
minimum=0,
maximum=5.0,
value=0.64,
step=0.1,
interactive=True,
visible=False,
label="Temperature",
info="Controls the randomness of the output. Lower values tend to generate more “predictable” output, while higher values tend to generate more “creative” output.",
)
# fetch answer for submitted question corresponding to input document
input_question.submit(
get_user_input,
[input_question, chatbot],
[input_question, chatbot],
queue=False,
).then(study_doc_qa_bot, [input_document, chatbot], chatbot)
# reset the chatbot Q&A history when input document changes
input_document.change(fn=reset_chatbot, inputs=[], outputs=chatbot)
# Loading examples on click for Q&A module
example_1.click(
load_history,
[],
[input_document, input_question],
queue=False,
)
example_2.click(
load_science,
[],
[input_document, input_question],
queue=False,
)
# Loading examples on click for Q&A module
example_3.click(
load_history,
[],
[summary_input, invisible_comp],
queue=False,
)
example_4.click(
load_science,
[],
[summary_input, invisible_comp],
queue=False,
)
# generate summary corresponding to document submitted by the user.
generate_summary.click(
summarize,
[summary_input, summary_length, summary_format, extractiveness, temperature],
[summary_output],
queue=False,
)
generate_questions_btn.click(
generate_questions,
[summary_input],
[generate_output],
queue=False,
)
generate_paraphrase.click(
paraphrase,
[paraphrase_input],
[paraphrase_output],
queue=False,
)
# clear the chatbot Q&A history when this button is clicked by the user
clear.click(lambda: None, None, chatbot, queue=False)
# run search if user submits query
user_query.submit(
cross_lingual_document_search,
[user_query, num_search_results, lang_choices, text_match],
[query_match_out_1, query_match_out_2, query_match_out_3, \
source_res_1,source_res_2,source_res_3],
queue=False,
)
# translate results corresponding to 1st search result obtained if user clicks 'Translate'
translate_btn_1.click(
translate_text,
[query_match_out_1],
[translate_res_1],
queue=False,
)
translate_btn_2.click(
translate_text,
[query_match_out_2],
[translate_res_2],
queue=False,
)
translate_btn_3.click(
translate_text,
[query_match_out_3],
[translate_res_3],
queue=False,
)
if __name__ == "__main__":
demo.launch(debug=True)
|