-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsummarize.py
More file actions
48 lines (40 loc) · 1.5 KB
/
summarize.py
File metadata and controls
48 lines (40 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import openai
# Function to turn a txt file into lecture notes
def txt_to_lecture_notes(file_path, api_key):
# Load the content of the txt file
try:
with open(file_path, 'r') as file:
text_content = file.read()
except FileNotFoundError:
return "Error: File not found. Please check the file path."
# OpenAI API Key
openai.api_key = api_key
# Use OpenAI's API to summarize the text content into lecture notes
try:
response = openai.ChatCompletion.create(
model="gpt-4", # You can choose a different model if needed
messages=[
{"role": "system", "content": "You are an expert lecturer. Summarize the following text into concise, well-structured lecture notes."},
{"role": "user", "content": text_content}
],
temperature=0.5,
max_tokens=1500 # Adjust as needed depending on the length of the text
)
# Extract and return the response text
lecture_notes = response['choices'][0]['message']['content']
return lecture_notes
except Exception as e:
return f"Error: {str(e)}"
def get_api_key():
print("Enter your OpenAI API key: ")
key = input()
return key
def get_txt_file():
print("Enter name of txt file (without .txt extension: ")
file = input() + ".txt"
return file
# Example usage:
api_key = get_api_key()
file_path = get_txt_file()
notes = txt_to_lecture_notes(file_path, api_key)
print(notes)