-
Notifications
You must be signed in to change notification settings - Fork 8
adding pdf-tex summarizer example #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
HadiYoussef2005
wants to merge
2
commits into
main
Choose a base branch
from
pdf_summarizer_tex_example
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
MODEL=gpt-4o-mini | ||
ENVIRONMENT=dev | ||
OPENAI_API_KEY= | ||
OPENROUTER_MODEL=gryphe/mythomax-l2-13b | ||
SWARMZERO_LOG_LEVEL=INFO | ||
LANGTRACE_API_KEY= |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.venv | ||
swarmzero-data | ||
.env |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# PDF Summarizer to Tex Workflow | ||
|
||
A sequential workflow built using SwarmZero framework that enables concise summarization of large pdf's to be outputted in a tex file | ||
|
||
## Description | ||
|
||
This workflow utilizes PyMuPDF for extracting text out of a pdf and is built on top fo the SwarmZero framework, providing enhanced summarization cpabilities wiht AI-powered processing. | ||
|
||
## Prerequisites | ||
|
||
- Python 3.11 or higher | ||
- Poetry package manager | ||
- OpenAI API Key | ||
- Langtrace API Key | ||
|
||
## Installation | ||
|
||
1. Clone the repository: | ||
```bash | ||
git clone https://github.com/swarmzero/examples.git | ||
cd examples/workflows/pdf_summarizer_tex_workflow | ||
``` | ||
|
||
2. Install dependencies using Poetry: | ||
```bash | ||
poetry install --no-root | ||
``` | ||
|
||
3. Set up environment variables: | ||
Create a `.env` file in the root directory and add your API keys based on the .env.example file | ||
## Usage | ||
|
||
Run the workflow on a pdf like so: | ||
``` bash | ||
python main.py path/to/pdf/text.pdf | ||
``` | ||
|
||
## Learn more | ||
Visit [SwarmZero](https://swarmzero.ai) to learn more about the SwarmZero framework. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
from .formatter_agent import formatter_agent | ||
from .summarizer_agent import summarizer_agent | ||
|
||
__all__ = ["formatter_agent", "summarizer_agent"] |
20 changes: 20 additions & 0 deletions
20
workflows/pdf_summarizer_tex_workflow/agents/formatter_agent.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from swarmzero.agent import Agent, SDKContext | ||
import os | ||
from tools import format_latex | ||
|
||
CONFIG_PATH = os.path.abspath( | ||
os.path.join( | ||
os.path.dirname(__file__), | ||
os.pardir, | ||
"swarmzero_config.toml" | ||
) | ||
) | ||
sdk_context = SDKContext(CONFIG_PATH) | ||
formatter_agent = Agent( | ||
name="formatter", | ||
functions=[format_latex], | ||
instruction="Format bullet points into LaTeX.", | ||
description="Wraps bullets into a LaTeX itemize document.", | ||
sdk_context=sdk_context, | ||
chat_only_mode=True, | ||
) |
20 changes: 20 additions & 0 deletions
20
workflows/pdf_summarizer_tex_workflow/agents/summarizer_agent.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from swarmzero.agent import Agent, SDKContext | ||
import os | ||
from tools import summarize_bullets | ||
|
||
CONFIG_PATH = os.path.abspath( | ||
os.path.join( | ||
os.path.dirname(__file__), | ||
os.pardir, | ||
"swarmzero_config.toml" | ||
) | ||
) | ||
sdk_context = SDKContext(CONFIG_PATH) | ||
summarizer_agent = Agent( | ||
name="summarizer", | ||
functions=[summarize_bullets], | ||
instruction="Summarize text into bullet points.", | ||
description="Returns bullet-point summary for a given text chunk.", | ||
sdk_context=sdk_context, | ||
chat_only_mode=True, | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import asyncio | ||
import os | ||
import sys | ||
from typing import List | ||
|
||
from swarmzero.sdk_context import SDKContext | ||
from swarmzero.workflow import Workflow, WorkflowStep, StepMode | ||
|
||
from agents import formatter_agent, summarizer_agent | ||
from util import extract_text_from_pdf, chunk_text | ||
|
||
CONFIG_PATH = os.path.join(os.path.dirname(__file__), "swarmzero_config.toml") | ||
sdk_context = SDKContext(CONFIG_PATH) | ||
|
||
async def run_summarizer(chunk: str, **kwargs) -> str: | ||
return await summarizer_agent.chat(chunk) | ||
|
||
async def run_formatter(bullets: str, **kwargs) -> str: | ||
return await formatter_agent.chat(bullets) | ||
|
||
workflow = Workflow( | ||
HadiYoussef2005 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
name="pdf_latex_summary", | ||
instruction="Summarize PDF and convert to LaTeX.", | ||
description="Runs summarizer then formatter agents sequentially.", | ||
steps=[ | ||
WorkflowStep(name="SummarizeBullets", | ||
runner=run_summarizer, | ||
mode=StepMode.SEQUENTIAL), | ||
|
||
WorkflowStep(name="FormatLatex", | ||
runner=run_formatter, | ||
mode=StepMode.SEQUENTIAL), | ||
], | ||
sdk_context=sdk_context, | ||
) | ||
|
||
async def main(): | ||
pdf_path = sys.argv[1] if len(sys.argv) > 1 else "sample.pdf" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. include a sample pdf under a new 'data' folder |
||
text = extract_text_from_pdf(pdf_path) | ||
chunks = chunk_text(text) | ||
|
||
all_items: List[str] = [] | ||
for idx, chunk in enumerate(chunks, start=1): | ||
print(f"Chunk {idx}/{len(chunks)} → processing…") | ||
latex_doc = await workflow.run(chunk) | ||
all_items += [line.strip() for line in latex_doc.splitlines() if line.strip().startswith("\\item")] | ||
|
||
final = ( | ||
"\\documentclass{article}\n" | ||
"\\usepackage[utf8]{inputenc}\n" | ||
"\\usepackage{enumitem}\n" | ||
"\\begin{document}\n" | ||
"\\section*{Summary Notes}\n" | ||
"\\begin{itemize}[leftmargin=*, label=--]\n" | ||
+ "\n".join(all_items) + | ||
"\n\\end{itemize}\n" | ||
"\\end{document}" | ||
) | ||
|
||
out_file = os.path.splitext(os.path.basename(pdf_path))[0] + "_summary.tex" | ||
with open(out_file, "w", encoding="utf-8") as f: | ||
f.write(final) | ||
print(f"Written LaTeX summary to {out_file}") | ||
|
||
if __name__ == "__main__": | ||
asyncio.run(main()) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.