Created
July 13, 2026 19:14
-
-
Save wilyJ80/de0a28f4b16aaa2e6b1091c279cd35b0 to your computer and use it in GitHub Desktop.
llamacpp-summarize
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
| from langchain_text_splitters import RecursiveCharacterTextSplitter | |
| from llama_cpp import Llama, CreateChatCompletionResponse | |
| from typing import cast | |
| from pymupdf import pymupdf | |
| print('[INFO] Loading text...') | |
| doc = pymupdf.open('./report.pdf') | |
| content = '' | |
| for page in doc: | |
| content += page.get_text() | |
| print('[INFO] Splitting text...') | |
| splitter = RecursiveCharacterTextSplitter(chunk_size=2000, chunk_overlap=200) | |
| splits = splitter.split_text(content) | |
| llm = Llama( | |
| model_path="./models/Qwen3.5-4B-Q4_K_M.gguf", | |
| verbose=False, | |
| n_ctx=4098, | |
| n_threads=8, | |
| n_batch=512, | |
| n_ubatch=512, | |
| use_mmap=True, | |
| use_mlock=False, | |
| flash_attn=True, | |
| n_gpu_layers=0, | |
| seed=42, | |
| temperature=0 | |
| ) | |
| print('[INFO] Starting AI summarization...') | |
| first_summary = '' | |
| for split in splits: | |
| response = cast( | |
| CreateChatCompletionResponse, | |
| llm.create_chat_completion( | |
| messages=[ | |
| { | |
| "role": "system", | |
| "content": """ | |
| você deve resumir este conteúdo. | |
| mantenha o resumo conciso e traga \"o quê\" ao invés de \"como\". | |
| """ | |
| }, | |
| { | |
| "role": "user", | |
| "content": f""" | |
| <CONTEXTO> | |
| {split} | |
| </CONTEXTO> | |
| """ | |
| } | |
| ], | |
| temperature=0.1, | |
| stream=False | |
| ) | |
| ) | |
| result = response['choices'][0]['message']['content'] | |
| print("=============================================================") | |
| print(result) | |
| first_summary += f"\n\n{result}" | |
| print("======================== RESUMO 1 ==========================") | |
| print(first_summary) | |
| response = cast( | |
| CreateChatCompletionResponse, | |
| llm.create_chat_completion( | |
| messages=[ | |
| { | |
| "role": "system", | |
| "content": """ | |
| você deve resumir este conteúdo. | |
| mantenha o resumo conciso e traga \"o quê\" ao invés de \"como\". | |
| """ | |
| }, | |
| { | |
| "role": "user", | |
| "content": f""" | |
| <CONTEXTO> | |
| {first_summary} | |
| </CONTEXTO> | |
| """ | |
| } | |
| ], | |
| temperature=0.1, | |
| stream=False | |
| ) | |
| ) | |
| result = response['choices'][0]['message']['content'] | |
| print("======================== RESUMO 2 ==========================") | |
| print(result) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment