Merge branch 'main' of https://github.com/AnnaErsh/RAGnTeX
Browse filesThis view is limited to 50 files because it contains too many changes. See raw diff
- .dockerignore +7 -0
- .github/workflows/check.yml +16 -0
- .github/workflows/ci.yml +63 -0
- .github/workflows/hf_sync.yml +19 -0
- Dockerfile +42 -0
- LICENSE +21 -0
- README.md +30 -0
- docs/Makefile +20 -0
- docs/conf.py +40 -0
- docs/index.rst +104 -0
- docs/make.bat +35 -0
- docs/modules.rst +7 -0
- docs/source/modules.rst +8 -0
- docs/source/ragntex.rst +7 -0
- docs/source/src.compilation.rst +29 -0
- docs/source/src.database.rst +29 -0
- docs/source/src.interface.rst +37 -0
- docs/source/src.processing.rst +37 -0
- docs/source/src.rst +34 -0
- docs/source/src.services.rst +37 -0
- docs/source/src.telemetry.rst +29 -0
- docs/src.compilation.rst +29 -0
- docs/src.database.rst +29 -0
- docs/src.interface.rst +37 -0
- docs/src.processing.rst +37 -0
- docs/src.rst +34 -0
- docs/src.services.rst +37 -0
- docs/src.telemetry.rst +29 -0
- gfx/favicon.ico +0 -0
- gfx/github-mark-white.png +3 -0
- gfx/icon_logo.png +3 -0
- gfx/long_logo.png +3 -0
- mypy.ini +5 -0
- poetry.lock +0 -0
- pylint-output.txt +4 -0
- pyproject.toml +21 -2
- ragntex.py +12 -131
- src/__init__.py +8 -17
- src/compilation/__init__.py +6 -2
- src/compilation/json_to_tex.py +113 -0
- src/compilation/latex_compilation.py +56 -17
- src/compilation/latex_tools.py +32 -0
- src/database/__init__.py +6 -2
- src/database/database.py +9 -9
- src/database/db_manipulation.py +119 -0
- src/generator.py +95 -0
- src/interface/__init__.py +14 -1
- src/interface/gradio_interface.py +506 -0
- src/interface/manage_files.py +108 -0
- src/interface/session_manager.py +123 -0
.dockerignore
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
__pycache__
|
| 2 |
+
*.pyc
|
| 3 |
+
*.pyo
|
| 4 |
+
*.pyd
|
| 5 |
+
*.db
|
| 6 |
+
.env
|
| 7 |
+
.venv
|
.github/workflows/check.yml
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
name: Check file size
|
| 2 |
+
on:
|
| 3 |
+
pull_request:
|
| 4 |
+
branches: [main]
|
| 5 |
+
|
| 6 |
+
# to run this workflow manually from the Actions tab
|
| 7 |
+
workflow_dispatch:
|
| 8 |
+
|
| 9 |
+
jobs:
|
| 10 |
+
sync-to-hub:
|
| 11 |
+
runs-on: ubuntu-latest
|
| 12 |
+
steps:
|
| 13 |
+
- name: Check large files
|
| 14 |
+
uses: ActionsDesk/lfs-warning@v2.0
|
| 15 |
+
with:
|
| 16 |
+
filesizelimit: 10485760 # this is 10MB
|
.github/workflows/ci.yml
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
name: CI
|
| 2 |
+
|
| 3 |
+
on:
|
| 4 |
+
push:
|
| 5 |
+
branches: [main, dev]
|
| 6 |
+
pull_request:
|
| 7 |
+
branches: [main, dev]
|
| 8 |
+
|
| 9 |
+
jobs:
|
| 10 |
+
lint:
|
| 11 |
+
runs-on: ubuntu-latest
|
| 12 |
+
|
| 13 |
+
steps:
|
| 14 |
+
- uses: actions/checkout@v4
|
| 15 |
+
|
| 16 |
+
- name: Set up Python
|
| 17 |
+
uses: actions/setup-python@v5
|
| 18 |
+
with:
|
| 19 |
+
python-version: "3.10"
|
| 20 |
+
|
| 21 |
+
- name: Install Poetry
|
| 22 |
+
uses: abatilo/actions-poetry@v2
|
| 23 |
+
with:
|
| 24 |
+
poetry-version: "2.1.1"
|
| 25 |
+
|
| 26 |
+
- name: Verify Poetry Installation
|
| 27 |
+
run: poetry --version
|
| 28 |
+
|
| 29 |
+
- name: Install dependencies
|
| 30 |
+
run: poetry install --with dev
|
| 31 |
+
|
| 32 |
+
- name: Run Black and isort
|
| 33 |
+
run: |
|
| 34 |
+
poetry run black .
|
| 35 |
+
poetry run isort .
|
| 36 |
+
|
| 37 |
+
- name: Commit and push changes
|
| 38 |
+
uses: stefanzweifel/git-auto-commit-action@v5
|
| 39 |
+
with:
|
| 40 |
+
commit_message: "style: Automatic formatting via CI (Black + isort)"
|
| 41 |
+
branch: ${{ github.head_ref || github.ref_name }}
|
| 42 |
+
file_pattern: "*.py"
|
| 43 |
+
|
| 44 |
+
- name: Run Mypy
|
| 45 |
+
run: poetry run mypy .
|
| 46 |
+
|
| 47 |
+
- name: Run Pylint
|
| 48 |
+
run: |
|
| 49 |
+
poetry run pylint . | tee pylint-output.txt
|
| 50 |
+
score=$(tail -n 2 pylint-output.txt | grep -oP "rated at \K[0-9.]+")
|
| 51 |
+
echo "Pylint score: $score"
|
| 52 |
+
awk -v s="$score" 'BEGIN {exit (s >= 9.5) ? 0 : 1}'
|
| 53 |
+
|
| 54 |
+
- name: Run Bandit security scan
|
| 55 |
+
run: poetry run bandit -r src/
|
| 56 |
+
|
| 57 |
+
- name: Run Safety vulnerability check (warnings only)
|
| 58 |
+
run: |
|
| 59 |
+
poetry run safety check --full-report || true
|
| 60 |
+
|
| 61 |
+
- name: Build docs
|
| 62 |
+
working-directory: docs
|
| 63 |
+
run: poetry run sphinx-build -b html . _build/html
|
.github/workflows/hf_sync.yml
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
name: Sync to Hugging Face hub
|
| 2 |
+
on:
|
| 3 |
+
push:
|
| 4 |
+
branches: [main]
|
| 5 |
+
# to run this workflow manually from the Actions tab
|
| 6 |
+
workflow_dispatch:
|
| 7 |
+
|
| 8 |
+
jobs:
|
| 9 |
+
sync-to-hub:
|
| 10 |
+
runs-on: ubuntu-latest
|
| 11 |
+
steps:
|
| 12 |
+
- uses: actions/checkout@v3
|
| 13 |
+
with:
|
| 14 |
+
fetch-depth: 0
|
| 15 |
+
lfs: true
|
| 16 |
+
- name: Push to hub
|
| 17 |
+
env:
|
| 18 |
+
HF_SECRET: ${{ secrets.HF_SECRET }}
|
| 19 |
+
run: git push https://eranmi:$HF_SECRET@huggingface.co/spaces/eranmi/RAGnTeX main
|
Dockerfile
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Use official Python base image
|
| 2 |
+
FROM python:3.10-slim
|
| 3 |
+
|
| 4 |
+
# Set environment variables
|
| 5 |
+
ENV PYTHONDONTWRITEBYTECODE=1
|
| 6 |
+
ENV PYTHONUNBUFFERED=1
|
| 7 |
+
|
| 8 |
+
# Set work directory
|
| 9 |
+
WORKDIR /app
|
| 10 |
+
|
| 11 |
+
# Install system dependencies
|
| 12 |
+
RUN apt-get update && apt-get install -y \
|
| 13 |
+
build-essential \
|
| 14 |
+
libgl1-mesa-glx \
|
| 15 |
+
libglib2.0-0 \
|
| 16 |
+
texlive-latex-base \
|
| 17 |
+
texlive-fonts-recommended \
|
| 18 |
+
texlive-latex-extra \
|
| 19 |
+
&& rm -rf /var/lib/apt/lists/*
|
| 20 |
+
|
| 21 |
+
# Clean up
|
| 22 |
+
RUN rm -rf /var/lib/apt/lists/*
|
| 23 |
+
|
| 24 |
+
# Install Poetry
|
| 25 |
+
RUN pip install --upgrade pip
|
| 26 |
+
RUN pip install poetry
|
| 27 |
+
|
| 28 |
+
# Copy only poetry files first (for caching)
|
| 29 |
+
COPY pyproject.toml poetry.lock* /app/
|
| 30 |
+
|
| 31 |
+
# Install dependencies
|
| 32 |
+
RUN poetry config virtualenvs.create false \
|
| 33 |
+
&& poetry install --no-root --no-interaction --no-ansi
|
| 34 |
+
|
| 35 |
+
# Copy the rest of the application
|
| 36 |
+
COPY . /app
|
| 37 |
+
|
| 38 |
+
# Expose Gradio port
|
| 39 |
+
EXPOSE 7860
|
| 40 |
+
|
| 41 |
+
# Command to run the app
|
| 42 |
+
CMD ["python", "ragntex.py"]
|
LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
MIT License
|
| 2 |
+
|
| 3 |
+
Copyright (c) 2025 RAGnTeX
|
| 4 |
+
|
| 5 |
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
| 6 |
+
of this software and associated documentation files (the "Software"), to deal
|
| 7 |
+
in the Software without restriction, including without limitation the rights
|
| 8 |
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
| 9 |
+
copies of the Software, and to permit persons to whom the Software is
|
| 10 |
+
furnished to do so, subject to the following conditions:
|
| 11 |
+
|
| 12 |
+
The above copyright notice and this permission notice shall be included in all
|
| 13 |
+
copies or substantial portions of the Software.
|
| 14 |
+
|
| 15 |
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
| 16 |
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
| 17 |
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
| 18 |
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
| 19 |
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
| 20 |
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
| 21 |
+
SOFTWARE.
|
README.md
CHANGED
|
@@ -1,3 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
# 📚 From Text to Visuals: Auto-Generating LaTeX Beamer Presentations with GenAI
|
| 2 |
|
| 3 |
In this project, we explore how generative AI can automate the creation of professional-looking presentation slides—directly from extensive collections of PDF documents.
|
|
@@ -42,3 +51,24 @@ The model outputs fully formatted compilable LaTeX code following a clear and co
|
|
| 42 |
- **Summary**: Key takeaways
|
| 43 |
|
| 44 |
It also incorporates relevant images extracted from the documents, organizing them into LaTeX Beamer-friendly layouts (e.g., *Core Idea 2* and *Core Idea 3* slide formats).
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: RAGnTeX
|
| 3 |
+
emoji: 📄
|
| 4 |
+
colorFrom: indigo
|
| 5 |
+
colorTo: pink
|
| 6 |
+
sdk: docker
|
| 7 |
+
pinned: false
|
| 8 |
+
---
|
| 9 |
+
|
| 10 |
# 📚 From Text to Visuals: Auto-Generating LaTeX Beamer Presentations with GenAI
|
| 11 |
|
| 12 |
In this project, we explore how generative AI can automate the creation of professional-looking presentation slides—directly from extensive collections of PDF documents.
|
|
|
|
| 51 |
- **Summary**: Key takeaways
|
| 52 |
|
| 53 |
It also incorporates relevant images extracted from the documents, organizing them into LaTeX Beamer-friendly layouts (e.g., *Core Idea 2* and *Core Idea 3* slide formats).
|
| 54 |
+
|
| 55 |
+
### Prerequisites
|
| 56 |
+
|
| 57 |
+
This project requires the LaTeX toolchain, specifically `pdflatex`, to generate PDF presentations.
|
| 58 |
+
|
| 59 |
+
Please install it before running the project:
|
| 60 |
+
|
| 61 |
+
### On Debian/Ubuntu:
|
| 62 |
+
|
| 63 |
+
```bash
|
| 64 |
+
sudo apt-get update && sudo apt-get install -y texlive-latex-base texlive-fonts-recommended texlive-latex-extra
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
### Run docker:
|
| 68 |
+
```bash
|
| 69 |
+
docker run \
|
| 70 |
+
--env-file .env \
|
| 71 |
+
-e IN_DOCKER=true \
|
| 72 |
+
-p 7860:7860 \
|
| 73 |
+
-v /path/to/your/output:/app/output \
|
| 74 |
+
ragntex
|
docs/Makefile
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Minimal makefile for Sphinx documentation
|
| 2 |
+
#
|
| 3 |
+
|
| 4 |
+
# You can set these variables from the command line, and also
|
| 5 |
+
# from the environment for the first two.
|
| 6 |
+
SPHINXOPTS ?=
|
| 7 |
+
SPHINXBUILD ?= sphinx-build
|
| 8 |
+
SOURCEDIR = .
|
| 9 |
+
BUILDDIR = _build
|
| 10 |
+
|
| 11 |
+
# Put it first so that "make" without argument is like "make help".
|
| 12 |
+
help:
|
| 13 |
+
@$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
|
| 14 |
+
|
| 15 |
+
.PHONY: help Makefile
|
| 16 |
+
|
| 17 |
+
# Catch-all target: route all unknown targets to Sphinx using the new
|
| 18 |
+
# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS).
|
| 19 |
+
%: Makefile
|
| 20 |
+
@$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
|
docs/conf.py
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Configuration file for the Sphinx documentation builder.
|
| 2 |
+
#
|
| 3 |
+
# For the full list of built-in configuration values, see the documentation:
|
| 4 |
+
# https://www.sphinx-doc.org/en/master/usage/configuration.html
|
| 5 |
+
|
| 6 |
+
# -- Project information -----------------------------------------------------
|
| 7 |
+
# https://www.sphinx-doc.org/en/master/usage/configuration.html#project-information
|
| 8 |
+
import os
|
| 9 |
+
import sys
|
| 10 |
+
|
| 11 |
+
project = "RAGnTeX"
|
| 12 |
+
copyright = "2025, Anna Ershova, Kajetan Niewczas"
|
| 13 |
+
author = "Anna Ershova, Kajetan Niewczas"
|
| 14 |
+
# release = "0.1.0"
|
| 15 |
+
|
| 16 |
+
# ── allow “import ragntex …” ────────────────────────────────────────────────
|
| 17 |
+
sys.path.insert(0, os.path.abspath("../src"))
|
| 18 |
+
# -- General configuration ---------------------------------------------------
|
| 19 |
+
# https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration
|
| 20 |
+
|
| 21 |
+
extensions = [
|
| 22 |
+
"sphinx.ext.autodoc",
|
| 23 |
+
"sphinx.ext.napoleon",
|
| 24 |
+
"sphinx_autodoc_typehints",
|
| 25 |
+
]
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
templates_path = ["_templates"]
|
| 29 |
+
exclude_patterns = ["_build", "Thumbs.db", ".DS_Store"]
|
| 30 |
+
|
| 31 |
+
# -- Options for Napoleon ----------------------------------------------------
|
| 32 |
+
|
| 33 |
+
napoleon_google_docstring = True
|
| 34 |
+
napoleon_numpy_docstring = False
|
| 35 |
+
|
| 36 |
+
# -- Options for HTML output -------------------------------------------------
|
| 37 |
+
# https://www.sphinx-doc.org/en/master/usage/configuration.html#options-for-html-output
|
| 38 |
+
|
| 39 |
+
html_theme = "furo"
|
| 40 |
+
html_static_path = ["_static"]
|
docs/index.rst
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
.. RAGnTeX documentation master file, created by
|
| 2 |
+
sphinx-quickstart on Fri Jun 13 12:43:29 2025.
|
| 3 |
+
You can adapt this file completely to your liking, but it should at least
|
| 4 |
+
contain the root `toctree` directive.
|
| 5 |
+
RAGnTeX documentation
|
| 6 |
+
=====================
|
| 7 |
+
|
| 8 |
+
📚 From Text to Visuals: Auto-Generating LaTeX Beamer Presentations with GenAI
|
| 9 |
+
------------------------------------------------------------------------------
|
| 10 |
+
|
| 11 |
+
In this project, we explore how generative AI can automate the creation of professional-looking presentation slides—directly from extensive collections of PDF documents.
|
| 12 |
+
|
| 13 |
+
---
|
| 14 |
+
|
| 15 |
+
🧠 Use Case
|
| 16 |
+
===========
|
| 17 |
+
|
| 18 |
+
Creating slide decks from dense documents (like whitepapers or scientific articles) is a time-consuming and cognitively heavy task. Our goal is to streamline this process using generative AI.
|
| 19 |
+
|
| 20 |
+
We built an AI assistant that transforms document collections into LaTeX Beamer presentations—complete with structure, content, and visuals—based on a user-defined topic.
|
| 21 |
+
|
| 22 |
+
---
|
| 23 |
+
|
| 24 |
+
🔍 How It Works
|
| 25 |
+
==============
|
| 26 |
+
|
| 27 |
+
💿 Database Creation
|
| 28 |
+
-------------------
|
| 29 |
+
|
| 30 |
+
We start by creating a vector database from a collection of PDF documents (e.g., arXiv papers). Each document is processed to extract text chunks, and for each chunk, we store:
|
| 31 |
+
|
| 32 |
+
- **Embeddings** of the text from the file
|
| 33 |
+
- **Associated metadata** (like images, their captions, how many images there are)
|
| 34 |
+
|
| 35 |
+
🗣️ User Prompt
|
| 36 |
+
--------------
|
| 37 |
+
|
| 38 |
+
A user enters a natural language query like:
|
| 39 |
+
|
| 40 |
+
::
|
| 41 |
+
|
| 42 |
+
"I need a presentation about AI agents."
|
| 43 |
+
|
| 44 |
+
📄 Document Retrieval
|
| 45 |
+
--------------------
|
| 46 |
+
|
| 47 |
+
We use **Chroma** as our vector database to retrieve the most relevant PDF documents based on the query.
|
| 48 |
+
|
| 49 |
+
🧠 Content Understanding
|
| 50 |
+
------------------------
|
| 51 |
+
|
| 52 |
+
Using **Gemini 2.0 Flash** (``google-genai==1.7.0``), the assistant analyzes the retrieved content and extracts the most relevant ideas and insights.
|
| 53 |
+
|
| 54 |
+
🎞️ Slide Generation (LaTeX Beamer)
|
| 55 |
+
----------------------------------
|
| 56 |
+
|
| 57 |
+
The model outputs fully formatted compilable LaTeX code following a clear and consistent structure:
|
| 58 |
+
|
| 59 |
+
- **Introduction**: Topic overview and motivation
|
| 60 |
+
- **Main Content**: 2–4 slides, each presenting a core idea
|
| 61 |
+
- **Summary**: Key takeaways
|
| 62 |
+
|
| 63 |
+
It also incorporates relevant images extracted from the documents, organizing them into LaTeX Beamer-friendly layouts (e.g., *Core Idea 2* and *Core Idea 3* slide formats).
|
| 64 |
+
|
| 65 |
+
Installation
|
| 66 |
+
============
|
| 67 |
+
|
| 68 |
+
Install poetry to manage dependencies and virtual environments:
|
| 69 |
+
|
| 70 |
+
.. code-block:: bash
|
| 71 |
+
|
| 72 |
+
pip install poetry
|
| 73 |
+
|
| 74 |
+
Then, install the project dependencies:
|
| 75 |
+
|
| 76 |
+
.. code-block:: bash
|
| 77 |
+
|
| 78 |
+
poetry install
|
| 79 |
+
|
| 80 |
+
Running
|
| 81 |
+
=======
|
| 82 |
+
|
| 83 |
+
Run project with:
|
| 84 |
+
|
| 85 |
+
.. code-block:: bash
|
| 86 |
+
|
| 87 |
+
poetry run python ragntex.py
|
| 88 |
+
|
| 89 |
+
Or via Docker:
|
| 90 |
+
|
| 91 |
+
.. code-block:: bash
|
| 92 |
+
|
| 93 |
+
docker run \
|
| 94 |
+
--env-file .env \
|
| 95 |
+
-e IN_DOCKER=true \
|
| 96 |
+
-p 7860:7860 \
|
| 97 |
+
-v /path/to/your/output:/app/output \
|
| 98 |
+
ragntex
|
| 99 |
+
|
| 100 |
+
.. toctree::
|
| 101 |
+
:maxdepth: 2
|
| 102 |
+
:caption: Contents:
|
| 103 |
+
|
| 104 |
+
src
|
docs/make.bat
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
@ECHO OFF
|
| 2 |
+
|
| 3 |
+
pushd %~dp0
|
| 4 |
+
|
| 5 |
+
REM Command file for Sphinx documentation
|
| 6 |
+
|
| 7 |
+
if "%SPHINXBUILD%" == "" (
|
| 8 |
+
set SPHINXBUILD=sphinx-build
|
| 9 |
+
)
|
| 10 |
+
set SOURCEDIR=.
|
| 11 |
+
set BUILDDIR=_build
|
| 12 |
+
|
| 13 |
+
%SPHINXBUILD% >NUL 2>NUL
|
| 14 |
+
if errorlevel 9009 (
|
| 15 |
+
echo.
|
| 16 |
+
echo.The 'sphinx-build' command was not found. Make sure you have Sphinx
|
| 17 |
+
echo.installed, then set the SPHINXBUILD environment variable to point
|
| 18 |
+
echo.to the full path of the 'sphinx-build' executable. Alternatively you
|
| 19 |
+
echo.may add the Sphinx directory to PATH.
|
| 20 |
+
echo.
|
| 21 |
+
echo.If you don't have Sphinx installed, grab it from
|
| 22 |
+
echo.https://www.sphinx-doc.org/
|
| 23 |
+
exit /b 1
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
if "%1" == "" goto help
|
| 27 |
+
|
| 28 |
+
%SPHINXBUILD% -M %1 %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O%
|
| 29 |
+
goto end
|
| 30 |
+
|
| 31 |
+
:help
|
| 32 |
+
%SPHINXBUILD% -M help %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O%
|
| 33 |
+
|
| 34 |
+
:end
|
| 35 |
+
popd
|
docs/modules.rst
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
src
|
| 2 |
+
===
|
| 3 |
+
|
| 4 |
+
.. toctree::
|
| 5 |
+
:maxdepth: 4
|
| 6 |
+
|
| 7 |
+
src
|
docs/source/modules.rst
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
RAGnTeX
|
| 2 |
+
=======
|
| 3 |
+
|
| 4 |
+
.. toctree::
|
| 5 |
+
:maxdepth: 4
|
| 6 |
+
|
| 7 |
+
ragntex
|
| 8 |
+
src
|
docs/source/ragntex.rst
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
ragntex module
|
| 2 |
+
==============
|
| 3 |
+
|
| 4 |
+
.. automodule:: ragntex
|
| 5 |
+
:members:
|
| 6 |
+
:undoc-members:
|
| 7 |
+
:show-inheritance:
|
docs/source/src.compilation.rst
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
src.compilation package
|
| 2 |
+
=======================
|
| 3 |
+
|
| 4 |
+
Submodules
|
| 5 |
+
----------
|
| 6 |
+
|
| 7 |
+
src.compilation.json\_to\_tex module
|
| 8 |
+
------------------------------------
|
| 9 |
+
|
| 10 |
+
.. automodule:: src.compilation.json_to_tex
|
| 11 |
+
:members:
|
| 12 |
+
:undoc-members:
|
| 13 |
+
:show-inheritance:
|
| 14 |
+
|
| 15 |
+
src.compilation.latex\_compilation module
|
| 16 |
+
-----------------------------------------
|
| 17 |
+
|
| 18 |
+
.. automodule:: src.compilation.latex_compilation
|
| 19 |
+
:members:
|
| 20 |
+
:undoc-members:
|
| 21 |
+
:show-inheritance:
|
| 22 |
+
|
| 23 |
+
Module contents
|
| 24 |
+
---------------
|
| 25 |
+
|
| 26 |
+
.. automodule:: src.compilation
|
| 27 |
+
:members:
|
| 28 |
+
:undoc-members:
|
| 29 |
+
:show-inheritance:
|
docs/source/src.database.rst
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
src.database package
|
| 2 |
+
====================
|
| 3 |
+
|
| 4 |
+
Submodules
|
| 5 |
+
----------
|
| 6 |
+
|
| 7 |
+
src.database.database module
|
| 8 |
+
----------------------------
|
| 9 |
+
|
| 10 |
+
.. automodule:: src.database.database
|
| 11 |
+
:members:
|
| 12 |
+
:undoc-members:
|
| 13 |
+
:show-inheritance:
|
| 14 |
+
|
| 15 |
+
src.database.db\_manipulation module
|
| 16 |
+
------------------------------------
|
| 17 |
+
|
| 18 |
+
.. automodule:: src.database.db_manipulation
|
| 19 |
+
:members:
|
| 20 |
+
:undoc-members:
|
| 21 |
+
:show-inheritance:
|
| 22 |
+
|
| 23 |
+
Module contents
|
| 24 |
+
---------------
|
| 25 |
+
|
| 26 |
+
.. automodule:: src.database
|
| 27 |
+
:members:
|
| 28 |
+
:undoc-members:
|
| 29 |
+
:show-inheritance:
|
docs/source/src.interface.rst
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
src.interface package
|
| 2 |
+
=====================
|
| 3 |
+
|
| 4 |
+
Submodules
|
| 5 |
+
----------
|
| 6 |
+
|
| 7 |
+
src.interface.download\_files module
|
| 8 |
+
------------------------------------
|
| 9 |
+
|
| 10 |
+
.. automodule:: src.interface.download_files
|
| 11 |
+
:members:
|
| 12 |
+
:undoc-members:
|
| 13 |
+
:show-inheritance:
|
| 14 |
+
|
| 15 |
+
src.interface.gradio\_interface module
|
| 16 |
+
--------------------------------------
|
| 17 |
+
|
| 18 |
+
.. automodule:: src.interface.gradio_interface
|
| 19 |
+
:members:
|
| 20 |
+
:undoc-members:
|
| 21 |
+
:show-inheritance:
|
| 22 |
+
|
| 23 |
+
src.interface.upload\_files module
|
| 24 |
+
----------------------------------
|
| 25 |
+
|
| 26 |
+
.. automodule:: src.interface.upload_files
|
| 27 |
+
:members:
|
| 28 |
+
:undoc-members:
|
| 29 |
+
:show-inheritance:
|
| 30 |
+
|
| 31 |
+
Module contents
|
| 32 |
+
---------------
|
| 33 |
+
|
| 34 |
+
.. automodule:: src.interface
|
| 35 |
+
:members:
|
| 36 |
+
:undoc-members:
|
| 37 |
+
:show-inheritance:
|
docs/source/src.processing.rst
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
src.processing package
|
| 2 |
+
======================
|
| 3 |
+
|
| 4 |
+
Submodules
|
| 5 |
+
----------
|
| 6 |
+
|
| 7 |
+
src.processing.document\_processing module
|
| 8 |
+
------------------------------------------
|
| 9 |
+
|
| 10 |
+
.. automodule:: src.processing.document_processing
|
| 11 |
+
:members:
|
| 12 |
+
:undoc-members:
|
| 13 |
+
:show-inheritance:
|
| 14 |
+
|
| 15 |
+
src.processing.images\_processing module
|
| 16 |
+
----------------------------------------
|
| 17 |
+
|
| 18 |
+
.. automodule:: src.processing.images_processing
|
| 19 |
+
:members:
|
| 20 |
+
:undoc-members:
|
| 21 |
+
:show-inheritance:
|
| 22 |
+
|
| 23 |
+
src.processing.output\_folder module
|
| 24 |
+
------------------------------------
|
| 25 |
+
|
| 26 |
+
.. automodule:: src.processing.output_folder
|
| 27 |
+
:members:
|
| 28 |
+
:undoc-members:
|
| 29 |
+
:show-inheritance:
|
| 30 |
+
|
| 31 |
+
Module contents
|
| 32 |
+
---------------
|
| 33 |
+
|
| 34 |
+
.. automodule:: src.processing
|
| 35 |
+
:members:
|
| 36 |
+
:undoc-members:
|
| 37 |
+
:show-inheritance:
|
docs/source/src.rst
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
src package
|
| 2 |
+
===========
|
| 3 |
+
|
| 4 |
+
Subpackages
|
| 5 |
+
-----------
|
| 6 |
+
|
| 7 |
+
.. toctree::
|
| 8 |
+
:maxdepth: 4
|
| 9 |
+
|
| 10 |
+
src.compilation
|
| 11 |
+
src.database
|
| 12 |
+
src.interface
|
| 13 |
+
src.processing
|
| 14 |
+
src.services
|
| 15 |
+
src.telemetry
|
| 16 |
+
|
| 17 |
+
Submodules
|
| 18 |
+
----------
|
| 19 |
+
|
| 20 |
+
src.generator module
|
| 21 |
+
--------------------
|
| 22 |
+
|
| 23 |
+
.. automodule:: src.generator
|
| 24 |
+
:members:
|
| 25 |
+
:undoc-members:
|
| 26 |
+
:show-inheritance:
|
| 27 |
+
|
| 28 |
+
Module contents
|
| 29 |
+
---------------
|
| 30 |
+
|
| 31 |
+
.. automodule:: src
|
| 32 |
+
:members:
|
| 33 |
+
:undoc-members:
|
| 34 |
+
:show-inheritance:
|
docs/source/src.services.rst
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
src.services package
|
| 2 |
+
====================
|
| 3 |
+
|
| 4 |
+
Submodules
|
| 5 |
+
----------
|
| 6 |
+
|
| 7 |
+
src.services.google\_client module
|
| 8 |
+
----------------------------------
|
| 9 |
+
|
| 10 |
+
.. automodule:: src.services.google_client
|
| 11 |
+
:members:
|
| 12 |
+
:undoc-members:
|
| 13 |
+
:show-inheritance:
|
| 14 |
+
|
| 15 |
+
src.services.output\_schema module
|
| 16 |
+
----------------------------------
|
| 17 |
+
|
| 18 |
+
.. automodule:: src.services.output_schema
|
| 19 |
+
:members:
|
| 20 |
+
:undoc-members:
|
| 21 |
+
:show-inheritance:
|
| 22 |
+
|
| 23 |
+
src.services.prompt module
|
| 24 |
+
--------------------------
|
| 25 |
+
|
| 26 |
+
.. automodule:: src.services.prompt
|
| 27 |
+
:members:
|
| 28 |
+
:undoc-members:
|
| 29 |
+
:show-inheritance:
|
| 30 |
+
|
| 31 |
+
Module contents
|
| 32 |
+
---------------
|
| 33 |
+
|
| 34 |
+
.. automodule:: src.services
|
| 35 |
+
:members:
|
| 36 |
+
:undoc-members:
|
| 37 |
+
:show-inheritance:
|
docs/source/src.telemetry.rst
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
src.telemetry package
|
| 2 |
+
=====================
|
| 3 |
+
|
| 4 |
+
Submodules
|
| 5 |
+
----------
|
| 6 |
+
|
| 7 |
+
src.telemetry.logging\_utils module
|
| 8 |
+
-----------------------------------
|
| 9 |
+
|
| 10 |
+
.. automodule:: src.telemetry.logging_utils
|
| 11 |
+
:members:
|
| 12 |
+
:undoc-members:
|
| 13 |
+
:show-inheritance:
|
| 14 |
+
|
| 15 |
+
src.telemetry.telemetry module
|
| 16 |
+
------------------------------
|
| 17 |
+
|
| 18 |
+
.. automodule:: src.telemetry.telemetry
|
| 19 |
+
:members:
|
| 20 |
+
:undoc-members:
|
| 21 |
+
:show-inheritance:
|
| 22 |
+
|
| 23 |
+
Module contents
|
| 24 |
+
---------------
|
| 25 |
+
|
| 26 |
+
.. automodule:: src.telemetry
|
| 27 |
+
:members:
|
| 28 |
+
:undoc-members:
|
| 29 |
+
:show-inheritance:
|
docs/src.compilation.rst
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
src.compilation package
|
| 2 |
+
=======================
|
| 3 |
+
|
| 4 |
+
Submodules
|
| 5 |
+
----------
|
| 6 |
+
|
| 7 |
+
src.compilation.json\_to\_tex module
|
| 8 |
+
------------------------------------
|
| 9 |
+
|
| 10 |
+
.. automodule:: src.compilation.json_to_tex
|
| 11 |
+
:members:
|
| 12 |
+
:undoc-members:
|
| 13 |
+
:show-inheritance:
|
| 14 |
+
|
| 15 |
+
src.compilation.latex\_compilation module
|
| 16 |
+
-----------------------------------------
|
| 17 |
+
|
| 18 |
+
.. automodule:: src.compilation.latex_compilation
|
| 19 |
+
:members:
|
| 20 |
+
:undoc-members:
|
| 21 |
+
:show-inheritance:
|
| 22 |
+
|
| 23 |
+
Module contents
|
| 24 |
+
---------------
|
| 25 |
+
|
| 26 |
+
.. automodule:: src.compilation
|
| 27 |
+
:members:
|
| 28 |
+
:undoc-members:
|
| 29 |
+
:show-inheritance:
|
docs/src.database.rst
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
src.database package
|
| 2 |
+
====================
|
| 3 |
+
|
| 4 |
+
Submodules
|
| 5 |
+
----------
|
| 6 |
+
|
| 7 |
+
src.database.database module
|
| 8 |
+
----------------------------
|
| 9 |
+
|
| 10 |
+
.. automodule:: src.database.database
|
| 11 |
+
:members:
|
| 12 |
+
:undoc-members:
|
| 13 |
+
:show-inheritance:
|
| 14 |
+
|
| 15 |
+
src.database.db\_manipulation module
|
| 16 |
+
------------------------------------
|
| 17 |
+
|
| 18 |
+
.. automodule:: src.database.db_manipulation
|
| 19 |
+
:members:
|
| 20 |
+
:undoc-members:
|
| 21 |
+
:show-inheritance:
|
| 22 |
+
|
| 23 |
+
Module contents
|
| 24 |
+
---------------
|
| 25 |
+
|
| 26 |
+
.. automodule:: src.database
|
| 27 |
+
:members:
|
| 28 |
+
:undoc-members:
|
| 29 |
+
:show-inheritance:
|
docs/src.interface.rst
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
src.interface package
|
| 2 |
+
=====================
|
| 3 |
+
|
| 4 |
+
Submodules
|
| 5 |
+
----------
|
| 6 |
+
|
| 7 |
+
src.interface.download\_files module
|
| 8 |
+
------------------------------------
|
| 9 |
+
|
| 10 |
+
.. automodule:: src.interface.download_files
|
| 11 |
+
:members:
|
| 12 |
+
:undoc-members:
|
| 13 |
+
:show-inheritance:
|
| 14 |
+
|
| 15 |
+
src.interface.gradio\_interface module
|
| 16 |
+
--------------------------------------
|
| 17 |
+
|
| 18 |
+
.. automodule:: src.interface.gradio_interface
|
| 19 |
+
:members:
|
| 20 |
+
:undoc-members:
|
| 21 |
+
:show-inheritance:
|
| 22 |
+
|
| 23 |
+
src.interface.upload\_files module
|
| 24 |
+
----------------------------------
|
| 25 |
+
|
| 26 |
+
.. automodule:: src.interface.upload_files
|
| 27 |
+
:members:
|
| 28 |
+
:undoc-members:
|
| 29 |
+
:show-inheritance:
|
| 30 |
+
|
| 31 |
+
Module contents
|
| 32 |
+
---------------
|
| 33 |
+
|
| 34 |
+
.. automodule:: src.interface
|
| 35 |
+
:members:
|
| 36 |
+
:undoc-members:
|
| 37 |
+
:show-inheritance:
|
docs/src.processing.rst
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
src.processing package
|
| 2 |
+
======================
|
| 3 |
+
|
| 4 |
+
Submodules
|
| 5 |
+
----------
|
| 6 |
+
|
| 7 |
+
src.processing.document\_processing module
|
| 8 |
+
------------------------------------------
|
| 9 |
+
|
| 10 |
+
.. automodule:: src.processing.document_processing
|
| 11 |
+
:members:
|
| 12 |
+
:undoc-members:
|
| 13 |
+
:show-inheritance:
|
| 14 |
+
|
| 15 |
+
src.processing.images\_processing module
|
| 16 |
+
----------------------------------------
|
| 17 |
+
|
| 18 |
+
.. automodule:: src.processing.images_processing
|
| 19 |
+
:members:
|
| 20 |
+
:undoc-members:
|
| 21 |
+
:show-inheritance:
|
| 22 |
+
|
| 23 |
+
src.processing.output\_folder module
|
| 24 |
+
------------------------------------
|
| 25 |
+
|
| 26 |
+
.. automodule:: src.processing.output_folder
|
| 27 |
+
:members:
|
| 28 |
+
:undoc-members:
|
| 29 |
+
:show-inheritance:
|
| 30 |
+
|
| 31 |
+
Module contents
|
| 32 |
+
---------------
|
| 33 |
+
|
| 34 |
+
.. automodule:: src.processing
|
| 35 |
+
:members:
|
| 36 |
+
:undoc-members:
|
| 37 |
+
:show-inheritance:
|
docs/src.rst
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
src package
|
| 2 |
+
===========
|
| 3 |
+
|
| 4 |
+
Subpackages
|
| 5 |
+
-----------
|
| 6 |
+
|
| 7 |
+
.. toctree::
|
| 8 |
+
:maxdepth: 4
|
| 9 |
+
|
| 10 |
+
src.compilation
|
| 11 |
+
src.database
|
| 12 |
+
src.interface
|
| 13 |
+
src.processing
|
| 14 |
+
src.services
|
| 15 |
+
src.telemetry
|
| 16 |
+
|
| 17 |
+
Submodules
|
| 18 |
+
----------
|
| 19 |
+
|
| 20 |
+
src.generator module
|
| 21 |
+
--------------------
|
| 22 |
+
|
| 23 |
+
.. automodule:: src.generator
|
| 24 |
+
:members:
|
| 25 |
+
:undoc-members:
|
| 26 |
+
:show-inheritance:
|
| 27 |
+
|
| 28 |
+
Module contents
|
| 29 |
+
---------------
|
| 30 |
+
|
| 31 |
+
.. automodule:: src
|
| 32 |
+
:members:
|
| 33 |
+
:undoc-members:
|
| 34 |
+
:show-inheritance:
|
docs/src.services.rst
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
src.services package
|
| 2 |
+
====================
|
| 3 |
+
|
| 4 |
+
Submodules
|
| 5 |
+
----------
|
| 6 |
+
|
| 7 |
+
src.services.google\_client module
|
| 8 |
+
----------------------------------
|
| 9 |
+
|
| 10 |
+
.. automodule:: src.services.google_client
|
| 11 |
+
:members:
|
| 12 |
+
:undoc-members:
|
| 13 |
+
:show-inheritance:
|
| 14 |
+
|
| 15 |
+
src.services.output\_schema module
|
| 16 |
+
----------------------------------
|
| 17 |
+
|
| 18 |
+
.. automodule:: src.services.output_schema
|
| 19 |
+
:members:
|
| 20 |
+
:undoc-members:
|
| 21 |
+
:show-inheritance:
|
| 22 |
+
|
| 23 |
+
src.services.prompt module
|
| 24 |
+
--------------------------
|
| 25 |
+
|
| 26 |
+
.. automodule:: src.services.prompt
|
| 27 |
+
:members:
|
| 28 |
+
:undoc-members:
|
| 29 |
+
:show-inheritance:
|
| 30 |
+
|
| 31 |
+
Module contents
|
| 32 |
+
---------------
|
| 33 |
+
|
| 34 |
+
.. automodule:: src.services
|
| 35 |
+
:members:
|
| 36 |
+
:undoc-members:
|
| 37 |
+
:show-inheritance:
|
docs/src.telemetry.rst
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
src.telemetry package
|
| 2 |
+
=====================
|
| 3 |
+
|
| 4 |
+
Submodules
|
| 5 |
+
----------
|
| 6 |
+
|
| 7 |
+
src.telemetry.logging\_utils module
|
| 8 |
+
-----------------------------------
|
| 9 |
+
|
| 10 |
+
.. automodule:: src.telemetry.logging_utils
|
| 11 |
+
:members:
|
| 12 |
+
:undoc-members:
|
| 13 |
+
:show-inheritance:
|
| 14 |
+
|
| 15 |
+
src.telemetry.telemetry module
|
| 16 |
+
------------------------------
|
| 17 |
+
|
| 18 |
+
.. automodule:: src.telemetry.telemetry
|
| 19 |
+
:members:
|
| 20 |
+
:undoc-members:
|
| 21 |
+
:show-inheritance:
|
| 22 |
+
|
| 23 |
+
Module contents
|
| 24 |
+
---------------
|
| 25 |
+
|
| 26 |
+
.. automodule:: src.telemetry
|
| 27 |
+
:members:
|
| 28 |
+
:undoc-members:
|
| 29 |
+
:show-inheritance:
|
gfx/favicon.ico
ADDED
|
|
gfx/github-mark-white.png
ADDED
|
Git LFS Details
|
gfx/icon_logo.png
ADDED
|
|
Git LFS Details
|
gfx/long_logo.png
ADDED
|
Git LFS Details
|
mypy.ini
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# mypy.ini
|
| 2 |
+
|
| 3 |
+
[mypy]
|
| 4 |
+
ignore_missing_imports = True
|
| 5 |
+
|
poetry.lock
CHANGED
|
The diff for this file is too large to render.
See raw diff
|
|
|
pylint-output.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
------------------------------------
|
| 3 |
+
Your code has been rated at 10.00/10
|
| 4 |
+
|
pyproject.toml
CHANGED
|
@@ -3,11 +3,13 @@ name = "ragntex"
|
|
| 3 |
version = "1.0.0"
|
| 4 |
description = "From Text to Visuals: Auto-Generating LaTeX Beamer Presentations with GenAI"
|
| 5 |
authors = [
|
| 6 |
-
{name = "AnnaErsh",email = "aershova096@gmail.com"}
|
|
|
|
| 7 |
]
|
| 8 |
license = {text = "Apache 2.0"}
|
| 9 |
readme = "README.md"
|
| 10 |
-
|
|
|
|
| 11 |
dependencies = [
|
| 12 |
"google-genai (==1.11.0)",
|
| 13 |
"chromadb (==1.0.5)",
|
|
@@ -18,9 +20,26 @@ dependencies = [
|
|
| 18 |
"rtree (>=1.4.0,<2.0.0)",
|
| 19 |
"pymupdf (>=1.25.5,<2.0.0)",
|
| 20 |
"gradio (>=5.27.1,<6.0.0)",
|
|
|
|
|
|
|
|
|
|
| 21 |
]
|
| 22 |
|
| 23 |
|
| 24 |
[build-system]
|
| 25 |
requires = ["poetry-core>=2.0.0,<3.0.0"]
|
| 26 |
build-backend = "poetry.core.masonry.api"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
version = "1.0.0"
|
| 4 |
description = "From Text to Visuals: Auto-Generating LaTeX Beamer Presentations with GenAI"
|
| 5 |
authors = [
|
| 6 |
+
{name = "AnnaErsh",email = "aershova096@gmail.com"},
|
| 7 |
+
{name = "KNiewczas",email = "kajetan.niewczas@gmail.com"}
|
| 8 |
]
|
| 9 |
license = {text = "Apache 2.0"}
|
| 10 |
readme = "README.md"
|
| 11 |
+
packages = [{ include = "src" }]
|
| 12 |
+
requires-python = ">=3.10,<3.11"
|
| 13 |
dependencies = [
|
| 14 |
"google-genai (==1.11.0)",
|
| 15 |
"chromadb (==1.0.5)",
|
|
|
|
| 20 |
"rtree (>=1.4.0,<2.0.0)",
|
| 21 |
"pymupdf (>=1.25.5,<2.0.0)",
|
| 22 |
"gradio (>=5.27.1,<6.0.0)",
|
| 23 |
+
"langfuse (>=2.60.5,<3.0.0)",
|
| 24 |
+
"pydantic (>=2.6.0,<2.10.0)",
|
| 25 |
+
"rsa (>=4.7,<5.0)",
|
| 26 |
]
|
| 27 |
|
| 28 |
|
| 29 |
[build-system]
|
| 30 |
requires = ["poetry-core>=2.0.0,<3.0.0"]
|
| 31 |
build-backend = "poetry.core.masonry.api"
|
| 32 |
+
|
| 33 |
+
[tool.poetry.group.dev.dependencies]
|
| 34 |
+
pylint = "^3.3.7"
|
| 35 |
+
black = "^25.1.0"
|
| 36 |
+
pytest = "^8.4.0"
|
| 37 |
+
mypy = "^1.16.0"
|
| 38 |
+
isort = "^6.0.1"
|
| 39 |
+
safety = "^3.5.2"
|
| 40 |
+
bandit = "^1.8.3"
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
[tool.poetry.group.docs.dependencies]
|
| 44 |
+
sphinx-autodoc-typehints = "1.22"
|
| 45 |
+
furo = "^2024.8.6"
|
ragntex.py
CHANGED
|
@@ -1,135 +1,16 @@
|
|
| 1 |
-
|
| 2 |
-
import re
|
| 3 |
-
from datetime import datetime
|
| 4 |
-
from dotenv import load_dotenv
|
| 5 |
-
|
| 6 |
-
# from src.database import db, embed_fn
|
| 7 |
-
# from src.document_processing import process_documents
|
| 8 |
-
# from src.images_processing import save_pdf_images, save_pdf_figures
|
| 9 |
-
# from src.latex_compilation import CompilePresentation
|
| 10 |
-
# from src.prompt import prompt
|
| 11 |
-
# from src.google_client import client
|
| 12 |
-
|
| 13 |
-
from src import (
|
| 14 |
-
db,
|
| 15 |
-
embed_fn,
|
| 16 |
-
process_documents,
|
| 17 |
-
save_pdf_images,
|
| 18 |
-
save_pdf_figures,
|
| 19 |
-
prompt,
|
| 20 |
-
CompilePresentation,
|
| 21 |
-
client,
|
| 22 |
-
# demo,
|
| 23 |
-
Logger,
|
| 24 |
-
)
|
| 25 |
-
|
| 26 |
-
LOGGER = Logger.get_logger()
|
| 27 |
-
|
| 28 |
-
# demo.launch()
|
| 29 |
-
load_dotenv()
|
| 30 |
-
MAIN_DIR = os.getenv("MAIN_DIR")
|
| 31 |
|
| 32 |
-
|
| 33 |
-
all_entries = db.get(include=["metadatas"])
|
| 34 |
-
existing_pdfs = [meta.get("source_pdf") for meta in all_entries["metadatas"]]
|
| 35 |
-
|
| 36 |
-
# Dataset
|
| 37 |
-
dataset = MAIN_DIR + "dataset/"
|
| 38 |
-
files = os.listdir(dataset)
|
| 39 |
|
| 40 |
-
|
| 41 |
-
# Look for new PDFs to append to our database
|
| 42 |
-
pdf_files = [
|
| 43 |
-
os.path.join(dataset, f)
|
| 44 |
-
for f in files
|
| 45 |
-
if f.lower().endswith(".pdf") and f not in existing_pdfs
|
| 46 |
-
]
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
#
|
| 51 |
-
if
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
metadatas=metadatas,
|
| 56 |
)
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
LOGGER.info("Retrieving relevant documents...")
|
| 60 |
-
embed_fn.document_mode = False
|
| 61 |
-
query = "I need an interesting presentation."
|
| 62 |
-
query_oneline = query.replace(
|
| 63 |
-
"\n", " "
|
| 64 |
-
) # (Optional) For cleaner input in case of newlines
|
| 65 |
-
result = db.query(query_texts=[query], n_results=2)
|
| 66 |
-
[documents] = result["documents"]
|
| 67 |
-
[metadatas] = result["metadatas"]
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
for passage, metas in zip(documents, metadatas):
|
| 71 |
-
passage_oneline = passage.replace("\n", " ")
|
| 72 |
-
images_passage = metas["images_passage"]
|
| 73 |
-
|
| 74 |
-
prompt += f"PASSAGE: {passage_oneline}\n"
|
| 75 |
-
prompt += f"IMAGES: {images_passage}\n"
|
| 76 |
-
|
| 77 |
-
# Create a new subfolder
|
| 78 |
-
LOGGER.info("💾 Creating new subfolder...")
|
| 79 |
-
base_path = MAIN_DIR + "output/"
|
| 80 |
-
timestamp = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
|
| 81 |
-
work_dir = os.path.join(base_path, timestamp)
|
| 82 |
-
os.makedirs(work_dir, exist_ok=False)
|
| 83 |
-
LOGGER.info("📁 Created folder: %s", work_dir)
|
| 84 |
-
|
| 85 |
-
# Generate the presentation code
|
| 86 |
-
model_name = "gemini-2.0-flash"
|
| 87 |
-
# model_name = "gemini-2.5-flash-preview-04-17"
|
| 88 |
-
LOGGER.info("⭐️ Generating response...")
|
| 89 |
-
answer = client.models.generate_content(model=model_name, contents=prompt)
|
| 90 |
-
LOGGER.info("🎲 Generated the response using: %s", model_name)
|
| 91 |
-
|
| 92 |
-
# Create a subfolder for graphics
|
| 93 |
-
graphics_dir = os.path.join(work_dir, "gfx")
|
| 94 |
-
os.makedirs(graphics_dir, exist_ok=True)
|
| 95 |
-
LOGGER.info("🌄 Images will be saved to: %s", graphics_dir)
|
| 96 |
-
|
| 97 |
-
# Find images, which are used in the presentation
|
| 98 |
-
pattern_img = re.compile(
|
| 99 |
-
r"doc(?P<doc>[a-zA-Z0-9_]+)_page(?P<page>\d+)_img(?P<img>\d+)_hash(?P<hash>[a-fA-F0-9]{8})\.png"
|
| 100 |
-
)
|
| 101 |
-
matches_img = pattern_img.finditer(answer.text)
|
| 102 |
-
|
| 103 |
-
req_imgs = []
|
| 104 |
-
for match in matches_img:
|
| 105 |
-
req_img = {
|
| 106 |
-
"doc": match.group("doc"),
|
| 107 |
-
"page": int(match.group("page")),
|
| 108 |
-
"img": int(match.group("img")),
|
| 109 |
-
"hash": match.group("hash"),
|
| 110 |
-
}
|
| 111 |
-
req_imgs.append(req_img)
|
| 112 |
-
|
| 113 |
-
# Find figures, which are used in the presentation
|
| 114 |
-
pattern_fig = re.compile(
|
| 115 |
-
r"doc(?P<doc>[a-zA-Z0-9_]+)_page(?P<page>\d+)_fig(?P<fig>\d+)_hash(?P<hash>[a-fA-F0-9]{8})\.png"
|
| 116 |
-
)
|
| 117 |
-
matches_fig = pattern_fig.finditer(answer.text)
|
| 118 |
-
|
| 119 |
-
req_figs = []
|
| 120 |
-
for match in matches_fig:
|
| 121 |
-
req_fig = {
|
| 122 |
-
"doc": match.group("doc"),
|
| 123 |
-
"page": int(match.group("page")),
|
| 124 |
-
"fig": int(match.group("fig")),
|
| 125 |
-
"hash": match.group("hash"),
|
| 126 |
-
}
|
| 127 |
-
req_figs.append(req_fig)
|
| 128 |
-
|
| 129 |
-
# Save the required graphics
|
| 130 |
-
for metadata in metadatas:
|
| 131 |
-
save_pdf_images(metadata["pdf_path"], req_imgs, graphics_dir)
|
| 132 |
-
save_pdf_figures(metadata["pdf_path"], req_figs, graphics_dir)
|
| 133 |
-
|
| 134 |
-
LOGGER.info("Compiling presentation...")
|
| 135 |
-
CompilePresentation(answer.text, work_dir)
|
|
|
|
| 1 |
+
"""RAGnTex: A Retrieval-Augmented Generation for LaTeX Documents"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
+
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
+
from src import demo, init_telemetry
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
# Setup OpenTelemetry
|
| 8 |
+
_ = init_telemetry()
|
| 9 |
+
# Run Gradio
|
| 10 |
+
if os.getenv("IN_DOCKER") == "true":
|
| 11 |
+
demo.launch(
|
| 12 |
+
server_name="0.0.0.0", # nosec: intentional bind all interfaces for HuggingFace Spaces
|
| 13 |
+
server_port=7860,
|
|
|
|
| 14 |
)
|
| 15 |
+
else:
|
| 16 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
src/__init__.py
CHANGED
|
@@ -1,20 +1,11 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
from .
|
| 4 |
-
from .
|
| 5 |
-
from .
|
| 6 |
-
# from .interface import demo
|
| 7 |
|
| 8 |
__all__ = [
|
| 9 |
-
"
|
| 10 |
-
"
|
| 11 |
-
"
|
| 12 |
-
"save_pdf_images",
|
| 13 |
-
"save_pdf_figures",
|
| 14 |
-
"prompt",
|
| 15 |
-
"CompilePresentation",
|
| 16 |
-
"client",
|
| 17 |
-
"upload_files",
|
| 18 |
-
"Logger",
|
| 19 |
-
# "demo",
|
| 20 |
]
|
|
|
|
| 1 |
+
"""Initialize the package by exposing the main interface and telemetry setup."""
|
| 2 |
+
|
| 3 |
+
from .generator import generate_presentation
|
| 4 |
+
from .interface import demo
|
| 5 |
+
from .telemetry import init_telemetry
|
|
|
|
| 6 |
|
| 7 |
__all__ = [
|
| 8 |
+
"demo",
|
| 9 |
+
"init_telemetry",
|
| 10 |
+
"generate_presentation",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
]
|
src/compilation/__init__.py
CHANGED
|
@@ -1,3 +1,7 @@
|
|
| 1 |
-
|
| 2 |
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""This module initializes the latex compilation package."""
|
| 2 |
|
| 3 |
+
from .json_to_tex import json_to_tex
|
| 4 |
+
from .latex_compilation import compile_presentation
|
| 5 |
+
from .latex_tools import replace_unicode_greek
|
| 6 |
+
|
| 7 |
+
__all__ = ["compile_presentation", "json_to_tex", "replace_unicode_greek"]
|
src/compilation/json_to_tex.py
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""This module provides functionality to convert a JSON LLM output to LaTeX Beamer presentation."""
|
| 2 |
+
|
| 3 |
+
import json
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
def render_content(content: list[str], content_format: str) -> str:
|
| 7 |
+
"""Render content in LaTeX format depending is it si itemized list or plain text.
|
| 8 |
+
Args:
|
| 9 |
+
content (list[str]): List of content items.
|
| 10 |
+
content_format (str): Format of the content, either "itemize" or "text".
|
| 11 |
+
Returns:
|
| 12 |
+
str: LaTeX formatted content.
|
| 13 |
+
"""
|
| 14 |
+
# Remove blank or whitespace-only entries
|
| 15 |
+
filtered_content = [c.strip() for c in content if c.strip()]
|
| 16 |
+
if content_format == "text":
|
| 17 |
+
return "\n\n".join(filtered_content)
|
| 18 |
+
return (
|
| 19 |
+
"\\begin{itemize}\n"
|
| 20 |
+
+ "\n".join(f"\\item {c}" for c in filtered_content)
|
| 21 |
+
+ "\n\\end{itemize}"
|
| 22 |
+
)
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def json_to_tex(
|
| 26 |
+
presentation_json: str, theme: str, color_theme: str, aspect_ratio: str
|
| 27 |
+
) -> str:
|
| 28 |
+
"""Convert a JSON representation of a presentation to LaTeX Beamer format.
|
| 29 |
+
Args:
|
| 30 |
+
presentation (Dict[str, Any]): JSON LLM output of the presentation.
|
| 31 |
+
theme (str): Beamer theme to use.
|
| 32 |
+
color_theme (str): Beamer color theme to use.
|
| 33 |
+
aspect_ratio (str): Aspect ratio for the presentation, e.g., "16:9", "4:3".
|
| 34 |
+
Returns:
|
| 35 |
+
str: LaTeX Beamer code as a string.
|
| 36 |
+
"""
|
| 37 |
+
presentation = json.loads(presentation_json)
|
| 38 |
+
aspect_ratio_num = 169 if aspect_ratio == "16:9" else 43
|
| 39 |
+
header = f"""\\documentclass[aspectratio={aspect_ratio_num}]{{beamer}}
|
| 40 |
+
\\usepackage[utf8]{{inputenc}}
|
| 41 |
+
\\usetheme{{{theme}}}
|
| 42 |
+
\\usecolortheme{{{color_theme}}}
|
| 43 |
+
\\title{{{presentation["title"]}}}
|
| 44 |
+
\\author{{{presentation["author"]}}}
|
| 45 |
+
\\date{{\\today}}
|
| 46 |
+
|
| 47 |
+
\\usepackage{{graphicx}}
|
| 48 |
+
|
| 49 |
+
\\begin{{document}}
|
| 50 |
+
|
| 51 |
+
\\frame{{\\titlepage}}"""
|
| 52 |
+
|
| 53 |
+
slides = []
|
| 54 |
+
|
| 55 |
+
for slide in presentation["slides"]:
|
| 56 |
+
slide_type = slide["type"]
|
| 57 |
+
content = slide.get("content", [])
|
| 58 |
+
content_format = slide.get("content_format", "itemize")
|
| 59 |
+
body = render_content(content, content_format)
|
| 60 |
+
|
| 61 |
+
# if slide_type == "title":
|
| 62 |
+
# continue
|
| 63 |
+
|
| 64 |
+
if slide_type in {"introduction", "summary"}:
|
| 65 |
+
title = "Introduction" if slide_type == "introduction" else "Summary"
|
| 66 |
+
slides.append(
|
| 67 |
+
f"""\\begin{{frame}}
|
| 68 |
+
\\frametitle{{{title}}}
|
| 69 |
+
{body}
|
| 70 |
+
\\end{{frame}}"""
|
| 71 |
+
)
|
| 72 |
+
|
| 73 |
+
elif slide_type == "core_idea":
|
| 74 |
+
core_title = slide.get("title", "Core Idea")
|
| 75 |
+
layout = slide.get("layout", "text_only")
|
| 76 |
+
|
| 77 |
+
if layout == "text_only":
|
| 78 |
+
slides.append(
|
| 79 |
+
f"""\\begin{{frame}}
|
| 80 |
+
\\frametitle{{{core_title}}}
|
| 81 |
+
{body}
|
| 82 |
+
\\end{{frame}}"""
|
| 83 |
+
)
|
| 84 |
+
|
| 85 |
+
elif layout == "two_column":
|
| 86 |
+
img = slide["image"]
|
| 87 |
+
slides.append(
|
| 88 |
+
f"""\\begin{{frame}}
|
| 89 |
+
\\frametitle{{{core_title}}}
|
| 90 |
+
\\begin{{columns}}
|
| 91 |
+
\\begin{{column}}{{0.5\\linewidth}}
|
| 92 |
+
{body}
|
| 93 |
+
\\end{{column}}
|
| 94 |
+
\\begin{{column}}{{0.5\\linewidth}}
|
| 95 |
+
\\center{{\\includegraphics[height=1.0\\textheight, width=1.0\\textwidth, keepaspectratio]{{{img["path"]}}} \\\\ {img["caption"]}}}
|
| 96 |
+
\\end{{column}}
|
| 97 |
+
\\end{{columns}}
|
| 98 |
+
\\end{{frame}}"""
|
| 99 |
+
)
|
| 100 |
+
|
| 101 |
+
elif layout == "single_image":
|
| 102 |
+
img = slide["image"]
|
| 103 |
+
slides.append(
|
| 104 |
+
f"""\\begin{{frame}}
|
| 105 |
+
\\frametitle{{{core_title}}}
|
| 106 |
+
\\center{{\\includegraphics[height=0.5\\textheight, width=0.8\\textwidth, keepaspectratio]{{{img["path"]}}} \\\\ {img["caption"]} \\\\}}
|
| 107 |
+
{body}
|
| 108 |
+
\\end{{frame}}"""
|
| 109 |
+
)
|
| 110 |
+
|
| 111 |
+
footer = "\\end{document}"
|
| 112 |
+
|
| 113 |
+
return "\n\n".join([header] + slides + [footer])
|
src/compilation/latex_compilation.py
CHANGED
|
@@ -1,11 +1,24 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
LOGGER = Logger.get_logger()
|
| 6 |
|
| 7 |
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
# Remove possible Markdown wrapping around the output
|
| 10 |
if latex_code.startswith("```latex"):
|
| 11 |
latex_code = latex_code.split("\n", 1)[1] # Remove the first line
|
|
@@ -14,40 +27,66 @@ def CompilePresentation(latex_code, work_dir):
|
|
| 14 |
|
| 15 |
# Save LaTeX code to file
|
| 16 |
tex_file = os.path.join(work_dir, "presentation.tex")
|
| 17 |
-
with open(tex_file, "w") as f:
|
| 18 |
f.write(latex_code)
|
| 19 |
|
| 20 |
-
LOGGER.info("
|
| 21 |
-
LOGGER.info("📄 Files in the directory:")
|
| 22 |
|
| 23 |
# List files in the directory using Python (instead of `!ls`)
|
| 24 |
for file in os.listdir(work_dir):
|
| 25 |
LOGGER.info(file)
|
| 26 |
|
| 27 |
# Compile with pdflatex (using subprocess instead of `!`)
|
| 28 |
-
os.chdir(work_dir) # Change working directory
|
| 29 |
-
|
| 30 |
try:
|
| 31 |
# Run pdflatex twice to ensure proper slide enumeration
|
| 32 |
subprocess.run(
|
| 33 |
["pdflatex", "-interaction=nonstopmode", "presentation.tex"],
|
|
|
|
| 34 |
check=True,
|
| 35 |
stdout=subprocess.PIPE,
|
| 36 |
stderr=subprocess.PIPE,
|
| 37 |
-
)
|
| 38 |
subprocess.run(
|
| 39 |
["pdflatex", "-interaction=nonstopmode", "presentation.tex"],
|
|
|
|
| 40 |
check=True,
|
| 41 |
stdout=subprocess.PIPE,
|
| 42 |
stderr=subprocess.PIPE,
|
| 43 |
-
)
|
| 44 |
|
| 45 |
except subprocess.CalledProcessError as e:
|
| 46 |
# Handle error in case of failed LaTeX compilation
|
| 47 |
-
|
| 48 |
-
|
|
|
|
| 49 |
# Check for PDF output
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Module for compiling LaTeX presentations into PDF format."""
|
| 2 |
+
|
| 3 |
+
import os # nosec: checked usage, inputs are sanitized
|
| 4 |
+
import subprocess # nosec: checked usage, inputs are sanitized
|
| 5 |
+
from pathlib import Path
|
| 6 |
+
|
| 7 |
+
from langfuse.decorators import langfuse_context, observe
|
| 8 |
+
|
| 9 |
+
from ..telemetry import Logger
|
| 10 |
|
| 11 |
LOGGER = Logger.get_logger()
|
| 12 |
|
| 13 |
|
| 14 |
+
@observe(name="🧱 compile_presentation")
|
| 15 |
+
def compile_presentation(latex_code, work_dir) -> str:
|
| 16 |
+
"""Compile LaTeX code into a PDF presentation.
|
| 17 |
+
Args:
|
| 18 |
+
latex_code (str): LaTeX code to be compiled.
|
| 19 |
+
work_dir (str): Directory where the LaTeX file will be saved and compiled.
|
| 20 |
+
"""
|
| 21 |
+
LOGGER.info("Compiling presentation...")
|
| 22 |
# Remove possible Markdown wrapping around the output
|
| 23 |
if latex_code.startswith("```latex"):
|
| 24 |
latex_code = latex_code.split("\n", 1)[1] # Remove the first line
|
|
|
|
| 27 |
|
| 28 |
# Save LaTeX code to file
|
| 29 |
tex_file = os.path.join(work_dir, "presentation.tex")
|
| 30 |
+
with open(tex_file, "w", encoding="utf-8") as f:
|
| 31 |
f.write(latex_code)
|
| 32 |
|
| 33 |
+
LOGGER.info("📄 Files in the directory: %s", work_dir)
|
|
|
|
| 34 |
|
| 35 |
# List files in the directory using Python (instead of `!ls`)
|
| 36 |
for file in os.listdir(work_dir):
|
| 37 |
LOGGER.info(file)
|
| 38 |
|
| 39 |
# Compile with pdflatex (using subprocess instead of `!`)
|
|
|
|
|
|
|
| 40 |
try:
|
| 41 |
# Run pdflatex twice to ensure proper slide enumeration
|
| 42 |
subprocess.run(
|
| 43 |
["pdflatex", "-interaction=nonstopmode", "presentation.tex"],
|
| 44 |
+
cwd=work_dir,
|
| 45 |
check=True,
|
| 46 |
stdout=subprocess.PIPE,
|
| 47 |
stderr=subprocess.PIPE,
|
| 48 |
+
) # nosec B603 B607 - fixed command, no untrusted input
|
| 49 |
subprocess.run(
|
| 50 |
["pdflatex", "-interaction=nonstopmode", "presentation.tex"],
|
| 51 |
+
cwd=work_dir,
|
| 52 |
check=True,
|
| 53 |
stdout=subprocess.PIPE,
|
| 54 |
stderr=subprocess.PIPE,
|
| 55 |
+
) # nosec B603 B607 - fixed command, no untrusted input
|
| 56 |
|
| 57 |
except subprocess.CalledProcessError as e:
|
| 58 |
# Handle error in case of failed LaTeX compilation
|
| 59 |
+
langfuse_context.update_current_observation(
|
| 60 |
+
output={"pdf.success": False, "pdf.error_log": e.stderr.decode()}
|
| 61 |
+
)
|
| 62 |
# Check for PDF output
|
| 63 |
+
pdf_file = Path(work_dir) / "presentation.pdf"
|
| 64 |
+
if not pdf_file.exists():
|
| 65 |
+
LOGGER.error(
|
| 66 |
+
"❌ PDF generation failed and no PDF file found. Here's the log: %s",
|
| 67 |
+
e.stderr.decode(),
|
| 68 |
+
)
|
| 69 |
+
return "❌ Presentation compilation failed. Please try again later."
|
| 70 |
+
LOGGER.error(
|
| 71 |
+
"⚠️ PDF generation failed, but PDF file exists. Here's the log: %s",
|
| 72 |
+
e.stderr.decode(),
|
| 73 |
+
)
|
| 74 |
+
return (
|
| 75 |
+
"⚠️ Presentation compilation contains errors. Please cross-check the output."
|
| 76 |
+
)
|
| 77 |
+
|
| 78 |
+
# Success, but still check for PDF output
|
| 79 |
+
pdf_file = Path(work_dir) / "presentation.pdf"
|
| 80 |
+
if not pdf_file.exists():
|
| 81 |
+
langfuse_context.update_current_observation(
|
| 82 |
+
output={
|
| 83 |
+
"pdf.success": False,
|
| 84 |
+
"pdf.error_log": "❌ PDF generation succeded, but no PDF file found.",
|
| 85 |
+
}
|
| 86 |
+
)
|
| 87 |
+
LOGGER.error("❌ PDF generation failed. No PDF file found.")
|
| 88 |
+
return "❌ Presentation compilation failed. Please try again later."
|
| 89 |
+
|
| 90 |
+
langfuse_context.update_current_observation(output={"pdf.success": True})
|
| 91 |
+
LOGGER.info("✅ PDF generated successfully in: %s", work_dir)
|
| 92 |
+
return "⭐️ Presentation generated successfully!"
|
src/compilation/latex_tools.py
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""This module provides tools to work with LaTeX Beamer presentations."""
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
def replace_unicode_greek(text):
|
| 5 |
+
"""Replace Greek Unicode characters with LaTeX equivalents.
|
| 6 |
+
Args:
|
| 7 |
+
text (str): Input text containing Greek characters.
|
| 8 |
+
Returns:
|
| 9 |
+
str: Text with Greek characters replaced by LaTeX commands.
|
| 10 |
+
"""
|
| 11 |
+
greek_map = {
|
| 12 |
+
'α': r'$\alpha$', 'β': r'$\beta$', 'γ': r'$\gamma$',
|
| 13 |
+
'δ': r'$\delta$', 'ε': r'$\epsilon$', 'ζ': r'$\zeta$',
|
| 14 |
+
'η': r'$\eta$', 'θ': r'$\theta$', 'ι': r'$\iota$',
|
| 15 |
+
'κ': r'$\kappa$', 'λ': r'$\lambda$', 'μ': r'$\mu$',
|
| 16 |
+
'ν': r'$\nu$', 'ξ': r'$\xi$', 'ο': 'o', 'π': r'$\pi$',
|
| 17 |
+
'ρ': r'$\rho$', 'σ': r'$\sigma$', 'ς': r'$\varsigma$',
|
| 18 |
+
'τ': r'$\tau$', 'υ': r'$\upsilon$', 'φ': r'$\phi$',
|
| 19 |
+
'χ': r'$\chi$', 'ψ': r'$\psi$', 'ω': r'$\omega$',
|
| 20 |
+
'Α': 'A', 'Β': 'B', 'Γ': r'$\Gamma$', 'Δ': r'$\Delta$',
|
| 21 |
+
'Ε': 'E', 'Ζ': 'Z', 'Η': 'H', 'Θ': r'$\Theta$',
|
| 22 |
+
'Ι': 'I', 'Κ': 'K', 'Λ': r'$\Lambda$', 'Μ': 'M',
|
| 23 |
+
'Ν': 'N', 'Ξ': r'$\Xi$', 'Ο': 'O', 'Π': r'$\Pi$',
|
| 24 |
+
'Ρ': 'P', 'Σ': r'$\Sigma$', 'Τ': 'T', 'Υ': r'$\Upsilon$',
|
| 25 |
+
'Φ': r'$\Phi$', 'Χ': 'X', 'Ψ': r'$\Psi$', 'Ω': r'$\Omega$',
|
| 26 |
+
'ϕ': r'$\varphi$', 'ϑ': r'$\vartheta$', 'ϰ': r'$\varkappa$', 'ϱ': r'$\varrho$',
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
for char, replacement in greek_map.items():
|
| 30 |
+
text = text.replace(char, replacement)
|
| 31 |
+
|
| 32 |
+
return text
|
src/database/__init__.py
CHANGED
|
@@ -1,3 +1,7 @@
|
|
| 1 |
-
|
| 2 |
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""This module initializes the database package."""
|
| 2 |
|
| 3 |
+
from .database import chroma_client, embed_fn
|
| 4 |
+
from .db_manipulation import (clean_db, ingest_files_to_db,
|
| 5 |
+
retrive_files_from_db)
|
| 6 |
+
|
| 7 |
+
__all__ = ["chroma_client", "embed_fn", "ingest_files_to_db", "retrive_files_from_db", "clean_db"]
|
src/database/database.py
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from google.api_core import retry
|
| 2 |
from google.genai import types
|
| 3 |
-
from google import genai
|
| 4 |
-
from chromadb import Documents, EmbeddingFunction, Embeddings
|
| 5 |
-
import chromadb
|
| 6 |
|
| 7 |
from ..services import client
|
| 8 |
|
|
@@ -11,11 +13,13 @@ is_retriable = lambda e: (isinstance(e, genai.errors.APIError) and e.code in {42
|
|
| 11 |
|
| 12 |
|
| 13 |
class GeminiEmbeddingFunction(EmbeddingFunction):
|
|
|
|
|
|
|
| 14 |
def __init__(self):
|
| 15 |
self.document_mode = True
|
| 16 |
|
| 17 |
@retry.Retry(predicate=is_retriable)
|
| 18 |
-
def __call__(self,
|
| 19 |
if self.document_mode:
|
| 20 |
embedding_task = "retrieval_document"
|
| 21 |
else:
|
|
@@ -23,7 +27,7 @@ class GeminiEmbeddingFunction(EmbeddingFunction):
|
|
| 23 |
|
| 24 |
response = client.models.embed_content(
|
| 25 |
model="models/text-embedding-004",
|
| 26 |
-
contents=
|
| 27 |
config=types.EmbedContentConfig(
|
| 28 |
task_type=embedding_task,
|
| 29 |
),
|
|
@@ -31,11 +35,7 @@ class GeminiEmbeddingFunction(EmbeddingFunction):
|
|
| 31 |
return [e.values for e in response.embeddings]
|
| 32 |
|
| 33 |
|
| 34 |
-
DB_NAME = "ragntex"
|
| 35 |
-
|
| 36 |
embed_fn = GeminiEmbeddingFunction()
|
| 37 |
embed_fn.document_mode = True
|
| 38 |
|
| 39 |
chroma_client = chromadb.Client()
|
| 40 |
-
# chroma_client.delete_collection(DB_NAME)
|
| 41 |
-
db = chroma_client.get_or_create_collection(name=DB_NAME, embedding_function=embed_fn)
|
|
|
|
| 1 |
+
"""Database module for user-uploaded files."""
|
| 2 |
+
|
| 3 |
+
import chromadb
|
| 4 |
+
from chromadb import Documents, EmbeddingFunction, Embeddings
|
| 5 |
+
from google import genai
|
| 6 |
from google.api_core import retry
|
| 7 |
from google.genai import types
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
from ..services import client
|
| 10 |
|
|
|
|
| 13 |
|
| 14 |
|
| 15 |
class GeminiEmbeddingFunction(EmbeddingFunction):
|
| 16 |
+
"""A class to handle retrieval embedding functions using Google Gemini API."""
|
| 17 |
+
|
| 18 |
def __init__(self):
|
| 19 |
self.document_mode = True
|
| 20 |
|
| 21 |
@retry.Retry(predicate=is_retriable)
|
| 22 |
+
def __call__(self, inputs: Documents) -> Embeddings:
|
| 23 |
if self.document_mode:
|
| 24 |
embedding_task = "retrieval_document"
|
| 25 |
else:
|
|
|
|
| 27 |
|
| 28 |
response = client.models.embed_content(
|
| 29 |
model="models/text-embedding-004",
|
| 30 |
+
contents=inputs,
|
| 31 |
config=types.EmbedContentConfig(
|
| 32 |
task_type=embedding_task,
|
| 33 |
),
|
|
|
|
| 35 |
return [e.values for e in response.embeddings]
|
| 36 |
|
| 37 |
|
|
|
|
|
|
|
| 38 |
embed_fn = GeminiEmbeddingFunction()
|
| 39 |
embed_fn.document_mode = True
|
| 40 |
|
| 41 |
chroma_client = chromadb.Client()
|
|
|
|
|
|
src/database/db_manipulation.py
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Module for adding and getting PDF files to/from the database."""
|
| 2 |
+
|
| 3 |
+
from typing import List, Mapping, Union
|
| 4 |
+
|
| 5 |
+
from langfuse.decorators import langfuse_context, observe
|
| 6 |
+
|
| 7 |
+
from ..processing import process_documents
|
| 8 |
+
from ..telemetry import Logger
|
| 9 |
+
from .database import chroma_client, embed_fn
|
| 10 |
+
|
| 11 |
+
LOGGER = Logger.get_logger()
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
@observe(name="𝌊 ingest_files_to_db")
|
| 15 |
+
def ingest_files_to_db(pdf_files, session_id) -> list[str]:
|
| 16 |
+
"""Add new files to the database if they are not yet there.
|
| 17 |
+
Args:
|
| 18 |
+
pdf_files (list): List of PDF files to be ingested.
|
| 19 |
+
session_id (str): Unique identifier for the current session.
|
| 20 |
+
"""
|
| 21 |
+
# Get or create the database collection
|
| 22 |
+
db = chroma_client.get_or_create_collection(
|
| 23 |
+
name=session_id, embedding_function=embed_fn
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
# Get all the existing entries and extract the known filenames
|
| 27 |
+
all_entries = db.get(include=["metadatas"]) or []
|
| 28 |
+
|
| 29 |
+
if not all_entries:
|
| 30 |
+
LOGGER.info("📂 No existing entries in the database. Ingesting all PDFs.")
|
| 31 |
+
|
| 32 |
+
existing_pdfs = set(
|
| 33 |
+
meta.get("pdf_path")
|
| 34 |
+
for meta in all_entries["metadatas"] # type: ignore[union-attr]
|
| 35 |
+
if meta.get("pdf_path")
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
# Filter out already ingested PDFs
|
| 39 |
+
new_pdfs = [file for file in pdf_files if file not in existing_pdfs]
|
| 40 |
+
|
| 41 |
+
if not new_pdfs:
|
| 42 |
+
LOGGER.info("📂 No new PDFs to ingest. Skipping ingestion.")
|
| 43 |
+
return []
|
| 44 |
+
|
| 45 |
+
# Process new files only
|
| 46 |
+
documents, metadatas, failed = process_documents(new_pdfs)
|
| 47 |
+
|
| 48 |
+
LOGGER.info("➕ Adding %d new PDFs to the database...", len(new_pdfs))
|
| 49 |
+
if documents:
|
| 50 |
+
db.add(
|
| 51 |
+
documents=documents,
|
| 52 |
+
ids=[str(i) for i in range(len(documents))],
|
| 53 |
+
metadatas=metadatas,
|
| 54 |
+
)
|
| 55 |
+
LOGGER.info("✅ Documents ingested successfully.")
|
| 56 |
+
|
| 57 |
+
return failed
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
Metadata = Mapping[str, Union[str, int, float, bool]]
|
| 61 |
+
DocumentsBatch = List[List[str]]
|
| 62 |
+
MetadataBatch = List[List[Metadata]]
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
@observe(name="🔍 retrieve_files_from_db")
|
| 66 |
+
def retrive_files_from_db(
|
| 67 |
+
topic: str, session_id: str
|
| 68 |
+
) -> tuple[DocumentsBatch, MetadataBatch]:
|
| 69 |
+
"""Retrieve relevant documents from the database based on the provided topic.
|
| 70 |
+
Args:
|
| 71 |
+
topic (str): The topic for which to retrieve documents.
|
| 72 |
+
session_id (str): Unique identifier for the current session.
|
| 73 |
+
Returns:
|
| 74 |
+
tuple: A 2-element tuple:
|
| 75 |
+
- list[str]: List of retrieved documents.
|
| 76 |
+
- list[dict]: List of metadata associated with the documents.
|
| 77 |
+
"""
|
| 78 |
+
# Get or create the database collection
|
| 79 |
+
db = chroma_client.get_or_create_collection(
|
| 80 |
+
name=session_id, embedding_function=embed_fn
|
| 81 |
+
)
|
| 82 |
+
if not db:
|
| 83 |
+
LOGGER.error("❌ Database collection not found or could not be created.")
|
| 84 |
+
return [], []
|
| 85 |
+
|
| 86 |
+
LOGGER.info("🔍 Retrieving relevant documents...")
|
| 87 |
+
embed_fn.document_mode = False
|
| 88 |
+
query_oneline = topic.replace("\n", " ")
|
| 89 |
+
|
| 90 |
+
n_yelded_docs = 2
|
| 91 |
+
result = db.query(query_texts=[query_oneline], n_results=n_yelded_docs)
|
| 92 |
+
langfuse_context.update_current_observation(
|
| 93 |
+
output={
|
| 94 |
+
"input.query_oneline": query_oneline,
|
| 95 |
+
"output.n_results": n_yelded_docs,
|
| 96 |
+
"output.metadatas": result["metadatas"],
|
| 97 |
+
}
|
| 98 |
+
)
|
| 99 |
+
[documents] = result.get("documents") or [[]]
|
| 100 |
+
[metadatas] = result.get("metadatas") or [[]]
|
| 101 |
+
|
| 102 |
+
return [documents], [metadatas]
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
def clean_db(session_id: str) -> None:
|
| 106 |
+
"""Clean the database collection for the given session ID.
|
| 107 |
+
Args:
|
| 108 |
+
session_id (str): Unique identifier for the current session.
|
| 109 |
+
"""
|
| 110 |
+
db = chroma_client.get_or_create_collection(
|
| 111 |
+
name=session_id, embedding_function=embed_fn
|
| 112 |
+
)
|
| 113 |
+
if db:
|
| 114 |
+
chroma_client.delete_collection(name=session_id)
|
| 115 |
+
LOGGER.info("🗑️ Cleaning database for session ID: %s", session_id)
|
| 116 |
+
else:
|
| 117 |
+
LOGGER.warning(
|
| 118 |
+
"❌ Database collection not found for session ID: %s", session_id
|
| 119 |
+
)
|
src/generator.py
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Module to put it all together and manage the generation of a presentation."""
|
| 2 |
+
|
| 3 |
+
from dataclasses import dataclass
|
| 4 |
+
from typing import Optional
|
| 5 |
+
|
| 6 |
+
from langfuse.decorators import langfuse_context, observe
|
| 7 |
+
|
| 8 |
+
from src.compilation import (compile_presentation, json_to_tex,
|
| 9 |
+
replace_unicode_greek)
|
| 10 |
+
# from datetime import datetime
|
| 11 |
+
from src.database import retrive_files_from_db
|
| 12 |
+
from src.processing import create_output_folder, find_used_gfx
|
| 13 |
+
from src.services import build_prompt, client, generate_with_retry
|
| 14 |
+
from src.telemetry import Logger
|
| 15 |
+
|
| 16 |
+
LOGGER = Logger.get_logger()
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
@dataclass
|
| 20 |
+
class PresentationConfig:
|
| 21 |
+
"""Configuration for the presentation generation."""
|
| 22 |
+
|
| 23 |
+
model_name: str = "gemini-2.0-flash"
|
| 24 |
+
aspect_ratio: str = "16:9"
|
| 25 |
+
theme: str = "default"
|
| 26 |
+
color_theme: str = "default"
|
| 27 |
+
topic: str = ""
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
@observe(name="🧑🎨 generate_presentation")
|
| 31 |
+
def generate_presentation(
|
| 32 |
+
config_dict, session_id
|
| 33 |
+
) -> tuple[str, Optional[str], Optional[str]]:
|
| 34 |
+
"""Main function to generate a presentation based on the provided theme, color, and topic.
|
| 35 |
+
|
| 36 |
+
Args:
|
| 37 |
+
config_dict (dict): Configuration dictionary containing:
|
| 38 |
+
- model_name (str): Name of the model to use for generation.
|
| 39 |
+
- aspect_ratio (str): Aspect ratio for the presentation, e.g., "16:9", "4:3".
|
| 40 |
+
- theme (str): Beamer theme to use.
|
| 41 |
+
- color_theme (str): Beamer color theme to use.
|
| 42 |
+
- topic (str): Topic of the presentation.
|
| 43 |
+
session_id (str): Unique identifier for the current session.
|
| 44 |
+
|
| 45 |
+
Returns:
|
| 46 |
+
tuple:
|
| 47 |
+
A 3-element tuple containing:
|
| 48 |
+
- str: Status message (success or error).
|
| 49 |
+
- Optional[str]: Trace ID for telemetry purposes, or None if unavailable.
|
| 50 |
+
- Optional[str]: Path to the presentation folder, or None if unavailable.
|
| 51 |
+
"""
|
| 52 |
+
# Cast dict to dataclass
|
| 53 |
+
try:
|
| 54 |
+
config = PresentationConfig(**config_dict)
|
| 55 |
+
except TypeError as e:
|
| 56 |
+
# Handle missing or extra keys gracefully
|
| 57 |
+
return f"Config error: {str(e)}", None, None
|
| 58 |
+
|
| 59 |
+
trace_id = langfuse_context.get_current_trace_id()
|
| 60 |
+
|
| 61 |
+
[documents], [metadatas] = retrive_files_from_db(config.topic, session_id)
|
| 62 |
+
|
| 63 |
+
prompt = build_prompt(documents, metadatas, config.aspect_ratio)
|
| 64 |
+
|
| 65 |
+
work_dir = create_output_folder(session_id)
|
| 66 |
+
# Generate the presentation code
|
| 67 |
+
model_name = config.model_name
|
| 68 |
+
LOGGER.info("⭐️ Generating response...")
|
| 69 |
+
try:
|
| 70 |
+
answer = generate_with_retry(client, model_name, prompt)
|
| 71 |
+
except Exception as e:
|
| 72 |
+
LOGGER.error("❌ Gemini is not reponding: %s", str(e))
|
| 73 |
+
return f"Gemini error: {str(e)}", trace_id, work_dir
|
| 74 |
+
|
| 75 |
+
langfuse_context.update_current_observation(
|
| 76 |
+
output={
|
| 77 |
+
"model_name": model_name,
|
| 78 |
+
"session_id": session_id,
|
| 79 |
+
"aspect_ratio": config.aspect_ratio,
|
| 80 |
+
"prompt_length": len(prompt),
|
| 81 |
+
"output.response": answer.text,
|
| 82 |
+
}
|
| 83 |
+
)
|
| 84 |
+
LOGGER.info("🎲 Generated the response using: %s", model_name)
|
| 85 |
+
|
| 86 |
+
find_used_gfx(answer, work_dir, metadatas)
|
| 87 |
+
latex_code = json_to_tex(
|
| 88 |
+
answer.text, config.theme, config.color_theme, config.aspect_ratio
|
| 89 |
+
)
|
| 90 |
+
latex_code = replace_unicode_greek(latex_code)
|
| 91 |
+
compilation_status = compile_presentation(latex_code, work_dir)
|
| 92 |
+
|
| 93 |
+
if compilation_status:
|
| 94 |
+
return compilation_status, trace_id, work_dir
|
| 95 |
+
return "❌ Unknown critial error. Please try again later.", trace_id, work_dir
|
src/interface/__init__.py
CHANGED
|
@@ -1,3 +1,16 @@
|
|
|
|
|
|
|
|
| 1 |
from .gradio_interface import demo
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
__all__ = [
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""This module initializes the UI package."""
|
| 2 |
+
|
| 3 |
from .gradio_interface import demo
|
| 4 |
+
from .manage_files import delete_files, download_files, upload_files
|
| 5 |
+
from .session_manager import (check_session_status, create_session,
|
| 6 |
+
with_update_session)
|
| 7 |
|
| 8 |
+
__all__ = [
|
| 9 |
+
"demo",
|
| 10 |
+
"upload_files",
|
| 11 |
+
"download_files",
|
| 12 |
+
"delete_files",
|
| 13 |
+
"create_session",
|
| 14 |
+
"with_update_session",
|
| 15 |
+
"check_session_status",
|
| 16 |
+
]
|
src/interface/gradio_interface.py
ADDED
|
@@ -0,0 +1,506 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# pylint: disable=no-member, line-too-long
|
| 2 |
+
"""Gradio UI for the RAG'n'TeX LaTeX Presentation Generator."""
|
| 3 |
+
|
| 4 |
+
import base64
|
| 5 |
+
|
| 6 |
+
import gradio as gr
|
| 7 |
+
|
| 8 |
+
from ..generator import generate_presentation
|
| 9 |
+
from ..telemetry import submit_feedback
|
| 10 |
+
from .manage_files import download_files, upload_files
|
| 11 |
+
from .session_manager import (check_session_status, create_session,
|
| 12 |
+
with_update_session)
|
| 13 |
+
|
| 14 |
+
SESSION_TIMEOUT = 900
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def update_config(key, value, config) -> dict:
|
| 18 |
+
"""Helper function to update configuration settings.
|
| 19 |
+
These settings will be later used for generate_presentation function.
|
| 20 |
+
|
| 21 |
+
Args:
|
| 22 |
+
key (str): Configuration key to update.
|
| 23 |
+
value (str): New value for the configuration key.
|
| 24 |
+
config (dict): Current configuration dictionary.
|
| 25 |
+
|
| 26 |
+
Returns:
|
| 27 |
+
dict: Updated configuration dictionary with the new key-value pair.
|
| 28 |
+
"""
|
| 29 |
+
config[key] = value
|
| 30 |
+
return config
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
def upload_and_update_list(
|
| 34 |
+
files: list, uploaded_list, session_id
|
| 35 |
+
) -> tuple[str, list[str]]:
|
| 36 |
+
"""Helper function to handle uploaded documents and update the list of uploaded files.
|
| 37 |
+
Args:
|
| 38 |
+
files (list): List of file-like objects to be uploaded.
|
| 39 |
+
uploaded_list (list): Current list of uploaded file paths.
|
| 40 |
+
session_id (str): Unique identifier for the current session.
|
| 41 |
+
Returns:
|
| 42 |
+
tuple: A 2-element tuple:
|
| 43 |
+
- str: Status message indicating the result of the upload operation.
|
| 44 |
+
- list[str]: Updated list of uploaded file paths."""
|
| 45 |
+
|
| 46 |
+
status, new_paths = upload_files(files, session_id)
|
| 47 |
+
updated_list = uploaded_list + [p for p in new_paths if p not in uploaded_list]
|
| 48 |
+
|
| 49 |
+
return status, updated_list
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
def generate_iframe(folder_path) -> str:
|
| 53 |
+
"""Generate an HTML iframe to display the PDF presentation.
|
| 54 |
+
Args:
|
| 55 |
+
folder_path (str): Path to the folder containing the generated PDF presentation.
|
| 56 |
+
Returns:
|
| 57 |
+
str: HTML string containing the iframe to display the PDF.
|
| 58 |
+
"""
|
| 59 |
+
|
| 60 |
+
file_path = f"{folder_path}/presentation.pdf"
|
| 61 |
+
if not file_path:
|
| 62 |
+
return ""
|
| 63 |
+
with open(file_path, "rb") as f:
|
| 64 |
+
base64_pdf = base64.b64encode(f.read()).decode("utf-8")
|
| 65 |
+
pdf_display = f"""
|
| 66 |
+
<div style="position:relative; width:100%; padding-top:76.5%;">
|
| 67 |
+
<iframe src="data:application/pdf;base64,{base64_pdf}#view=FitH"
|
| 68 |
+
style="position:absolute; top:0; left:0; width:100%; height:100%; border:none;">
|
| 69 |
+
</iframe></div>
|
| 70 |
+
"""
|
| 71 |
+
|
| 72 |
+
return pdf_display
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
def encode_image(image_path) -> str:
|
| 76 |
+
"""Encode an image file to a base64 data URL.
|
| 77 |
+
Args:
|
| 78 |
+
image_path (str): Path to the image file.
|
| 79 |
+
Returns:
|
| 80 |
+
str: Base64 data URL of the image.
|
| 81 |
+
"""
|
| 82 |
+
with open(image_path, "rb") as f:
|
| 83 |
+
data = f.read()
|
| 84 |
+
return f"data:image/png;base64,{base64.b64encode(data).decode()}"
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
banner_base64 = encode_image("gfx/long_logo.png")
|
| 88 |
+
logo_base64 = encode_image("gfx/icon_logo.png")
|
| 89 |
+
github_base64 = encode_image("gfx/github-mark-white.png")
|
| 90 |
+
favicon_base64 = encode_image("gfx/favicon.ico")
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
# Custom Gradio theme
|
| 94 |
+
theme = gr.themes.Monochrome(
|
| 95 |
+
neutral_hue=gr.themes.Color(
|
| 96 |
+
c50="#f8f4fe",
|
| 97 |
+
c100="#ede9fe",
|
| 98 |
+
c200="#ddd6fe",
|
| 99 |
+
c300="#c4b5fd",
|
| 100 |
+
c400="#a78bfa",
|
| 101 |
+
c500="#8b5cf6",
|
| 102 |
+
c600="#7c3aed",
|
| 103 |
+
c700="#6d28d9",
|
| 104 |
+
c800="#5b21b6",
|
| 105 |
+
c900="#4c1d95",
|
| 106 |
+
c950="#181e44",
|
| 107 |
+
),
|
| 108 |
+
).set(body_background_fill="*neutral_50", body_text_color="*neutral_950")
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
# Additional JavaScript to handle session management and UI updates
|
| 112 |
+
JS_FUNC = r"""
|
| 113 |
+
() => {
|
| 114 |
+
// Force light theme
|
| 115 |
+
const url = new URL(window.location);
|
| 116 |
+
if (url.searchParams.get('__theme') !== 'light') {
|
| 117 |
+
url.searchParams.set('__theme', 'light');
|
| 118 |
+
window.location.href = url.href;
|
| 119 |
+
}
|
| 120 |
+
|
| 121 |
+
const overlayDiv = document.createElement("div");
|
| 122 |
+
overlayDiv.id = "session-overlay";
|
| 123 |
+
overlayDiv.style = `
|
| 124 |
+
display: flex;
|
| 125 |
+
position: fixed;
|
| 126 |
+
top: 0; left: 0; width: 100vw; height: 100vh;
|
| 127 |
+
justify-content: center;
|
| 128 |
+
align-items: center;
|
| 129 |
+
background-color: rgba(0, 0, 0, 0.7);
|
| 130 |
+
color: white;
|
| 131 |
+
font-size: 1.5rem;
|
| 132 |
+
text-align: center;
|
| 133 |
+
z-index: 999999;
|
| 134 |
+
`;
|
| 135 |
+
overlayDiv.innerHTML = `
|
| 136 |
+
❌ Session expired.<br/>🔄 Please refresh the page to start over.
|
| 137 |
+
`;
|
| 138 |
+
|
| 139 |
+
const checker = setInterval(() => {
|
| 140 |
+
const checkBtn = document.querySelector('#check-session-button');
|
| 141 |
+
if (checkBtn) checkBtn.click();
|
| 142 |
+
|
| 143 |
+
const statusBox = document.querySelector('#session-status textarea');
|
| 144 |
+
if (!statusBox) return;
|
| 145 |
+
|
| 146 |
+
const value = statusBox.value;
|
| 147 |
+
|
| 148 |
+
if (value === "expired") {
|
| 149 |
+
document.body.appendChild(overlayDiv);
|
| 150 |
+
console.log("Session expired, please refresh the page.");
|
| 151 |
+
|
| 152 |
+
clearInterval(checker);
|
| 153 |
+
}
|
| 154 |
+
}, 5000);
|
| 155 |
+
|
| 156 |
+
}
|
| 157 |
+
"""
|
| 158 |
+
|
| 159 |
+
|
| 160 |
+
with gr.Blocks(theme=theme, js=JS_FUNC) as demo:
|
| 161 |
+
# Session management
|
| 162 |
+
session_id_state = gr.State()
|
| 163 |
+
session_timeout = gr.State(SESSION_TIMEOUT)
|
| 164 |
+
status_output = gr.Textbox(visible=False, elem_id="session-status")
|
| 165 |
+
check_btn = gr.Button(visible=False, elem_id="check-session-button")
|
| 166 |
+
check_btn.click(
|
| 167 |
+
fn=check_session_status,
|
| 168 |
+
inputs=[session_id_state],
|
| 169 |
+
outputs=[status_output],
|
| 170 |
+
)
|
| 171 |
+
###
|
| 172 |
+
|
| 173 |
+
uploaded_files_state = gr.State([])
|
| 174 |
+
presentation_folder_state = gr.State("")
|
| 175 |
+
|
| 176 |
+
# Page title and favicon (doesn't work? Gradio is really agressive)
|
| 177 |
+
gr.HTML(
|
| 178 |
+
f"""
|
| 179 |
+
<script>
|
| 180 |
+
document.title = "RAG'n'TeX Presentation Generator";
|
| 181 |
+
|
| 182 |
+
const links = document.querySelectorAll("link[rel*='icon']");
|
| 183 |
+
links.forEach(link => link.parentNode.removeChild(link));
|
| 184 |
+
|
| 185 |
+
const newFavicon = document.createElement('link');
|
| 186 |
+
newFavicon.rel = 'icon';
|
| 187 |
+
newFavicon.type = 'image/png';
|
| 188 |
+
newFavicon.href = '{favicon_base64}';
|
| 189 |
+
document.head.appendChild(newFavicon);
|
| 190 |
+
</script>
|
| 191 |
+
""",
|
| 192 |
+
elem_id="custom-head-injection",
|
| 193 |
+
visible=False,
|
| 194 |
+
)
|
| 195 |
+
|
| 196 |
+
# Mess with the download-box
|
| 197 |
+
gr.HTML(
|
| 198 |
+
"""
|
| 199 |
+
<style>
|
| 200 |
+
#download-box svg.feather-file {
|
| 201 |
+
display: none !important;
|
| 202 |
+
}
|
| 203 |
+
</style>
|
| 204 |
+
""",
|
| 205 |
+
elem_id="custom-style-injection",
|
| 206 |
+
visible=False,
|
| 207 |
+
)
|
| 208 |
+
|
| 209 |
+
gr.Markdown(
|
| 210 |
+
'<h1 style="text-align:center;">AI-powered LaTeX Presentation Generator</h1>'
|
| 211 |
+
)
|
| 212 |
+
with gr.Row():
|
| 213 |
+
with gr.Column(scale=1):
|
| 214 |
+
gr.HTML(
|
| 215 |
+
f"""
|
| 216 |
+
<div style="text-align:center;">
|
| 217 |
+
<img src="{banner_base64}" style="max-width: 100%; height: auto;" />
|
| 218 |
+
</div>
|
| 219 |
+
"""
|
| 220 |
+
)
|
| 221 |
+
with gr.Column(scale=1):
|
| 222 |
+
gr.Markdown(
|
| 223 |
+
'<div style="text-align:justify; margin-top: 15px;">Turn your PDFs and ideas into beautiful LaTeX '
|
| 224 |
+
"presentations with AI. Built on top of the <a href=\"https://ragntex.github.io/lore/\">RAG'n'TeX</a>"
|
| 225 |
+
" engine, this tool automatically selects relevant content and compiles well-structured"
|
| 226 |
+
" slides. Ideal for students, researchers, and educators looking to save time and "
|
| 227 |
+
"create topic-focused presentations with minimal effort.</div>"
|
| 228 |
+
)
|
| 229 |
+
gr.Markdown(
|
| 230 |
+
'<div style="text-align:justify; margin-top: 0px;">Just upload your PDFs, click <b>Upload Files</b>, '
|
| 231 |
+
"choose a theme, enter your topic, and hit <b>Generate Presentation</b> — it's that easy!</div>"
|
| 232 |
+
)
|
| 233 |
+
gr.HTML(
|
| 234 |
+
f"""
|
| 235 |
+
<div style="display: flex; flex-direction: row; align-items: center; justify-content: center; gap: 20px; margin-bottom: 12px;">
|
| 236 |
+
<p style="font-size: 16px; line-height: 1; margin: 0;">Please ⭐️ us on GitHub!</p>
|
| 237 |
+
<a href="https://github.com/RAGnTeX/RAGnTeX" target="_blank" style="
|
| 238 |
+
display: inline-flex;
|
| 239 |
+
align-items: center;
|
| 240 |
+
justify-content: center;
|
| 241 |
+
padding: 12px 16px;
|
| 242 |
+
background-color: #4c1d95;
|
| 243 |
+
color: white;
|
| 244 |
+
font-weight: 600;
|
| 245 |
+
font-size: 16px;
|
| 246 |
+
line-height: 1;
|
| 247 |
+
border-radius: 0px;
|
| 248 |
+
text-decoration: none;
|
| 249 |
+
gap: 8px;
|
| 250 |
+
transition: background-color 0.2s ease-in-out;
|
| 251 |
+
border: 2px solid transparent;
|
| 252 |
+
"
|
| 253 |
+
onmouseover="this.style.backgroundColor='#6d28d9'; this.style.border='2px solid #686868';"
|
| 254 |
+
onmouseout="this.style.backgroundColor='#4c1d95'; this.style.border='2px solid transparent';"
|
| 255 |
+
>
|
| 256 |
+
<img src="{github_base64}" alt="GitHub" style="height: 20px;">
|
| 257 |
+
RAG'n'TeX
|
| 258 |
+
</a>
|
| 259 |
+
</div>
|
| 260 |
+
"""
|
| 261 |
+
)
|
| 262 |
+
|
| 263 |
+
with gr.Row():
|
| 264 |
+
with gr.Column(scale=1):
|
| 265 |
+
gr.Markdown("## 📂 Step 1: Upload PDF Documents")
|
| 266 |
+
file_input = gr.File(
|
| 267 |
+
file_types=[".pdf"], file_count="multiple", label="Select PDF Files"
|
| 268 |
+
)
|
| 269 |
+
upload_button = gr.Button("Upload Files", variant="primary")
|
| 270 |
+
|
| 271 |
+
gr.Markdown("## 🎨 Step 2: Choose the parameters")
|
| 272 |
+
config_state = gr.State({})
|
| 273 |
+
model_state = gr.State("default")
|
| 274 |
+
model_radio = gr.Radio(
|
| 275 |
+
["gemini-2.0-flash", "gemini-2.5-flash-preview-05-20"],
|
| 276 |
+
label="LLM",
|
| 277 |
+
info="Choose Gemini version",
|
| 278 |
+
value="gemini-2.0-flash",
|
| 279 |
+
)
|
| 280 |
+
model_radio.change(
|
| 281 |
+
fn=lambda value, config: update_config("model_name", value, config),
|
| 282 |
+
inputs=[model_radio, config_state], # only real components here
|
| 283 |
+
outputs=config_state,
|
| 284 |
+
)
|
| 285 |
+
|
| 286 |
+
aspect_ratio_radio = gr.Radio(
|
| 287 |
+
["16:9", "4:3"],
|
| 288 |
+
label="Aspect Ratio",
|
| 289 |
+
info="Choose presentation aspect ratio",
|
| 290 |
+
value="16:9",
|
| 291 |
+
)
|
| 292 |
+
|
| 293 |
+
aspect_ratio_radio.change(
|
| 294 |
+
fn=lambda value, config: update_config("aspect_ratio", value, config),
|
| 295 |
+
inputs=[aspect_ratio_radio, config_state], # only real components here
|
| 296 |
+
outputs=config_state,
|
| 297 |
+
)
|
| 298 |
+
presentation_theme_state = gr.State("default")
|
| 299 |
+
theme_dropdown = gr.Dropdown(
|
| 300 |
+
choices=[
|
| 301 |
+
"default",
|
| 302 |
+
"Darmstadt",
|
| 303 |
+
"Malmoe",
|
| 304 |
+
"AnnArbor",
|
| 305 |
+
"Dresden",
|
| 306 |
+
"Marburg",
|
| 307 |
+
"Antibes",
|
| 308 |
+
"Frankfurt",
|
| 309 |
+
"Montpellier",
|
| 310 |
+
"Bergen",
|
| 311 |
+
"Goettingen",
|
| 312 |
+
"PaloAlto",
|
| 313 |
+
"Berkeley",
|
| 314 |
+
"Hannover",
|
| 315 |
+
"Pittsburgh",
|
| 316 |
+
"Berlin",
|
| 317 |
+
"Ilmenau",
|
| 318 |
+
"Rochester",
|
| 319 |
+
"Boadilla",
|
| 320 |
+
"JuanLesPins",
|
| 321 |
+
"Singapore",
|
| 322 |
+
"CambridgeUS",
|
| 323 |
+
"Luebeck",
|
| 324 |
+
"Szeged",
|
| 325 |
+
"Copenhagen",
|
| 326 |
+
"Madrid",
|
| 327 |
+
"Warsaw",
|
| 328 |
+
],
|
| 329 |
+
value="default", # default option
|
| 330 |
+
label="Choose a presentation theme",
|
| 331 |
+
)
|
| 332 |
+
|
| 333 |
+
theme_dropdown.change(
|
| 334 |
+
fn=lambda value, config: update_config("theme", value, config),
|
| 335 |
+
inputs=[theme_dropdown, config_state], # only real components here
|
| 336 |
+
outputs=config_state,
|
| 337 |
+
)
|
| 338 |
+
color_theme_state = gr.State("default")
|
| 339 |
+
color_dropdown = gr.Dropdown(
|
| 340 |
+
choices=[
|
| 341 |
+
"default",
|
| 342 |
+
"albatross",
|
| 343 |
+
"beetle",
|
| 344 |
+
"crane",
|
| 345 |
+
"dolphin",
|
| 346 |
+
"fly",
|
| 347 |
+
"lily",
|
| 348 |
+
"orchid",
|
| 349 |
+
"rose",
|
| 350 |
+
"seagull",
|
| 351 |
+
"seahorse",
|
| 352 |
+
"sidebartab",
|
| 353 |
+
"structure",
|
| 354 |
+
"whale",
|
| 355 |
+
"wolverine",
|
| 356 |
+
],
|
| 357 |
+
value="default", # default option
|
| 358 |
+
label="Choose a color theme",
|
| 359 |
+
)
|
| 360 |
+
|
| 361 |
+
color_dropdown.change(
|
| 362 |
+
fn=lambda value, config: update_config("color_theme", value, config),
|
| 363 |
+
inputs=[color_dropdown, config_state], # only real components here
|
| 364 |
+
outputs=config_state,
|
| 365 |
+
)
|
| 366 |
+
|
| 367 |
+
gr.Markdown("## 🧠 Step 3: Enter Presentation Topic")
|
| 368 |
+
topic_input = gr.Textbox(
|
| 369 |
+
label="Presentation Topic",
|
| 370 |
+
placeholder="e.g., Introduction to Quantum Computing",
|
| 371 |
+
lines=1,
|
| 372 |
+
)
|
| 373 |
+
topic_input.change(
|
| 374 |
+
fn=lambda value, config: update_config("topic", value, config),
|
| 375 |
+
inputs=[topic_input, config_state], # only real components here
|
| 376 |
+
outputs=config_state,
|
| 377 |
+
)
|
| 378 |
+
submit_topic_button = gr.Button("Generate Presentation", variant="primary")
|
| 379 |
+
|
| 380 |
+
with gr.Column(scale=1):
|
| 381 |
+
gr.Markdown("## 📄 Uploaded Files")
|
| 382 |
+
upload_output = gr.Textbox(
|
| 383 |
+
label="Upload Status", lines=3, interactive=False
|
| 384 |
+
)
|
| 385 |
+
|
| 386 |
+
gr.Markdown("## 📝 Presentation Processing")
|
| 387 |
+
compilation_status = gr.Textbox(
|
| 388 |
+
label="Compilation Status", lines=1, interactive=False
|
| 389 |
+
)
|
| 390 |
+
|
| 391 |
+
gr.Markdown("## 🎉 Final Presentation")
|
| 392 |
+
pdf_output = gr.File(
|
| 393 |
+
label="Download/View Presentation",
|
| 394 |
+
interactive=False,
|
| 395 |
+
elem_id="download-box",
|
| 396 |
+
)
|
| 397 |
+
pdf_output_viewer = gr.HTML(label="Presentation Preview")
|
| 398 |
+
trace_id_state = gr.State("")
|
| 399 |
+
|
| 400 |
+
gr.Markdown("<br>")
|
| 401 |
+
|
| 402 |
+
with gr.Row():
|
| 403 |
+
with gr.Column(scale=1):
|
| 404 |
+
gr.Markdown("## ⭐ Rate Us")
|
| 405 |
+
rating = gr.Radio(
|
| 406 |
+
choices=["⭐️", "⭐️⭐️", "⭐️⭐️⭐️", "⭐️⭐️⭐️⭐️", "⭐️⭐️⭐️⭐️⭐️"],
|
| 407 |
+
label="How would you rate this app?",
|
| 408 |
+
interactive=True,
|
| 409 |
+
type="value",
|
| 410 |
+
value=None,
|
| 411 |
+
show_label=True,
|
| 412 |
+
container=True,
|
| 413 |
+
)
|
| 414 |
+
feedback_comment = gr.Textbox(
|
| 415 |
+
label="Give us a comment (optional)",
|
| 416 |
+
placeholder="What did you like or what can be improved?",
|
| 417 |
+
lines=3,
|
| 418 |
+
max_lines=5,
|
| 419 |
+
interactive=True,
|
| 420 |
+
)
|
| 421 |
+
submit_feedback_button = gr.Button("Submit Feedback", variant="secondary")
|
| 422 |
+
feedback_output = gr.Textbox(
|
| 423 |
+
label="Feedback Status", interactive=False, lines=2
|
| 424 |
+
)
|
| 425 |
+
|
| 426 |
+
with gr.Column(scale=1):
|
| 427 |
+
gr.Markdown("## 🙌 Credits")
|
| 428 |
+
gr.HTML(
|
| 429 |
+
f"""
|
| 430 |
+
<div style="display: flex; flex-direction: row; gap: 24px; margin-top: 0; align-items: flex-start;">
|
| 431 |
+
|
| 432 |
+
<!-- Left Column -->
|
| 433 |
+
<div style="flex: 1;">
|
| 434 |
+
<div style="text-align:justify; margin-top: 0px;">
|
| 435 |
+
<p> RAG'n'TeX is a collaborative effort by a team of developers and researchers driven
|
| 436 |
+
to make the everyday work of scientists and professionals easier. By combining LaTeX with AI,
|
| 437 |
+
we create smart tools that help you build polished, accurate presentations quickly and smoothly.
|
| 438 |
+
Our goal is to make the process simple and stress-free, so you can spend more time on what really matters.</p>
|
| 439 |
+
<br>
|
| 440 |
+
<p>📄 Licensed under the <a href="https://opensource.org/licenses/MIT" target="_blank" rel="noopener noreferrer">MIT License</a></p>
|
| 441 |
+
<br>
|
| 442 |
+
<h4>Authors:</h4>
|
| 443 |
+
<ul style="padding-left: 20px; margin-top: 4px;">
|
| 444 |
+
<li>👩🏻💻 <a href="https://github.com/AnnaErsh" target="_blank">Anna Ershova</a></li>
|
| 445 |
+
<li>👨🏼🔬 <a href="https://github.com/KajetanNiewczas" target="_blank">Kajetan Niewczas</a></li>
|
| 446 |
+
</ul>
|
| 447 |
+
</div>
|
| 448 |
+
</div>
|
| 449 |
+
|
| 450 |
+
<!-- Right Column -->
|
| 451 |
+
<div style="flex: 1; display: flex; justify-content: flex-end; align-items: center;">
|
| 452 |
+
<img src="{logo_base64}" alt="Illustration" style="max-width: 100%; border-radius: 8px;">
|
| 453 |
+
</div>
|
| 454 |
+
</div>
|
| 455 |
+
"""
|
| 456 |
+
)
|
| 457 |
+
|
| 458 |
+
# Event handlers
|
| 459 |
+
|
| 460 |
+
demo.load(
|
| 461 |
+
fn=create_session,
|
| 462 |
+
inputs=session_timeout,
|
| 463 |
+
outputs=session_id_state,
|
| 464 |
+
)
|
| 465 |
+
|
| 466 |
+
upload_button.click(
|
| 467 |
+
fn=with_update_session(upload_and_update_list),
|
| 468 |
+
inputs=[file_input, uploaded_files_state, session_id_state],
|
| 469 |
+
outputs=[upload_output, uploaded_files_state],
|
| 470 |
+
)
|
| 471 |
+
|
| 472 |
+
trace_id_state = gr.State("")
|
| 473 |
+
submit_topic_button.click(
|
| 474 |
+
fn=with_update_session(generate_presentation),
|
| 475 |
+
inputs=[
|
| 476 |
+
config_state,
|
| 477 |
+
session_id_state,
|
| 478 |
+
],
|
| 479 |
+
outputs=[compilation_status, trace_id_state, presentation_folder_state],
|
| 480 |
+
)
|
| 481 |
+
|
| 482 |
+
compilation_status.change(
|
| 483 |
+
fn=download_files,
|
| 484 |
+
inputs=[
|
| 485 |
+
presentation_folder_state,
|
| 486 |
+
session_id_state,
|
| 487 |
+
],
|
| 488 |
+
outputs=pdf_output,
|
| 489 |
+
)
|
| 490 |
+
|
| 491 |
+
pdf_output.change(
|
| 492 |
+
fn=generate_iframe,
|
| 493 |
+
inputs=presentation_folder_state,
|
| 494 |
+
outputs=pdf_output_viewer,
|
| 495 |
+
)
|
| 496 |
+
|
| 497 |
+
submit_feedback_button.click(
|
| 498 |
+
fn=with_update_session(submit_feedback),
|
| 499 |
+
inputs=[
|
| 500 |
+
rating,
|
| 501 |
+
feedback_comment,
|
| 502 |
+
trace_id_state,
|
| 503 |
+
session_id_state,
|
| 504 |
+
],
|
| 505 |
+
outputs=feedback_output,
|
| 506 |
+
)
|
src/interface/manage_files.py
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Module to handle file uploads and downloads via UI."""
|
| 2 |
+
|
| 3 |
+
import re
|
| 4 |
+
import shutil
|
| 5 |
+
import zipfile
|
| 6 |
+
from pathlib import Path
|
| 7 |
+
|
| 8 |
+
from ..database import ingest_files_to_db
|
| 9 |
+
from ..telemetry.logging_utils import Logger
|
| 10 |
+
|
| 11 |
+
LOGGER = Logger.get_logger()
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
def upload_files(files: list, session_id: str) -> tuple[str, list[str]]:
|
| 15 |
+
"""Upload files and save them in the temporary directory.
|
| 16 |
+
Args:
|
| 17 |
+
files (list): List of file-like objects to be uploaded.
|
| 18 |
+
session_id (str): Unique identifier for the current session.
|
| 19 |
+
Returns:
|
| 20 |
+
str: Message indicating the result of the upload operation.
|
| 21 |
+
list[str]: List of saved file paths.
|
| 22 |
+
"""
|
| 23 |
+
if not files:
|
| 24 |
+
return "❌ No new files selected", []
|
| 25 |
+
|
| 26 |
+
upload_dir = Path.cwd() / "tmp" / session_id
|
| 27 |
+
upload_dir.mkdir(parents=True, exist_ok=True)
|
| 28 |
+
|
| 29 |
+
messages = []
|
| 30 |
+
saved_paths = []
|
| 31 |
+
|
| 32 |
+
for file in files:
|
| 33 |
+
try:
|
| 34 |
+
temp_path = Path(file.name)
|
| 35 |
+
# Prepare the safe file name
|
| 36 |
+
safe_name = re.sub(r"[^a-zA-Z0-9_]", "_", temp_path.stem)
|
| 37 |
+
safe_name = re.sub(r"_+", "_", safe_name)
|
| 38 |
+
file_name = f"{safe_name}{temp_path.suffix}"
|
| 39 |
+
target_path = upload_dir / file_name
|
| 40 |
+
|
| 41 |
+
if target_path.exists():
|
| 42 |
+
messages.append(f"⚠️ File already exists: {file_name}")
|
| 43 |
+
continue
|
| 44 |
+
|
| 45 |
+
if not temp_path.exists():
|
| 46 |
+
messages.append(f"❌ File not found: {file_name}")
|
| 47 |
+
continue
|
| 48 |
+
|
| 49 |
+
shutil.move(str(temp_path), str(target_path))
|
| 50 |
+
messages.append(f"✅ Uploaded new file: {file_name}")
|
| 51 |
+
LOGGER.info("📤 Uploaded file %s to %s", file_name, upload_dir)
|
| 52 |
+
|
| 53 |
+
saved_paths.append(str(target_path))
|
| 54 |
+
|
| 55 |
+
except (FileNotFoundError, PermissionError, shutil.Error, OSError) as e:
|
| 56 |
+
LOGGER.error("❌ Error uploading %s", file_name, exc_info=e)
|
| 57 |
+
messages.append(f"❌ Error uploading {file_name}: {e}")
|
| 58 |
+
|
| 59 |
+
if saved_paths:
|
| 60 |
+
failed = ingest_files_to_db(saved_paths, session_id)
|
| 61 |
+
if failed:
|
| 62 |
+
messages.append(
|
| 63 |
+
f"⚠️ Failed to process files: {', '.join(Path(f).name for f in failed)}"
|
| 64 |
+
)
|
| 65 |
+
LOGGER.warning(
|
| 66 |
+
"⚠️ Failed to process files: %s", ", ".join(Path(f).name for f in failed)
|
| 67 |
+
)
|
| 68 |
+
else:
|
| 69 |
+
messages.append("🛠️ All files processed successfully!")
|
| 70 |
+
LOGGER.info("🛠️ All files processed successfully!")
|
| 71 |
+
|
| 72 |
+
return "\n".join(messages), saved_paths
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
def download_files(folder_path, session_id) -> str:
|
| 76 |
+
"""Prepare downloadable zip archive from the specified folder path."""
|
| 77 |
+
|
| 78 |
+
if not folder_path:
|
| 79 |
+
return "No folder path provided."
|
| 80 |
+
|
| 81 |
+
folder = Path(folder_path)
|
| 82 |
+
zip_path = Path.cwd() / "tmp" / session_id / "presentation.zip"
|
| 83 |
+
|
| 84 |
+
with zipfile.ZipFile(zip_path, "w", zipfile.ZIP_DEFLATED) as zipf:
|
| 85 |
+
for file in folder.rglob("*"):
|
| 86 |
+
if file.is_file() and file != zip_path:
|
| 87 |
+
arcname = Path("presentation") / file.relative_to(folder)
|
| 88 |
+
zipf.write(file, arcname=arcname)
|
| 89 |
+
LOGGER.info("📦 Created zip archive at %s", zip_path)
|
| 90 |
+
|
| 91 |
+
return str(zip_path)
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
def delete_files(session_id: str) -> None:
|
| 95 |
+
"""Delete all files in the specified session's temporary directory."""
|
| 96 |
+
|
| 97 |
+
upload_dir = Path.cwd() / "tmp" / session_id
|
| 98 |
+
|
| 99 |
+
if not upload_dir.exists():
|
| 100 |
+
return
|
| 101 |
+
|
| 102 |
+
try:
|
| 103 |
+
shutil.rmtree(upload_dir)
|
| 104 |
+
LOGGER.info("🗑️ Deleted folder: %s", upload_dir)
|
| 105 |
+
return
|
| 106 |
+
except Exception as e:
|
| 107 |
+
LOGGER.error("❌ Error deleting: %s", upload_dir, exc_info=e)
|
| 108 |
+
return
|
src/interface/session_manager.py
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Session Manager Module"""
|
| 2 |
+
|
| 3 |
+
import threading
|
| 4 |
+
import time
|
| 5 |
+
import uuid
|
| 6 |
+
from functools import wraps
|
| 7 |
+
from typing import Any, Callable
|
| 8 |
+
|
| 9 |
+
from ..database import clean_db
|
| 10 |
+
from ..telemetry.logging_utils import Logger
|
| 11 |
+
from .manage_files import delete_files
|
| 12 |
+
|
| 13 |
+
LOGGER = Logger.get_logger()
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
# Global session manager
|
| 17 |
+
session_data = {}
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def create_session(timeout=600) -> str:
|
| 21 |
+
"""Create a new session and return its ID.
|
| 22 |
+
This function generates a unique session ID, initializes the session data
|
| 23 |
+
with the current timestamp, and starts a background thread to monitor
|
| 24 |
+
the session for inactivity. If the session is inactive for a specified
|
| 25 |
+
timeout period, it will be automatically cleaned up.
|
| 26 |
+
|
| 27 |
+
Returns:
|
| 28 |
+
str: The unique session ID.
|
| 29 |
+
"""
|
| 30 |
+
|
| 31 |
+
# Create new session
|
| 32 |
+
session_id = str(uuid.uuid4())
|
| 33 |
+
session_data[session_id] = {
|
| 34 |
+
"last_active": time.time(),
|
| 35 |
+
}
|
| 36 |
+
LOGGER.info("🪪 Created new session with ID: %s", session_id)
|
| 37 |
+
|
| 38 |
+
# Start clean-up watcher
|
| 39 |
+
threading.Thread(
|
| 40 |
+
target=watch_session, args=(session_id, timeout), daemon=True
|
| 41 |
+
).start()
|
| 42 |
+
|
| 43 |
+
return session_id
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
def watch_session(session_id, timeout=600) -> None:
|
| 47 |
+
"""Clean up if no activity for `timeout` seconds.
|
| 48 |
+
This function runs in a separate thread and checks if the session
|
| 49 |
+
is still active. If the session has not been active for the specified
|
| 50 |
+
timeout period, it deletes the session data.
|
| 51 |
+
|
| 52 |
+
Args:
|
| 53 |
+
session_id (str): The ID of the session to watch.
|
| 54 |
+
timeout (int): The time in seconds after which the session is considered inactive.
|
| 55 |
+
"""
|
| 56 |
+
|
| 57 |
+
while True:
|
| 58 |
+
time.sleep(30)
|
| 59 |
+
session = session_data.get(session_id)
|
| 60 |
+
if not session:
|
| 61 |
+
break
|
| 62 |
+
if time.time() - session["last_active"] > timeout:
|
| 63 |
+
LOGGER.info("🕒 Session expired, ID: %s", session_id)
|
| 64 |
+
del session_data[session_id]
|
| 65 |
+
time.sleep(10)
|
| 66 |
+
delete_files(session_id)
|
| 67 |
+
clean_db(session_id)
|
| 68 |
+
LOGGER.info("🧹 Deleted session with ID: %s", session_id)
|
| 69 |
+
break
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
def update_session(session_id) -> None:
|
| 73 |
+
"""Update the last active time for a session.
|
| 74 |
+
This function checks if the session ID exists in the session data
|
| 75 |
+
and updates its last active timestamp.
|
| 76 |
+
|
| 77 |
+
Args:
|
| 78 |
+
session_id (str): The ID of the session to update.
|
| 79 |
+
"""
|
| 80 |
+
|
| 81 |
+
if session_id in session_data:
|
| 82 |
+
session_data[session_id]["last_active"] = time.time()
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
def with_update_session(fn: Callable[..., Any]) -> Callable[..., Any]:
|
| 86 |
+
"""Decorator to update session activity.
|
| 87 |
+
This decorator checks if the session ID is provided in the arguments
|
| 88 |
+
and updates the session's last active time.
|
| 89 |
+
|
| 90 |
+
Args:
|
| 91 |
+
fn (callable): The function to wrap.
|
| 92 |
+
|
| 93 |
+
Returns:
|
| 94 |
+
callable: The wrapped function that updates the session activity.
|
| 95 |
+
"""
|
| 96 |
+
|
| 97 |
+
@wraps(fn)
|
| 98 |
+
def wrapper(*args, **kwargs):
|
| 99 |
+
try:
|
| 100 |
+
session_id = kwargs.get("session_id")
|
| 101 |
+
if session_id is None and args:
|
| 102 |
+
session_id = args[-1]
|
| 103 |
+
if session_id in session_data:
|
| 104 |
+
update_session(session_id)
|
| 105 |
+
except Exception as e:
|
| 106 |
+
LOGGER.error("❌ Failed to upadate session ID: %s", session_id, exc_info=e)
|
| 107 |
+
return fn(*args, **kwargs)
|
| 108 |
+
|
| 109 |
+
return wrapper
|
| 110 |
+
|
| 111 |
+
|
| 112 |
+
def check_session_status(session_id: str) -> str:
|
| 113 |
+
"""Check if a session is active or expired.
|
| 114 |
+
This function checks if the provided session ID exists in the session data.
|
| 115 |
+
If it exists, the session is considered active; otherwise, it is expired.
|
| 116 |
+
|
| 117 |
+
Args:
|
| 118 |
+
session_id (str): The ID of the session to check.
|
| 119 |
+
|
| 120 |
+
Returns:
|
| 121 |
+
str: "active" if the session is active, "expired" if it is not.
|
| 122 |
+
"""
|
| 123 |
+
return "active" if session_id in session_data else "expired"
|