File size: 21,339 Bytes
57cfb76 015f1eb 57cfb76 57349e3 6005177 1bc9039 db80cf2 6005177 f402198 c0b7721 e16c6ed 57349e3 eaa6fbd 2c834ce 1bc9039 e16c6ed 7cc44f8 f837c69 417ab3c f837c69 417ab3c 2c834ce 015f1eb 417ab3c f837c69 015f1eb db80cf2 ccd6adb 57349e3 db80cf2 57cfb76 db80cf2 894ddd1 57cfb76 db80cf2 57cfb76 894ddd1 db80cf2 1bc9039 3f3baec 9d7438a e16c6ed adbdeee 9d7438a c14a77e c0b7721 1bc9039 c14a77e eaa6fbd c14a77e eaa6fbd db80cf2 eaa6fbd db80cf2 eaa6fbd c14a77e c0b7721 57cfb76 eaa6fbd 109f376 eaa6fbd 109f376 eaa6fbd 1bc9039 721b7bd 6949931 db80cf2 721b7bd 9d7438a adbdeee 9d7438a db80cf2 721b7bd 02063d6 721b7bd 1bc9039 c0b7721 c14a77e c0b7721 c14a77e cceba7f c14a77e 9d7438a c0b7721 c14a77e c0b7721 9d7438a 72a6115 9d7438a 72a6115 db80cf2 9d7438a 7a036c9 9d7438a 7a036c9 9d7438a c14a77e 1bc9039 c14a77e 1bc9039 e16c6ed 015f1eb 1bc9039 e16c6ed 015f1eb e16c6ed 1bc9039 015f1eb 1bc9039 e16c6ed 015f1eb e16c6ed 015f1eb 1bc9039 e16c6ed c14a77e 1bc9039 fcd33bb 02063d6 db80cf2 02063d6 03080db 02063d6 db80cf2 fcd33bb 9d7438a c0b7721 02063d6 9d7438a 02063d6 c0b7721 fcd33bb 9d7438a 02063d6 9d7438a fcd33bb 1bc9039 9d7438a adbdeee 02063d6 015f1eb 417ab3c eaa6fbd 109f376 417ab3c 1bc9039 417ab3c 109f376 1bc9039 aebe677 3519add 6949931 417ab3c 3519add e16c6ed 109f376 3519add 3a8a4ba 721b7bd 2c834ce 03080db 2c834ce 109f376 2c834ce 721b7bd c0b7721 3519add db80cf2 015f1eb 894ddd1 721b7bd 015f1eb db80cf2 ccd6adb 1bc9039 c0b7721 db80cf2 c0b7721 417ab3c 109f376 417ab3c c0b7721 | 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 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 | # pylint: disable=no-member, line-too-long
"""Gradio UI for the RAG'n'TeX LaTeX Presentation Generator."""
import base64
from typing import Any
import gradio as gr
from gradio_pdf import PDF
from ..generator import generate_presentation
from ..telemetry import submit_feedback
from .manage_files import download_files, upload_files
from .session_manager import (check_session_status, create_session,
with_update_session)
SESSION_TIMEOUT = 900
def update_config(key, value, config) -> dict:
"""Helper function to update configuration settings.
These settings will be later used for generate_presentation function.
Args:
key (str): Configuration key to update.
value (str): New value for the configuration key.
config (dict): Current configuration dictionary.
Returns:
dict: Updated configuration dictionary with the new key-value pair.
"""
config[key] = value
return config
def upload_and_update_list(
files: list, uploaded_list, session_id
) -> tuple[str, list[str]]:
"""Helper function to handle uploaded documents and update the list of uploaded files.
Args:
files (list): List of file-like objects to be uploaded.
uploaded_list (list): Current list of uploaded file paths.
session_id (str): Unique identifier for the current session.
Returns:
tuple: A 2-element tuple:
- str: Status message indicating the result of the upload operation.
- list[str]: Updated list of uploaded file paths."""
status, new_paths = upload_files(files, session_id)
updated_list = uploaded_list + [p for p in new_paths if p not in uploaded_list]
return status, updated_list
# def generate_iframe(folder_path) -> str:
# """Generate an HTML iframe to display the PDF presentation.
# Args:
# folder_path (str): Path to the folder containing the generated PDF presentation.
# Returns:
# str: HTML string containing the iframe to display the PDF.
# """
# file_path = f"{folder_path}/presentation.pdf"
# if not file_path:
# return ""
# with open(file_path, "rb") as f:
# base64_pdf = base64.b64encode(f.read()).decode("utf-8")
# pdf_display = f"""
# <div style="position:relative; width:100%; padding-top:76.5%;">
# <iframe src="data:application/pdf;base64,{base64_pdf}#view=FitH"
# style="position:absolute; top:0; left:0; width:100%; height:100%; border:none;">
# </iframe></div>
# """
def activate_preview(
browser_info: str, presentation_folder_state: str
) -> tuple[Any, Any]:
"""Activate the PDF preview in the viewer component.
Args:
browser_info (gr.State): State containing information about the user's browser.
presentation_folder_state (str): Path to the folder containing the generated PDF presentation.
Returns:
tuple[gr.Update, gr.Update]: Tuple containing updates for the PDF viewer and alert box visibility.
"""
if presentation_folder_state is None or not presentation_folder_state:
return gr.update(visible=False), gr.update(visible=False)
if "safari" in browser_info.lower() and "chrome" not in browser_info.lower():
return gr.update(visible=False), gr.update(visible=True)
else:
return gr.update(visible=True), gr.update(visible=False)
def encode_image(image_path) -> str:
"""Encode an image file to a base64 data URL.
Args:
image_path (str): Path to the image file.
Returns:
str: Base64 data URL of the image.
"""
with open(image_path, "rb") as f:
data = f.read()
return f"data:image/png;base64,{base64.b64encode(data).decode()}"
banner_base64 = encode_image("gfx/long_logo.png")
logo_base64 = encode_image("gfx/icon_logo.png")
github_base64 = encode_image("gfx/github-mark-white.png")
favicon_base64 = encode_image("gfx/favicon.ico")
# Custom Gradio theme
theme = gr.themes.Monochrome(
neutral_hue=gr.themes.Color(
c50="#f8f4fe",
c100="#ede9fe",
c200="#ddd6fe",
c300="#c4b5fd",
c400="#a78bfa",
c500="#8b5cf6",
c600="#7c3aed",
c700="#6d28d9",
c800="#5b21b6",
c900="#4c1d95",
c950="#181e44",
),
).set(body_background_fill="*neutral_50", body_text_color="*neutral_950")
# Additional JavaScript to handle session management and UI updates
JS_FUNC = r"""
() => {
// Force light theme
const url = new URL(window.location);
if (url.searchParams.get('__theme') !== 'light') {
url.searchParams.set('__theme', 'light');
window.location.href = url.href;
}
// Overlay for session expiration
const overlayDiv = document.createElement("div");
overlayDiv.id = "session-overlay";
overlayDiv.style = `
display: flex;
position: fixed;
top: 0; left: 0; width: 100vw; height: 100vh;
justify-content: center;
align-items: center;
background-color: rgba(0, 0, 0, 0.7);
color: white;
font-size: 1.5rem;
text-align: center;
z-index: 999999;
`;
overlayDiv.innerHTML = `
β Session expired.<br/>π Please refresh the page to start over.
`;
const checker = setInterval(() => {
const checkBtn = document.querySelector('#check-session-button');
if (checkBtn) checkBtn.click();
const statusBox = document.querySelector('#session-status textarea');
if (!statusBox) return;
const value = statusBox.value;
if (value === "expired") {
document.body.appendChild(overlayDiv);
console.log("Session expired, please refresh the page.");
clearInterval(checker);
}
}, 5000);
const browser = navigator.userAgent.toLowerCase();
const browser_element = document.querySelector('#browser_info textarea');
if (browser_element) {
browser_element.value = browser;
setTimeout(() => {
browser_element.dispatchEvent(new Event("input", { bubbles: true }));
browser_element.dispatchEvent(new Event("change", { bubbles: true }));
}, 1000);
}
}
"""
with gr.Blocks(theme=theme, js=JS_FUNC) as demo:
# Session management
session_id_state = gr.State()
session_timeout = gr.State(SESSION_TIMEOUT)
status_output = gr.Textbox(visible=False, elem_id="session-status")
check_btn = gr.Button(visible=False, elem_id="check-session-button")
check_btn.click(
fn=check_session_status,
inputs=[session_id_state],
outputs=[status_output],
)
###
uploaded_files_state = gr.State([])
presentation_folder_state = gr.State("")
browser_info = gr.Textbox(visible=False, elem_id="browser_info")
browser_info.change(fn=lambda x: None, inputs=browser_info, outputs=[])
# Page title and favicon (doesn't work? Gradio is really agressive)
gr.HTML(
f"""
<script>
document.title = "RAG'n'TeX Presentation Generator";
const links = document.querySelectorAll("link[rel*='icon']");
links.forEach(link => link.parentNode.removeChild(link));
const newFavicon = document.createElement('link');
newFavicon.rel = 'icon';
newFavicon.type = 'image/png';
newFavicon.href = '{favicon_base64}';
document.head.appendChild(newFavicon);
</script>
""",
elem_id="custom-head-injection",
visible=False,
)
# Mess with the download-box and preview-box
gr.HTML(
"""
<style>
#download-box svg.feather-file {
display: none !important;
}
</style>
""",
elem_id="custom-style-injection",
visible=False,
)
gr.Markdown(
'<h1 style="text-align:center;">AI-powered LaTeX Presentation Generator</h1>'
)
with gr.Row():
with gr.Column(scale=1):
gr.HTML(
f"""
<div style="text-align:center;">
<img src="{banner_base64}" style="max-width: 100%; height: auto;" />
</div>
"""
)
with gr.Column(scale=1):
gr.Markdown(
'<div style="text-align:justify; margin-top: 15px;">Turn your PDFs and ideas into beautiful LaTeX '
"presentations with AI. Built on top of the <a href=\"https://ragntex.github.io/lore/\">RAG'n'TeX</a>"
" engine, this tool automatically selects relevant content and compiles well-structured"
" slides. Ideal for students, researchers, and educators looking to save time and "
"create topic-focused presentations with minimal effort.</div>"
)
gr.Markdown(
'<div style="text-align:justify; margin-top: 0px;">Just upload your PDFs, click <b>Upload Files</b>, '
"choose a theme, enter your topic, and hit <b>Generate Presentation</b> β it's that easy!</div>"
)
gr.HTML(
f"""
<div style="display: flex; flex-direction: row; align-items: center; justify-content: center; gap: 20px; margin-bottom: 12px;">
<p style="font-size: 16px; line-height: 1; margin: 0;">Please βοΈ us on GitHub!</p>
<a href="https://github.com/RAGnTeX/RAGnTeX" target="_blank" style="
display: inline-flex;
align-items: center;
justify-content: center;
padding: 12px 16px;
background-color: #4c1d95;
color: white;
font-weight: 600;
font-size: 16px;
line-height: 1;
border-radius: 8px;
text-decoration: none;
gap: 8px;
transition: background-color 0.2s ease-in-out;
border: 2px solid transparent;
"
onmouseover="this.style.backgroundColor='#6d28d9'; this.style.border='2px solid #686868';"
onmouseout="this.style.backgroundColor='#4c1d95'; this.style.border='2px solid transparent';"
>
<img src="{github_base64}" alt="GitHub" style="height: 20px;">
RAG'n'TeX
</a>
</div>
"""
)
with gr.Row():
with gr.Column(scale=1):
gr.Markdown("## π Step 1: Upload PDF Documents")
file_input = gr.File(
file_types=[".pdf"], file_count="multiple", label="Select PDF Files"
)
upload_button = gr.Button("Upload Files", variant="primary")
gr.Markdown("## π¨ Step 2: Choose the parameters")
config_state = gr.State({})
model_state = gr.State("default")
model_radio = gr.Radio(
["gemini-2.0-flash", "gemini-2.5-flash-preview-05-20"],
label="LLM",
info="Choose Gemini version",
value="gemini-2.0-flash",
)
model_radio.change(
fn=lambda value, config: update_config("model_name", value, config),
inputs=[model_radio, config_state], # only real components here
outputs=config_state,
)
aspect_ratio_radio = gr.Radio(
["16:9", "4:3"],
label="Aspect Ratio",
info="Choose presentation aspect ratio",
value="16:9",
)
aspect_ratio_radio.change(
fn=lambda value, config: update_config("aspect_ratio", value, config),
inputs=[aspect_ratio_radio, config_state], # only real components here
outputs=config_state,
)
presentation_theme_state = gr.State("default")
theme_dropdown = gr.Dropdown(
choices=[
"default",
"Darmstadt",
"Malmoe",
"AnnArbor",
"Dresden",
"Marburg",
"Antibes",
"Frankfurt",
"Montpellier",
"Bergen",
"Goettingen",
"PaloAlto",
"Berkeley",
"Hannover",
"Pittsburgh",
"Berlin",
"Ilmenau",
"Rochester",
"Boadilla",
"JuanLesPins",
"Singapore",
"CambridgeUS",
"Luebeck",
"Szeged",
"Copenhagen",
"Madrid",
"Warsaw",
],
value="default", # default option
label="Choose a presentation theme",
)
theme_dropdown.change(
fn=lambda value, config: update_config("theme", value, config),
inputs=[theme_dropdown, config_state], # only real components here
outputs=config_state,
)
color_theme_state = gr.State("default")
color_dropdown = gr.Dropdown(
choices=[
"default",
"albatross",
"beetle",
"crane",
"dolphin",
"fly",
"lily",
"orchid",
"rose",
"seagull",
"seahorse",
"sidebartab",
"structure",
"whale",
"wolverine",
],
value="default", # default option
label="Choose a color theme",
)
color_dropdown.change(
fn=lambda value, config: update_config("color_theme", value, config),
inputs=[color_dropdown, config_state], # only real components here
outputs=config_state,
)
gr.Markdown("## π§ Step 3: Enter Presentation Topic")
topic_input = gr.Textbox(
label="Presentation Topic",
placeholder="e.g., Introduction to Quantum Computing",
lines=1,
)
topic_input.change(
fn=lambda value, config: update_config("topic", value, config),
inputs=[topic_input, config_state], # only real components here
outputs=config_state,
)
submit_topic_button = gr.Button("Generate Presentation", variant="primary")
with gr.Column(scale=1):
gr.Markdown("## π Uploaded Files")
upload_output = gr.Textbox(
label="Upload Status", lines=3, interactive=False
)
gr.Markdown("## π Presentation Processing")
compilation_status = gr.Textbox(
label="Compilation Status", lines=1, interactive=False
)
gr.Markdown("## π Final Presentation")
pdf_output = gr.File(
label="Download Presentation",
interactive=False,
elem_id="download-box",
file_count="multiple",
)
pdf_output_viewer = PDF(
label="Presentation Preview",
elem_id="preview-box",
visible=False,
)
browser_alert_box = gr.HTML(
"""
<div style="
border: 1px solid #ddd6fe;
background-color: #ffffff;
color: #181e44;
padding: 15px;
border-radius: 4px;
font-weight: bold;
max-width: 100%;
margin: 0px;
text-align: center;
">
β οΈ PDF preview is unavailable in Safari. Please download the PDF to view it.
</div>
""",
visible=False,
)
# pdf_output_viewer = gr.HTML(label="Presentation Preview")
trace_id_state = gr.State("")
gr.Markdown("<br>")
with gr.Row():
with gr.Column(scale=1):
gr.Markdown("## β Rate Us")
rating = gr.Radio(
choices=["βοΈ", "βοΈβοΈ", "βοΈβοΈβοΈ", "βοΈβοΈβοΈβοΈ", "βοΈβοΈβοΈβοΈβοΈ"],
label="How would you rate this app?",
interactive=True,
type="value",
value=None,
show_label=True,
container=True,
)
feedback_comment = gr.Textbox(
label="Give us a comment (optional)",
placeholder="What did you like or what can be improved?",
lines=3,
max_lines=5,
interactive=True,
)
submit_feedback_button = gr.Button("Submit Feedback", variant="secondary")
feedback_output = gr.Textbox(
label="Feedback Status", interactive=False, lines=2
)
with gr.Column(scale=1):
gr.Markdown("## π Credits")
gr.HTML(
f"""
<div style="display: flex; flex-direction: row; gap: 24px; margin-top: 0; align-items: flex-start;">
<!-- Left Column -->
<div style="flex: 1;">
<div style="text-align:justify; margin-top: 0px;">
<p> RAG'n'TeX is a collaborative effort by a team of developers and researchers driven
to make the everyday work of scientists and professionals easier. By combining LaTeX with AI,
we create smart tools that help you build polished, accurate presentations quickly and smoothly.
Our goal is to make the process simple and stress-free, so you can spend more time on what really matters.</p>
<br>
<p>π Licensed under the <a href="https://opensource.org/licenses/MIT" target="_blank" rel="noopener noreferrer">MIT License</a></p>
<br>
<h4>Authors:</h4>
<ul style="padding-left: 20px; margin-top: 4px;">
<li>π©π»βπ» <a href="https://github.com/AnnaErsh" target="_blank">Anna Ershova</a></li>
<li>π¨πΌβπ¬ <a href="https://github.com/KajetanNiewczas" target="_blank">Kajetan Niewczas</a></li>
</ul>
</div>
</div>
<!-- Right Column -->
<div style="flex: 1; display: flex; justify-content: flex-end; align-items: center;">
<img src="{logo_base64}" alt="Illustration" style="max-width: 100%; border-radius: 8px;">
</div>
</div>
"""
)
# Event handlers
demo.load(
fn=create_session,
inputs=session_timeout,
outputs=session_id_state,
)
upload_button.click(
fn=with_update_session(upload_and_update_list),
inputs=[file_input, uploaded_files_state, session_id_state],
outputs=[upload_output, uploaded_files_state],
)
trace_id_state = gr.State("")
submit_topic_button.click(
fn=with_update_session(lambda x: ""),
inputs=session_id_state,
outputs=compilation_status,
).then(
fn=with_update_session(generate_presentation),
inputs=[
config_state,
session_id_state,
],
outputs=[compilation_status, trace_id_state, presentation_folder_state],
)
compilation_status.change(
fn=download_files,
inputs=[
compilation_status,
presentation_folder_state,
session_id_state,
],
outputs=pdf_output,
)
browser_info.change(fn=lambda x: None, inputs=browser_info, outputs=[])
pdf_output.change(
fn=lambda folder: f"{folder}/presentation.pdf" if folder is not None else None,
inputs=presentation_folder_state,
outputs=pdf_output_viewer,
).then(
fn=activate_preview,
inputs=[browser_info, presentation_folder_state],
outputs=[pdf_output_viewer, browser_alert_box],
)
# pdf_output.change(
# fn=generate_iframe,
# inputs=presentation_folder_state,
# outputs=pdf_output_viewer,
# )
submit_feedback_button.click(
fn=with_update_session(submit_feedback),
inputs=[
rating,
feedback_comment,
trace_id_state,
session_id_state,
],
outputs=feedback_output,
)
|