You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Synchronously summarizes a chunk of text using the OpenAI API.
56
+
57
+
Args:
58
+
chunk (str): A chunk of text to summarize.
59
+
language (str): The language of the text.
60
+
61
+
Returns:
62
+
summary (str): The summarized text.
63
+
tokens_used (int): The number of tokens used for the OpenAI API call.
64
+
estimated_cost (float): The estimated cost of the OpenAI API call.
65
+
word_count (int): The word count of the summary.
66
+
"""
67
+
try:
68
+
# Define the prompt for summarization
69
+
system_role="You are a master of extracting pearls of knowledge from YouTube video transcripts. You grasp the very essence and distill it in a concise form for users. You always provide the response in the same language in which the transcript is provided. Your answers are always clear, concise and nicely formatted."
70
+
prompt= (
71
+
f"If I did not have time to read this YouTube video transcript, what are the most important things I absolutely must know. Enlighten me in no more than 200 words. "
72
+
f"Always provide your response in the same language as the transcript. In this case it might be in '{language}' language. Here is the transcript itself:\n\n{chunk}"
73
+
)
74
+
75
+
response=client.chat.completions.create(
76
+
model=model,
77
+
messages=[
78
+
{"role": "system", "content": system_role},
79
+
{"role": "user", "content": prompt},
80
+
],
81
+
max_tokens=max_tokens,
82
+
temperature=0.5,
83
+
stream=False
84
+
)
85
+
86
+
# Access the response attributes correctly
87
+
summary=response.choices[0].message.content
88
+
tokens_used=response.usage.total_tokens
89
+
estimated_cost= (tokens_used/1000) *0.002# Adjust based on DeepSeek pricing
system_role="You are a master of extracting pearls of knowledge from YouTube video transcripts. You grasp the very essence and distill it in a concise form for users. You always provide the response in the same language in which the transcript is provided. Your answers are always clear, concise and nicely formatted."
89
-
prompt= (
90
-
f"If I did not have time to read this YouTube video transcript, what are the most important things I absolutely must know. Enlighten me in no more than 200 words. "
91
-
f"Always provide your response in the same language as the transcript. In this case it might be in '{language}' language. Here is the transcript itself:\n\n{chunk}"
92
-
)
93
-
94
-
response=client.chat.completions.create(
95
-
model=model,
96
-
messages=[
97
-
{"role": "system", "content": system_role},
98
-
{"role": "user", "content": prompt},
99
-
],
100
-
max_tokens=max_tokens,
101
-
temperature=0.5,
102
-
stream=False
103
-
)
104
-
105
-
# if model_to_use == 1:
106
-
# # Call the OpenAI API
107
-
# response = openai.ChatCompletion.create(
108
-
# model="gpt-3.5-turbo",
109
-
# messages=[
110
-
# {"role": "system", "content": system_role},
111
-
# {"role": "user", "content": prompt}
112
-
# ],
113
-
# max_tokens=500, # Increased token limit to ensure the summary is complete
114
-
# temperature=0.5
115
-
# )
116
-
# elif model_to_use == 2:
117
-
# # DeepSeek API
118
-
# response = client.chat.completions.create(
119
-
# model="deepseek-chat",
120
-
# messages=[
121
-
# {"role": "system", "content": system_role},
122
-
# {"role": "user", "content": prompt},
123
-
# ],
124
-
# max_tokens=1024,
125
-
# temperature=0.5,
126
-
# stream=False
127
-
# )
128
-
129
-
# else:
130
-
# logger.error("Invalid model selection. Please select 1 for OpenAI or 2 for DeepSeek.")
logger.info(f"Summarization completed for all chunks.")
164
+
logger.info(f"Summarization completed for all chunks.\nTotal word count: {total_word_count}.\nTotal tokens used: {total_tokens_used}.\nEstimated cost: ${total_estimated_cost:.2f}")
# f"If I did not have time to read this YouTube video transcript, what are the most important things I absolutely must know. Enlighten me in no more than 200 words. "
161
-
# f"Always provide your response in the same language as the transcript. In this case it might be in '{language}' language. Here is the transcript itself:\n\n{text}"
162
-
# )
163
-
164
-
# # Call the OpenAI API
165
-
# response = openai.ChatCompletion.create(
166
-
# model="gpt-3.5-turbo",
167
-
# messages=[
168
-
# {"role": "system", "content": "You are a master of extracting pearls of knowledge from YouTube video transcripts. You grasp the very essense and distill it in a consice form for users. You always provide the response in the same language in which the transcript is provided. Your answers are always clear, consise and nicely formatted."},
169
-
# {"role": "user", "content": prompt}
170
-
# ],
171
-
# max_tokens=500, # Increased token limit to ensure the summary is complete
0 commit comments