|
3 | 3 | from transformers import pipeline
|
4 | 4 | import shutil
|
5 | 5 | import os
|
| 6 | +import asyncio |
6 | 7 |
|
7 | 8 |
|
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): |
30 | 10 |
|
31 | 11 | # Load the summarization pipeline
|
32 | 12 | summarizer = pipeline("summarization")
|
33 | 13 |
|
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 |
| - |
39 | 14 | # Split the text into smaller chunks
|
40 | 15 | max_tokens_per_chunk = 1024 # Initial value
|
41 | 16 | max_words_in_summary = 2000000
|
@@ -70,18 +45,37 @@ async def summary(file: UploadFile = File(...)):
|
70 | 45 | # Combine the summaries into a single summary
|
71 | 46 | combined_summary = " ".join(summaries)
|
72 | 47 |
|
73 |
| - # Print the combined summary |
| 48 | + # Print and return the combined summary |
74 | 49 | print("Combined Summary:")
|
75 | 50 | print(combined_summary)
|
76 | 51 | print("Deleting the saved file.......")
|
77 | 52 | os.remove("dat.txt")
|
78 | 53 | print("deleted....")
|
79 | 54 | return{"summary" : combined_summary,"exceptions" : exceptions}
|
80 | 55 |
|
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 |
83 | 80 |
|
84 | 81 |
|
85 |
| -if __name__ == "__main__": |
86 |
| - import uvicorn |
87 |
| - uvicorn.run(app, host="0.0.0.0", port=8000) |
0 commit comments