Created
November 17, 2024 02:11
-
-
Save immma/4209ba8fa0c31ac9033063169f01b983 to your computer and use it in GitHub Desktop.
summarize_file.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
txt_files = [f for f in os.listdir(directoryName) if f.endswith('.txt')] | |
for file in txt_files: | |
summary = summarize_text(directoryName + file) | |
print(summary) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import boto3 | |
import json | |
import os | |
import csv | |
# Initialize the Bedrock client | |
client = boto3.client('bedrock-runtime', region_name="us-east-1") | |
# Function to summarize text using AWS Bedrock | |
def summarize_text(text, model_id='anthropic.claude-v2'): | |
# Prepare the payload for the model | |
payload = { | |
"prompt": f"\n\nHuman: tolong buat summary dari dokumen ini {text}\n\nAssistant:", | |
"max_tokens_to_sample": 500, # Limit the size of the output | |
"temperature": 0.5 # Adjust randomness (between 0 and 1) | |
} | |
# Invoke the model using AWS Bedrock | |
response = client.invoke_model( | |
modelId=model_id, | |
contentType="application/json", | |
accept="application/json", | |
body=json.dumps(payload) | |
) | |
# Get the response from the model | |
result = json.loads(response['body'].read()) | |
# Extract and return the summarized text | |
summary = result.get('completion', "No summary available") | |
return summary | |
# Function to read text from a file | |
def read_text_from_file(file_path): | |
with open(file_path, 'r') as file: | |
return file.read() | |
myfile = 'myfile.txt' # or if your file is in folder, you can specify the folder as well 'folder/myfile.txt' | |
mytext = read_text_from_file(myfile) | |
summary = summarize_text(mytext) | |
print(summary) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Function to read text from a file | |
def read_text_from_file(file_path): | |
with open(file_path, 'r') as file: | |
return file.read() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import boto3 | |
import json | |
import os | |
import csv | |
# Initialize the Bedrock client | |
client = boto3.client('bedrock-runtime', region_name="us-east-1") | |
# Function to summarize text using AWS Bedrock | |
def summarize_text(text, model_id='anthropic.claude-v2'): | |
# Prepare the payload for the model | |
payload = { | |
"prompt": f"\n\nHuman: Please summarize this document {text}\n\nAssistant:", | |
"max_tokens_to_sample": 500, # Limit the size of the output | |
"temperature": 0.5 # Adjust randomness (between 0 and 1) | |
} | |
# Invoke the model using AWS Bedrock | |
response = client.invoke_model( | |
modelId=model_id, | |
contentType="application/json", | |
accept="application/json", | |
body=json.dumps(payload) | |
) | |
# Get the response from the model | |
result = json.loads(response['body'].read()) | |
# Extract and return the summarized text | |
summary = result.get('completion',"No summary available") | |
return summary |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment