anyonehomep1mane commited on
Commit
c01193a
·
1 Parent(s): ed86e9f

Dockerfile changes

Browse files
Files changed (1) hide show
  1. services/summarizer.py +37 -1
services/summarizer.py CHANGED
@@ -1 +1,37 @@
1
- "print('summarizer loaded')"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from utils.file_loader import extract_text
2
+ from prompts.summarize_prompt import SUMMARY_PROMPT
3
+ from llm.hf_llm import get_chat_model
4
+
5
+ chat_model = get_chat_model()
6
+ summarize_chain = SUMMARY_PROMPT | chat_model
7
+
8
+ MAX_CHARS = 18_000
9
+
10
+ def summarize_document(file):
11
+ if not file:
12
+ return "Please upload a document."
13
+
14
+ text = extract_text(file)
15
+
16
+ if text.startswith("❌"):
17
+ return text
18
+
19
+ if len(text.strip()) < 80:
20
+ return "Not enough meaningful text extracted."
21
+
22
+ warning = ""
23
+ if len(text) > MAX_CHARS:
24
+ text = text[:MAX_CHARS]
25
+ warning = "⚠️ Document truncated for processing.\n\n"
26
+
27
+ try:
28
+ response = summarize_chain.invoke({"text": text})
29
+ return warning + response.content.strip()
30
+
31
+ except Exception as e:
32
+ msg = str(e).lower()
33
+ if "token" in msg:
34
+ return "❌ Invalid or missing Hugging Face token."
35
+ if "rate" in msg:
36
+ return "❌ Rate limit exceeded. Try later."
37
+ return f"❌ Error: {str(e)}"