33import deepseek
44import logging
55from translate import translate_text # Import the translation function
6+ from translate import split_text
7+ from model_params import get_model_params
68from config import OPENAI_API_KEY
79from config import DEEPSEEK_API_KEY
810from config import MODEL_TO_USE # Import the model selection
911import tiktoken
10- from translate import split_text
1112import math
1213import asyncio
1314
1617
1718logger = logging .getLogger (__name__ )
1819
19- if MODEL_TO_USE :
20- if "gpt" in MODEL_TO_USE .lower ():
21- model_to_use = 1 # OpenAI model
22- elif "deepseek" in MODEL_TO_USE .lower ():
23- model_to_use = 2
24- else :
25- logger .error ("Invalid model selection in config. Please select 'gpt' for OpenAI or 'deepseek' for DeepSeek." )
26- logger .warning ("Falling back to DeepSeek model as default." )
27- model_to_use = 2 # 1 for OpenAI, 2 for DeepSeek
28- raise ValueError ("Invalid model selection in config. Please select 'gpt' for OpenAI or 'deepseek' for DeepSeek." )
29-
30- if model_to_use == 1 :
31- tokens_per_chunk = 100000
32- max_chunks_allowed = 4
33- max_tokens = 1024
34- model = "gpt-4o-mini"
35- client = openai .OpenAI (api_key = OPENAI_API_KEY , base_url = "https://api.openai.com/v1" )
36- logger .info (f"Using OpenAI { model } model for summarization." )
37- elif model_to_use == 2 :
38- tokens_per_chunk = 64000
39- max_chunks_allowed = 3
40- max_tokens = 1024
41- model = "deepseek-chat"
42- # for DeepSeek backward compatibility, you can still use `https://api.deepseek.com/v1` as `base_url`.
43- client = openai .OpenAI (api_key = DEEPSEEK_API_KEY , base_url = "https://api.deepseek.com" )
44- logger .info (f"Using DeepSeek { model } for summarization." )
45- else :
46- logger .error ("Invalid model selection. Please select 1 for OpenAI or 2 for DeepSeek." )
20+
21+ # if MODEL_TO_USE:
22+ # if "gpt" in MODEL_TO_USE.lower():
23+ # model_to_use = 1 # OpenAI model
24+ # elif "deepseek" in MODEL_TO_USE.lower():
25+ # model_to_use = 2
26+ # else:
27+ # logger.error("Invalid model selection in config. Please select 'gpt' for OpenAI or 'deepseek' for DeepSeek.")
28+ # logger.warning("Falling back to DeepSeek model as default.")
29+ # model_to_use = 2 # 1 for OpenAI, 2 for DeepSeek
30+ # raise ValueError("Invalid model selection in config. Please select 'gpt' for OpenAI or 'deepseek' for DeepSeek.")
31+
32+ # if model_to_use == 1:
33+ # tokens_per_chunk = 100000
34+ # max_chunks_allowed = 4
35+ # max_tokens = 1024
36+ # model = "gpt-4o-mini"
37+ # client = openai.OpenAI(api_key=OPENAI_API_KEY, base_url="https://api.openai.com/v1")
38+ # logger.info(f"Using OpenAI {model} model for summarization.")
39+ # elif model_to_use == 2:
40+ # tokens_per_chunk = 64000
41+ # max_chunks_allowed = 5
42+ # max_tokens = 1024
43+ # model = "deepseek-chat"
44+ # # for DeepSeek backward compatibility, you can still use `https://api.deepseek.com/v1` as `base_url`.
45+ # client = openai.OpenAI(api_key=DEEPSEEK_API_KEY, base_url="https://api.deepseek.com")
46+ # logger.info(f"Using DeepSeek {model} for summarization.")
47+ # else:
48+ # logger.error("Invalid model selection. Please select 1 for OpenAI or 2 for DeepSeek.")
4749
4850
4951async def summarize_chunk (chunk , language ):
@@ -64,21 +66,27 @@ def _summarize_sync(chunk, language):
6466 estimated_cost (float): The estimated cost of the OpenAI API call.
6567 word_count (int): The word count of the summary.
6668 """
69+ model_params = get_model_params (MODEL_TO_USE )
70+
6771 try :
6872 # Define the prompt for summarization
6973 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."
7074 prompt = (
7175 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. "
7276 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 } "
7377 )
78+ # prompt = (
79+ # 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. "
80+ # f"Provide your response strictly in the following language: '{language}'. Here is the transcript itself:\n\n{chunk}"
81+ # )
7482
75- response = client .chat .completions .create (
76- model = model ,
83+ response = model_params [ " client" ] .chat .completions .create (
84+ model = model_params [ " model" ] ,
7785 messages = [
7886 {"role" : "system" , "content" : system_role },
7987 {"role" : "user" , "content" : prompt },
8088 ],
81- max_tokens = max_tokens ,
89+ max_tokens = model_params [ " max_tokens" ] ,
8290 temperature = 0.5 ,
8391 stream = False
8492 )
@@ -113,6 +121,9 @@ async def summarize_text(text, language, num_key_points=3):
113121 word_count (int): The word count of the summary.
114122 """
115123
124+ tokens_per_chunk , max_chunks_allowed , max_tokens , model , client = get_model_params (MODEL_TO_USE ).values ()
125+ logger .info (f"Tokens per chunk: { tokens_per_chunk } of type { type (tokens_per_chunk )} " )
126+
116127 try :
117128 # Load the encoding for the model you're using (e.g., gpt-3.5-turbo)
118129 encoding = tiktoken .encoding_for_model ("gpt-3.5-turbo" )
@@ -125,7 +136,7 @@ async def summarize_text(text, language, num_key_points=3):
125136 raise
126137
127138 if token_count :
128- number_of_chunks = token_count / tokens_per_chunk # 15,000 tokens per chunk for GPT-3.5-turbo
139+ number_of_chunks = token_count / int ( tokens_per_chunk ) # 15,000 tokens per chunk for GPT-3.5-turbo
129140 logger .info (f"Number of chunks for summarization of the transcript: { number_of_chunks } " )
130141
131142 if number_of_chunks > max_chunks_allowed :
@@ -161,17 +172,17 @@ async def summarize_text(text, language, num_key_points=3):
161172 logger .error (f"Summarization failed: { e } " )
162173 raise
163174
164- logger .info (f"Summarization completed for all chunks.\n Total word count: { total_word_count } .\n Total tokens used: { total_tokens_used } .\n Estimated cost: ${ total_estimated_cost :.2f} " )
175+ logger .info (f"Summarization completed for all chunks. Total word count: { total_word_count } . Total tokens used: { total_tokens_used } . Estimated cost: ${ total_estimated_cost :.2f} " )
165176
166177 return combined_summary .strip (), total_tokens_used , total_estimated_cost , total_word_count
167178
168179
169- async def translate_summary_async (summary , src_lang , dest_lang ):
170- loop = asyncio .get_event_loop ()
171- return await loop .run_in_executor (None , translate_summary , summary , src_lang , dest_lang )
180+ # async def translate_summary_async(summary, src_lang, dest_lang):
181+ # loop = asyncio.get_event_loop()
182+ # return await loop.run_in_executor(None, translate_summary, summary, src_lang, dest_lang)
172183
173184# Translate the summary into the target language
174- def translate_summary (summary , src_lang , dest_lang ):
185+ async def translate_summary (summary , src_lang , dest_lang ):
175186 """
176187 Translates the summary into the target language.
177188
@@ -183,8 +194,10 @@ def translate_summary(summary, src_lang, dest_lang):
183194 Returns:
184195 translated_summary (str): The translated summary.
185196 """
197+ model_params = get_model_params (MODEL_TO_USE )
198+
186199 try :
187- translated_summary = translate_text (summary , src_lang = src_lang , dest_lang = dest_lang )
200+ translated_summary , total_tokens_used , total_estimated_cost , total_word_count = await translate_text (summary , src_lang = src_lang , dest_lang = dest_lang , model_params = model_params )
188201 if not translated_summary :
189202 raise Exception ("Translation failed." )
190203 return translated_summary
@@ -212,19 +225,21 @@ async def handle_summarization_request(text, original_language, target_language,
212225
213226 logger .info (f"Summarization is handled for original language '{ original_language } ' and target '{ target_language } '" )
214227
228+ model_params = get_model_params (MODEL_TO_USE )
229+
215230 try :
216- # Summarize the original transcript
231+
217232 summary , tokens_used , estimated_cost , word_count = await summarize_text (text , original_language , num_key_points )
218-
219233 if target_language == 'orig' :
220234 target_language = original_language
235+
221236 logger .info (f"Summary generated in { target_language } language. Translation skipped." )
222237
223238 # Translate the summary if the target language is not the original language
224239 if target_language != original_language :
225- summary = await translate_summary_async (summary , src_lang = original_language , dest_lang = target_language )
240+ summary = await translate_summary (summary , src_lang = original_language , dest_lang = target_language )
226241
227- return summary , tokens_used , estimated_cost , word_count , model
242+ return summary , tokens_used , estimated_cost , word_count , model_params [ ' model' ]
228243 except Exception as e :
229244 logger .error (f"Failed to handle summarization request: { e } " )
230245 raise
0 commit comments