Skip to content

Commit 0d218c0

Browse files
committed
Update summariser.py
1 parent 48972c3 commit 0d218c0

File tree

1 file changed

+17
-14
lines changed

1 file changed

+17
-14
lines changed

Backend/summariser.py

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,46 +5,42 @@
55
import os
66
import asyncio
77

8+
progress = None # just for tracking progress
89

910
def summary(text):
10-
1111
# Load the summarization pipeline
1212
summarizer = pipeline("summarization")
13-
1413
# Split the text into smaller chunks
1514
max_tokens_per_chunk = 1024 # Initial value
1615
max_words_in_summary = 2000000
17-
1816
# Calculate the maximum number of chunks needed
1917
max_num_chunks = (max_words_in_summary // max_tokens_per_chunk) + 1
20-
2118
# Split the text into chunks
2219
chunks = [text[i:i + max_tokens_per_chunk] for i in range(0, len(text), max_tokens_per_chunk)]
2320
# for the exceptions
2421
exceptions = "NULL"
25-
22+
global progress
23+
progress = 0
2624
# Generate summaries for each chunk
2725
summaries = []
2826
len_chunk=len(chunks)
2927
print("Note have been divided into chunks:"+str(len_chunk))
3028
for i, chunk in enumerate(chunks):
3129
# Reduce the chunk size dynamically if it exceeds the maximum sequence length
3230
while len(chunk) > max_tokens_per_chunk:
33-
max_tokens_per_chunk -= 50
34-
31+
max_tokens_per_chunk -= 50
3532
try:
3633
summary = summarizer(chunk, max_length=200, min_length=100, do_sample=False)
3734
summaries.append(summary[0]['summary_text']+"\n\n")
3835
print(summary[0]['summary_text'])
3936
print("\n \n STATUS:"+str(i+1)+"/"+str(len_chunk))
40-
print("\n \n COMPLETED:"+str((i+1)/len_chunk*100)+"%")
37+
progress = (i+1)/len_chunk*100
38+
print("\n \n COMPLETED:"+str(progress)+"%")
4139
except Exception as e:
4240
print(f"An error occurred while summarizing chunk {i}: {str(e)}")
4341
exceptions = "\n".join(f"An error occurred while summarizing chunk {i}: {str(e)}")
44-
4542
# Combine the summaries into a single summary
4643
combined_summary = " ".join(summaries)
47-
4844
# Print and return the combined summary
4945
print("Combined Summary:")
5046
print(combined_summary)
@@ -55,7 +51,6 @@ def summary(text):
5551

5652

5753
async def gen_summary(file):
58-
5954
try:
6055
with open("dat.txt", "wb") as buffer: # saving file
6156
shutil.copyfileobj(file.file, buffer)
@@ -75,8 +70,16 @@ async def gen_summary(file):
7570
@router_summariser.post("/get-summary")
7671
async def get_summary(file: UploadFile = File(...)):
7772
data = await gen_summary(file)
78-
7973
return data
8074

81-
82-
75+
@router_summariser.get("/summary-gen-progress") # route to track progress of summarization
76+
def get_summary_progress():
77+
global progress
78+
if progress is None :
79+
return {"status" : "No summarisation process in progress" }
80+
elif progress == 100 :
81+
return {"status" : "Completed" , "value" : progress}
82+
elif progress in range(0,101) :
83+
return {"status" : progress}
84+
else :
85+
return {"invalid data detected"}

0 commit comments

Comments
 (0)