|
4 | 4 | import shutil
|
5 | 5 | import os
|
6 | 6 | import asyncio
|
| 7 | +import boto3 |
| 8 | +from botocore.exceptions import NoCredentialsError |
| 9 | + |
| 10 | + |
| 11 | +s3_access_key = "AKIAZTHHIOR4CN6UXO6N" |
| 12 | +s3_secret_access_key = "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" |
| 13 | +s3_bucket_name = "learnmateai" |
| 14 | + |
| 15 | +def save_file_to_s3(filename, content): |
| 16 | + try: |
| 17 | + # Connect to Amazon S3 |
| 18 | + s3 = boto3.client( |
| 19 | + "s3", |
| 20 | + aws_access_key_id=s3_access_key, |
| 21 | + aws_secret_access_key=s3_secret_access_key |
| 22 | + ) |
| 23 | + |
| 24 | + # Set the desired S3 key |
| 25 | + s3_key = f"Generated_Files/Summarised_Notes/summarised_{filename}" |
| 26 | + |
| 27 | + # Save the file content to Amazon S3 |
| 28 | + s3.put_object(Body=content.encode('utf-8'), Bucket=s3_bucket_name, Key=s3_key) |
| 29 | + |
| 30 | + return {"message": "File uploaded successfully"} |
| 31 | + except NoCredentialsError: |
| 32 | + return {"message": "AWS credentials not found"} |
| 33 | + |
| 34 | + |
| 35 | + |
| 36 | + |
7 | 37 |
|
8 | 38 | progress = None # just for tracking progress
|
9 | 39 |
|
10 |
| -def summary(text): |
| 40 | +def summary(text,filename): |
11 | 41 | # Load the summarization pipeline
|
12 | 42 | summarizer = pipeline("summarization")
|
13 | 43 | # Split the text into smaller chunks
|
@@ -44,33 +74,44 @@ def summary(text):
|
44 | 74 | # Print and return the combined summary
|
45 | 75 | print("Combined Summary:")
|
46 | 76 | print(combined_summary)
|
47 |
| - print("Deleting the saved file.......") |
48 |
| - os.remove("dat.txt") |
49 |
| - print("deleted....") |
50 |
| - return{"summary" : combined_summary,"exceptions" : exceptions} |
| 77 | + with open(f'Local_Storage\Generated_Files\Summarised_Notes\summarised_{filename}', 'w', encoding='utf-8') as file: |
| 78 | + file.write(combined_summary) |
| 79 | + response = save_file_to_s3(filename, combined_summary) |
| 80 | + print(response) |
| 81 | + |
51 | 82 |
|
52 | 83 |
|
53 |
| -async def gen_summary(file): |
54 |
| - try: |
55 |
| - with open("dat.txt", "wb") as buffer: # saving file |
56 |
| - shutil.copyfileobj(file.file, buffer) |
57 |
| - finally: |
58 |
| - file.file.close() |
| 84 | +async def gen_summary(): |
| 85 | + # reading file |
| 86 | + folder_path = 'Local_Storage\\notes_txt' |
| 87 | + # Get the list of files in the folder |
| 88 | + file_list = os.listdir(folder_path) |
| 89 | + |
| 90 | + # Iterate over each file in the folder |
| 91 | + for file_name in file_list: |
| 92 | + # Construct the full file path |
| 93 | + file_path = os.path.join(folder_path, file_name) |
| 94 | + |
| 95 | + # Check if the path is a file (not a directory) |
| 96 | + if os.path.isfile(file_path): |
| 97 | + |
| 98 | + with open(file_path, 'r', encoding='utf-8') as file: |
| 99 | + text = file.read() |
| 100 | + |
| 101 | + loop = asyncio.get_running_loop() # making it to run in background |
| 102 | + await loop.run_in_executor(None, summary, text,file_name) |
59 | 103 |
|
60 |
| - with open("dat.txt", "r", encoding='utf-8') as file: |
61 |
| - text = file.read() # reading file |
62 | 104 |
|
63 |
| - loop = asyncio.get_running_loop() # making it to run in background |
64 |
| - return await loop.run_in_executor(None, summary, text) |
| 105 | + |
65 | 106 |
|
66 | 107 |
|
67 | 108 | router_summariser = APIRouter()
|
68 | 109 |
|
69 | 110 |
|
70 | 111 | @router_summariser.post("/get-summary")
|
71 |
| -async def get_summary(file: UploadFile = File(...)): |
72 |
| - data = await gen_summary(file) |
73 |
| - return data |
| 112 | +async def get_summary(): |
| 113 | + await gen_summary() |
| 114 | + return {"status" : 1} |
74 | 115 |
|
75 | 116 | @router_summariser.get("/summary-gen-progress") # route to track progress of summarization
|
76 | 117 | def get_summary_progress():
|
|
0 commit comments