Skip to content

Commit f510f3d

Browse files
committed
Update test_sum.py
1 parent fbcc863 commit f510f3d

File tree

1 file changed

+27
-33
lines changed

1 file changed

+27
-33
lines changed

Backend/test_sum.py

Lines changed: 27 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,14 @@
33
from transformers import pipeline
44
import shutil
55
import os
6+
import asyncio
67

78

8-
app = FastAPI()
9-
10-
# Define a router for the user-related endpoints
11-
router_users = APIRouter()
12-
13-
@router_users.get("/")
14-
def read_root():
15-
return {"Hello": "World"}
16-
17-
18-
19-
@app.post("/get-summary")
20-
async def summary(file: UploadFile = File(...)):
21-
22-
23-
# saving the file
24-
try:
25-
with open("dat.txt", "wb") as buffer:
26-
shutil.copyfileobj(file.file, buffer)
27-
finally:
28-
file.file.close()
29-
9+
def summary(text):
3010

3111
# Load the summarization pipeline
3212
summarizer = pipeline("summarization")
3313

34-
# Read the contents of the text file
35-
with open("dat.txt", "r", encoding='utf-8') as file:
36-
text = file.read()
37-
38-
3914
# Split the text into smaller chunks
4015
max_tokens_per_chunk = 1024 # Initial value
4116
max_words_in_summary = 2000000
@@ -70,18 +45,37 @@ async def summary(file: UploadFile = File(...)):
7045
# Combine the summaries into a single summary
7146
combined_summary = " ".join(summaries)
7247

73-
# Print the combined summary
48+
# Print and return the combined summary
7449
print("Combined Summary:")
7550
print(combined_summary)
7651
print("Deleting the saved file.......")
7752
os.remove("dat.txt")
7853
print("deleted....")
7954
return{"summary" : combined_summary,"exceptions" : exceptions}
8055

81-
# Mount the routers on the app
82-
app.include_router(router_users)
56+
57+
async def gen_summary(file):
58+
59+
try:
60+
with open("dat.txt", "wb") as buffer: # saving file
61+
shutil.copyfileobj(file.file, buffer)
62+
finally:
63+
file.file.close()
64+
65+
with open("dat.txt", "r", encoding='utf-8') as file:
66+
text = file.read() # reading file
67+
68+
loop = asyncio.get_running_loop() # making it to run in background
69+
return await loop.run_in_executor(None, summary, text)
70+
71+
72+
router_summariser = APIRouter()
73+
74+
75+
@router_summariser.post("/get-summary")
76+
async def get_summary(file: UploadFile = File(...)):
77+
data = await gen_summary(file)
78+
79+
return data
8380

8481

85-
if __name__ == "__main__":
86-
import uvicorn
87-
uvicorn.run(app, host="0.0.0.0", port=8000)

0 commit comments

Comments
 (0)