natalieac commited on
Commit
143a91a
·
verified ·
1 Parent(s): 8618163

Upload 2 files

Browse files
Files changed (2) hide show
  1. backend/settings.py +42 -0
  2. backend/summarizer.py +39 -0
backend/settings.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from pathlib import Path
2
+ from pydantic import BaseModel
3
+ import os
4
+
5
+ class Settings(BaseModel):
6
+
7
+ ROOT: Path = Path(".")
8
+ DATA_DIR: Path = ROOT / "backend" / "data"
9
+ RAW_DIR: Path = DATA_DIR / "raw"
10
+ PROCESSED_DIR: Path = DATA_DIR / "processed"
11
+ INDEX_DIR: Path = ROOT / "backend" / "index"
12
+ MODEL_DIR: Path = ROOT / "backend" / "models"
13
+
14
+
15
+ EMBEDDING_MODEL_ID: str = "BAAI/bge-m3"
16
+ RERANK_MODEL_ID: str = "BAAI/bge-reranker-v2-m3"
17
+ LLM_REPO_ID: str = "Qwen/Qwen2.5-3B-Instruct-GGUF"
18
+ LLM_FILENAME_PATTERN: str = "Qwen2.5-3B-Instruct-Q4_K_M.gguf"
19
+
20
+
21
+ LLM_PATH: Path = MODEL_DIR / "qwen2.5-3b-gguf" / "Qwen2.5-3B-Instruct-Q4_K_M.gguf"
22
+
23
+
24
+ N_CHARS_CHUNK: int = 2000
25
+ N_CHARS_OVERLAP: int = 300
26
+ TOP_K: int = 8
27
+ TOP_K_RERANK: int = 3
28
+
29
+
30
+ LLM_N_CTX: int = 4096
31
+ LLM_N_GPU_LAYERS: int = 0
32
+
33
+ HF_TOKEN: str | None = (
34
+ os.getenv("study")
35
+ or os.getenv("HF_TOKEN")
36
+ or os.getenv("HUGGINGFACE_HUB_TOKEN")
37
+ )
38
+
39
+ settings = Settings()
40
+
41
+
42
+
backend/summarizer.py ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from llm_local import get_llm
2
+ from rank_bm25 import BM25Okapi
3
+ import re
4
+
5
+ def _split_sentences(text: str):
6
+ sents = re.split(r'(?<=[.!?])\s+', text)
7
+ return [s.strip() for s in sents if s.strip()]
8
+
9
+ def extractive_sents(chunks: list[dict], query: str | None, max_sents=80):
10
+ sents = []
11
+ for ch in chunks:
12
+ for s in _split_sentences(ch["text"]):
13
+ sents.append((s, ch["meta"].get("page_num")))
14
+ if not sents: return []
15
+ corpus = [s for s,_ in sents]
16
+ bm25 = BM25Okapi([c.split() for c in corpus])
17
+ if query:
18
+ scores = bm25.get_scores(query.split())
19
+ ranked = [(corpus[i], sents[i][1]) for i,_ in sorted(enumerate(scores), key=lambda z:z[1], reverse=True)]
20
+ return ranked[:max_sents]
21
+ return sents[:max_sents]
22
+
23
+ def map_reduce(chunks: list[dict], focus: str, style: str, length: str):
24
+ llm = get_llm()
25
+ partials = []
26
+ for ch in chunks:
27
+ p = ch["meta"].get("page_num")
28
+ prompt = f"Resuma em PT-BR, foco: {focus}, tamanho: {length}. Prefixe a página se houver.\n\n" \
29
+ f"{'[p.'+str(p)+'] ' if p else ''}{ch['text'][:2500]}"
30
+ out = llm.create_chat_completion(messages=[{"role":"user","content":prompt}],
31
+ temperature=0.2, max_tokens=450)
32
+ partials.append(out["choices"][0]["message"]["content"])
33
+ merged = "\n\n---\n\n".join(partials)
34
+ final = llm.create_chat_completion(messages=[{"role":"user","content":
35
+ f"Una os resumos abaixo num único resumo coerente, estilo: {style}, tamanho: {length}. "
36
+ f"Conserve referências de páginas quando houver.\n\n{merged}"}],
37
+ temperature=0.2, max_tokens=700)
38
+ return final["choices"][0]["message"]["content"]
39
+