oceansweep commited on
Commit
086d7f1
1 Parent(s): 6260f65

Upload Local_Summarization_Lib.py

Browse files
App_Function_Libraries/Local_Summarization_Lib.py CHANGED
@@ -21,6 +21,8 @@
21
  import json
22
  import logging
23
  import os
 
 
24
  import requests
25
  # Import 3rd-party Libraries
26
  from openai import OpenAI
@@ -38,7 +40,7 @@ logger = logging.getLogger()
38
  openai_api_key = "Fake_key"
39
  client = OpenAI(api_key=openai_api_key)
40
 
41
- def summarize_with_local_llm(input_data, custom_prompt_arg):
42
  try:
43
  if isinstance(input_data, str) and os.path.isfile(input_data):
44
  logging.debug("Local LLM: Loading json data for summarization")
@@ -65,6 +67,9 @@ def summarize_with_local_llm(input_data, custom_prompt_arg):
65
  else:
66
  raise ValueError("Invalid input data format")
67
 
 
 
 
68
  headers = {
69
  'Content-Type': 'application/json'
70
  }
@@ -75,7 +80,7 @@ def summarize_with_local_llm(input_data, custom_prompt_arg):
75
  "messages": [
76
  {
77
  "role": "system",
78
- "content": "You are a professional summarizer."
79
  },
80
  {
81
  "role": "user",
@@ -106,19 +111,25 @@ def summarize_with_local_llm(input_data, custom_prompt_arg):
106
  print("Error occurred while processing summary with Local LLM:", str(e))
107
  return "Local LLM: Error occurred while processing summary"
108
 
109
- def summarize_with_llama(input_data, custom_prompt, api_url="http://127.0.0.1:8080/completion", api_key=None):
110
- loaded_config_data = load_and_log_configs()
111
  try:
112
- # API key validation
113
- if api_key is None:
114
- logging.info("llama.cpp: API key not provided as parameter")
115
- logging.info("llama.cpp: Attempting to use API key from config file")
116
- api_key = loaded_config_data['api_keys']['llama']
117
-
118
- if api_key is None or api_key.strip() == "":
119
- logging.info("llama.cpp: API key not found or is empty")
120
-
121
- logging.debug(f"llama.cpp: Using API Key: {api_key[:5]}...{api_key[-5:]}")
 
 
 
 
 
 
 
122
 
123
  # Load transcript
124
  logging.debug("llama.cpp: Loading JSON data")
@@ -154,11 +165,20 @@ def summarize_with_llama(input_data, custom_prompt, api_url="http://127.0.0.1:80
154
  if len(api_key) > 5:
155
  headers['Authorization'] = f'Bearer {api_key}'
156
 
157
- llama_prompt = f"{text} \n\n\n\n{custom_prompt}"
 
 
158
  logging.debug("llama: Prompt being sent is {llama_prompt}")
 
 
159
 
160
  data = {
161
- "prompt": llama_prompt
 
 
 
 
 
162
  }
163
 
164
  logging.debug("llama: Submitting request to API endpoint")
@@ -184,17 +204,28 @@ def summarize_with_llama(input_data, custom_prompt, api_url="http://127.0.0.1:80
184
 
185
 
186
  # https://lite.koboldai.net/koboldcpp_api#/api%2Fv1/post_api_v1_generate
187
- def summarize_with_kobold(input_data, api_key, custom_prompt_input, kobold_api_IP="http://127.0.0.1:5001/api/v1/generate"):
188
- loaded_config_data = load_and_log_configs()
189
  try:
190
- # API key validation
191
- if api_key is None:
192
- logging.info("Kobold.cpp: API key not provided as parameter")
193
- logging.info("Kobold.cpp: Attempting to use API key from config file")
194
- api_key = loaded_config_data['api_keys']['kobold']
 
 
 
 
 
 
 
 
 
 
 
 
195
 
196
- if api_key is None or api_key.strip() == "":
197
- logging.info("Kobold.cpp: API key not found or is empty")
198
 
199
  if isinstance(input_data, str) and os.path.isfile(input_data):
200
  logging.debug("Kobold.cpp: Loading json data for summarization")
@@ -226,7 +257,7 @@ def summarize_with_kobold(input_data, api_key, custom_prompt_input, kobold_api_I
226
  'content-type': 'application/json',
227
  }
228
 
229
- kobold_prompt = f"{text} \n\n\n\n{custom_prompt_input}"
230
  logging.debug("kobold: Prompt being sent is {kobold_prompt}")
231
 
232
  # FIXME
@@ -234,45 +265,69 @@ def summarize_with_kobold(input_data, api_key, custom_prompt_input, kobold_api_I
234
  data = {
235
  "max_context_length": 8096,
236
  "max_length": 4096,
237
- "prompt": f"{text}\n\n\n\n{custom_prompt_input}"
 
 
 
 
238
  }
239
 
240
  logging.debug("kobold: Submitting request to API endpoint")
241
  print("kobold: Submitting request to API endpoint")
242
- response = requests.post(kobold_api_IP, headers=headers, json=data)
243
- response_data = response.json()
244
- logging.debug("kobold: API Response Data: %s", response_data)
245
-
246
- if response.status_code == 200:
247
- if 'results' in response_data and len(response_data['results']) > 0:
248
- summary = response_data['results'][0]['text'].strip()
249
- logging.debug("kobold: Summarization successful")
250
- print("Summarization successful.")
251
- return summary
 
 
 
 
 
 
 
 
 
 
252
  else:
253
- logging.error("Expected data not found in API response.")
254
- return "Expected data not found in API response."
255
- else:
256
- logging.error(f"kobold: API request failed with status code {response.status_code}: {response.text}")
257
- return f"kobold: API request failed: {response.text}"
258
-
259
  except Exception as e:
260
  logging.error("kobold: Error in processing: %s", str(e))
261
  return f"kobold: Error occurred while processing summary with kobold: {str(e)}"
262
 
263
 
264
  # https://github.com/oobabooga/text-generation-webui/wiki/12-%E2%80%90-OpenAI-API
265
- def summarize_with_oobabooga(input_data, api_key, custom_prompt, api_url="http://127.0.0.1:5000/v1/chat/completions"):
266
- loaded_config_data = load_and_log_configs()
267
  try:
268
- # API key validation
269
- if api_key is None:
270
- logging.info("ooba: API key not provided as parameter")
271
- logging.info("ooba: Attempting to use API key from config file")
272
- api_key = loaded_config_data['api_keys']['ooba']
 
 
 
 
 
 
 
 
 
 
 
 
273
 
274
- if api_key is None or api_key.strip() == "":
275
- logging.info("ooba: API key not found or is empty")
276
 
277
  if isinstance(input_data, str) and os.path.isfile(input_data):
278
  logging.debug("Oobabooga: Loading json data for summarization")
@@ -310,10 +365,14 @@ def summarize_with_oobabooga(input_data, api_key, custom_prompt, api_url="http:/
310
  ooba_prompt = f"{text}" + f"\n\n\n\n{custom_prompt}"
311
  logging.debug("ooba: Prompt being sent is {ooba_prompt}")
312
 
 
 
 
313
  data = {
314
  "mode": "chat",
315
  "character": "Example",
316
- "messages": [{"role": "user", "content": ooba_prompt}]
 
317
  }
318
 
319
  logging.debug("ooba: Submitting request to API endpoint")
@@ -336,117 +395,310 @@ def summarize_with_oobabooga(input_data, api_key, custom_prompt, api_url="http:/
336
  return f"ooba: Error occurred while processing summary with oobabooga: {str(e)}"
337
 
338
 
339
- # FIXME - Install is more trouble than care to deal with right now.
340
- def summarize_with_tabbyapi(input_data, custom_prompt_input, api_key=None, api_IP="http://127.0.0.1:5000/v1/chat/completions"):
341
- loaded_config_data = load_and_log_configs()
342
- model = loaded_config_data['models']['tabby']
343
- # API key validation
344
- if api_key is None:
345
- logging.info("tabby: API key not provided as parameter")
346
- logging.info("tabby: Attempting to use API key from config file")
347
- api_key = loaded_config_data['api_keys']['tabby']
348
-
349
- if api_key is None or api_key.strip() == "":
350
- logging.info("tabby: API key not found or is empty")
351
-
352
- if isinstance(input_data, str) and os.path.isfile(input_data):
353
- logging.debug("tabby: Loading json data for summarization")
354
- with open(input_data, 'r') as file:
355
- data = json.load(file)
356
- else:
357
- logging.debug("tabby: Using provided string data for summarization")
358
- data = input_data
359
-
360
- logging.debug(f"tabby: Loaded data: {data}")
361
- logging.debug(f"tabby: Type of data: {type(data)}")
362
-
363
- if isinstance(data, dict) and 'summary' in data:
364
- # If the loaded data is a dictionary and already contains a summary, return it
365
- logging.debug("tabby: Summary already exists in the loaded data")
366
- return data['summary']
367
-
368
- # If the loaded data is a list of segment dictionaries or a string, proceed with summarization
369
- if isinstance(data, list):
370
- segments = data
371
- text = extract_text_from_segments(segments)
372
- elif isinstance(data, str):
373
- text = data
374
- else:
375
- raise ValueError("Invalid input data format")
376
-
377
- headers = {
378
- 'Authorization': f'Bearer {api_key}',
379
- 'Content-Type': 'application/json'
380
- }
381
- data2 = {
382
- 'text': text,
383
- 'model': 'tabby' # Specify the model if needed
384
- }
385
- tabby_api_ip = loaded_config_data['local_apis']['tabby']['ip']
386
  try:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
387
  response = requests.post(tabby_api_ip, headers=headers, json=data2)
388
- response.raise_for_status()
389
- summary = response.json().get('summary', '')
390
- return summary
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
391
  except requests.exceptions.RequestException as e:
392
- logger.error(f"Error summarizing with TabbyAPI: {e}")
393
- return "Error summarizing with TabbyAPI."
394
-
395
-
396
- # FIXME - https://docs.vllm.ai/en/latest/getting_started/quickstart.html .... Great docs.
397
- def summarize_with_vllm(input_data, custom_prompt_input, api_key=None, vllm_api_url="http://127.0.0.1:8000/v1/chat/completions"):
398
- loaded_config_data = load_and_log_configs()
399
- llm_model = loaded_config_data['models']['vllm']
400
- # API key validation
401
- if api_key is None:
402
- logging.info("vLLM: API key not provided as parameter")
403
- logging.info("vLLM: Attempting to use API key from config file")
404
- api_key = loaded_config_data['api_keys']['llama']
405
-
406
- if api_key is None or api_key.strip() == "":
407
- logging.info("vLLM: API key not found or is empty")
408
- vllm_client = OpenAI(
409
- base_url=vllm_api_url,
410
- api_key=custom_prompt_input
411
- )
412
-
413
- if isinstance(input_data, str) and os.path.isfile(input_data):
414
- logging.debug("vLLM: Loading json data for summarization")
415
- with open(input_data, 'r') as file:
416
- data = json.load(file)
417
- else:
418
- logging.debug("vLLM: Using provided string data for summarization")
419
- data = input_data
420
-
421
- logging.debug(f"vLLM: Loaded data: {data}")
422
- logging.debug(f"vLLM: Type of data: {type(data)}")
423
-
424
- if isinstance(data, dict) and 'summary' in data:
425
- # If the loaded data is a dictionary and already contains a summary, return it
426
- logging.debug("vLLM: Summary already exists in the loaded data")
427
- return data['summary']
428
-
429
- # If the loaded data is a list of segment dictionaries or a string, proceed with summarization
430
- if isinstance(data, list):
431
- segments = data
432
- text = extract_text_from_segments(segments)
433
- elif isinstance(data, str):
434
- text = data
435
- else:
436
- raise ValueError("Invalid input data format")
437
-
438
-
439
- custom_prompt = custom_prompt_input
440
-
441
- completion = client.chat.completions.create(
442
- model=llm_model,
443
- messages=[
444
- {"role": "system", "content": "You are a professional summarizer."},
445
- {"role": "user", "content": f"{text} \n\n\n\n{custom_prompt}"}
446
- ]
447
- )
448
- vllm_summary = completion.choices[0].message.content
449
- return vllm_summary
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
450
 
451
 
452
  def save_summary_to_file(summary, file_path):
 
21
  import json
22
  import logging
23
  import os
24
+ from typing import Union
25
+
26
  import requests
27
  # Import 3rd-party Libraries
28
  from openai import OpenAI
 
40
  openai_api_key = "Fake_key"
41
  client = OpenAI(api_key=openai_api_key)
42
 
43
+ def summarize_with_local_llm(input_data, custom_prompt_arg, temp, system_message=None):
44
  try:
45
  if isinstance(input_data, str) and os.path.isfile(input_data):
46
  logging.debug("Local LLM: Loading json data for summarization")
 
67
  else:
68
  raise ValueError("Invalid input data format")
69
 
70
+ if system_message is None:
71
+ system_message = "You are a helpful AI assistant."
72
+
73
  headers = {
74
  'Content-Type': 'application/json'
75
  }
 
80
  "messages": [
81
  {
82
  "role": "system",
83
+ "content": system_message
84
  },
85
  {
86
  "role": "user",
 
111
  print("Error occurred while processing summary with Local LLM:", str(e))
112
  return "Local LLM: Error occurred while processing summary"
113
 
114
+ def summarize_with_llama(input_data, custom_prompt, api_url="http://127.0.0.1:8080/completion", api_key=None, temp=None, system_message=None):
 
115
  try:
116
+ logging.debug("Llama.cpp: Loading and validating configurations")
117
+ loaded_config_data = load_and_log_configs()
118
+ if loaded_config_data is None:
119
+ logging.error("Failed to load configuration data")
120
+ llama_api_key = None
121
+ else:
122
+ # Prioritize the API key passed as a parameter
123
+ if api_key and api_key.strip():
124
+ llama_api_key = api_key
125
+ logging.info("Llama.cpp: Using API key provided as parameter")
126
+ else:
127
+ # If no parameter is provided, use the key from the config
128
+ llama_api_key = loaded_config_data['api_keys'].get('llama')
129
+ if llama_api_key:
130
+ logging.info("Llama.cpp: Using API key from config file")
131
+ else:
132
+ logging.warning("Llama.cpp: No API key found in config file")
133
 
134
  # Load transcript
135
  logging.debug("llama.cpp: Loading JSON data")
 
165
  if len(api_key) > 5:
166
  headers['Authorization'] = f'Bearer {api_key}'
167
 
168
+ llama_prompt = f"{custom_prompt} \n\n\n\n{text}"
169
+ if system_message == None:
170
+ system_message = "You are a helpful AI assistant."
171
  logging.debug("llama: Prompt being sent is {llama_prompt}")
172
+ if system_message is None:
173
+ system_message = "You are a helpful AI assistant."
174
 
175
  data = {
176
+ "messages": [
177
+ {"role": "system", "content": system_message},
178
+ {"role": "user", "content": llama_prompt}
179
+ ],
180
+ "max_tokens": 4096,
181
+ "temperature": temp
182
  }
183
 
184
  logging.debug("llama: Submitting request to API endpoint")
 
204
 
205
 
206
  # https://lite.koboldai.net/koboldcpp_api#/api%2Fv1/post_api_v1_generate
207
+ def summarize_with_kobold(input_data, api_key, custom_prompt_input, kobold_api_ip="http://127.0.0.1:5001/api/v1/generate", temp=None, system_message=None):
208
+ logging.debug("Kobold: Summarization process starting...")
209
  try:
210
+ logging.debug("Kobold: Loading and validating configurations")
211
+ loaded_config_data = load_and_log_configs()
212
+ if loaded_config_data is None:
213
+ logging.error("Failed to load configuration data")
214
+ kobold_api_key = None
215
+ else:
216
+ # Prioritize the API key passed as a parameter
217
+ if api_key and api_key.strip():
218
+ kobold_api_key = api_key
219
+ logging.info("Kobold: Using API key provided as parameter")
220
+ else:
221
+ # If no parameter is provided, use the key from the config
222
+ kobold_api_key = loaded_config_data['api_keys'].get('kobold')
223
+ if kobold_api_key:
224
+ logging.info("Kobold: Using API key from config file")
225
+ else:
226
+ logging.warning("Kobold: No API key found in config file")
227
 
228
+ logging.debug(f"Kobold: Using API Key: {kobold_api_key[:5]}...{kobold_api_key[-5:]}")
 
229
 
230
  if isinstance(input_data, str) and os.path.isfile(input_data):
231
  logging.debug("Kobold.cpp: Loading json data for summarization")
 
257
  'content-type': 'application/json',
258
  }
259
 
260
+ kobold_prompt = f"{custom_prompt_input}\n\n\n\n{text}"
261
  logging.debug("kobold: Prompt being sent is {kobold_prompt}")
262
 
263
  # FIXME
 
265
  data = {
266
  "max_context_length": 8096,
267
  "max_length": 4096,
268
+ "prompt": kobold_prompt,
269
+ "temperature": 0.7,
270
+ #"top_p": 0.9,
271
+ #"top_k": 100
272
+ #"rep_penalty": 1.0,
273
  }
274
 
275
  logging.debug("kobold: Submitting request to API endpoint")
276
  print("kobold: Submitting request to API endpoint")
277
+ kobold_api_ip = loaded_config_data['local_api_ip']['kobold']
278
+ try:
279
+ response = requests.post(kobold_api_ip, headers=headers, json=data)
280
+ logging.debug("kobold: API Response Status Code: %d", response.status_code)
281
+
282
+ if response.status_code == 200:
283
+ try:
284
+ response_data = response.json()
285
+ logging.debug("kobold: API Response Data: %s", response_data)
286
+
287
+ if response_data and 'results' in response_data and len(response_data['results']) > 0:
288
+ summary = response_data['results'][0]['text'].strip()
289
+ logging.debug("kobold: Summarization successful")
290
+ return summary
291
+ else:
292
+ logging.error("Expected data not found in API response.")
293
+ return "Expected data not found in API response."
294
+ except ValueError as e:
295
+ logging.error("kobold: Error parsing JSON response: %s", str(e))
296
+ return f"Error parsing JSON response: {str(e)}"
297
  else:
298
+ logging.error(f"kobold: API request failed with status code {response.status_code}: {response.text}")
299
+ return f"kobold: API request failed: {response.text}"
300
+ except Exception as e:
301
+ logging.error("kobold: Error in processing: %s", str(e))
302
+ return f"kobold: Error occurred while processing summary with kobold: {str(e)}"
 
303
  except Exception as e:
304
  logging.error("kobold: Error in processing: %s", str(e))
305
  return f"kobold: Error occurred while processing summary with kobold: {str(e)}"
306
 
307
 
308
  # https://github.com/oobabooga/text-generation-webui/wiki/12-%E2%80%90-OpenAI-API
309
+ def summarize_with_oobabooga(input_data, api_key, custom_prompt, api_url="http://127.0.0.1:5000/v1/chat/completions", temp=None, system_message=None):
310
+ logging.debug("Oobabooga: Summarization process starting...")
311
  try:
312
+ logging.debug("Oobabooga: Loading and validating configurations")
313
+ loaded_config_data = load_and_log_configs()
314
+ if loaded_config_data is None:
315
+ logging.error("Failed to load configuration data")
316
+ ooba_api_key = None
317
+ else:
318
+ # Prioritize the API key passed as a parameter
319
+ if api_key and api_key.strip():
320
+ ooba_api_key = api_key
321
+ logging.info("Oobabooga: Using API key provided as parameter")
322
+ else:
323
+ # If no parameter is provided, use the key from the config
324
+ ooba_api_key = loaded_config_data['api_keys'].get('ooba')
325
+ if ooba_api_key:
326
+ logging.info("Anthropic: Using API key from config file")
327
+ else:
328
+ logging.warning("Anthropic: No API key found in config file")
329
 
330
+ logging.debug(f"Oobabooga: Using API Key: {ooba_api_key[:5]}...{ooba_api_key[-5:]}")
 
331
 
332
  if isinstance(input_data, str) and os.path.isfile(input_data):
333
  logging.debug("Oobabooga: Loading json data for summarization")
 
365
  ooba_prompt = f"{text}" + f"\n\n\n\n{custom_prompt}"
366
  logging.debug("ooba: Prompt being sent is {ooba_prompt}")
367
 
368
+ if system_message is None:
369
+ system_message = "You are a helpful AI assistant."
370
+
371
  data = {
372
  "mode": "chat",
373
  "character": "Example",
374
+ "messages": [{"role": "user", "content": ooba_prompt}],
375
+ "system_message": system_message,
376
  }
377
 
378
  logging.debug("ooba: Submitting request to API endpoint")
 
395
  return f"ooba: Error occurred while processing summary with oobabooga: {str(e)}"
396
 
397
 
398
+
399
+ def summarize_with_tabbyapi(input_data, custom_prompt_input, api_key=None, api_IP="http://127.0.0.1:5000/v1/chat/completions", temp=None, system_message=None):
400
+ logging.debug("TabbyAPI: Summarization process starting...")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
401
  try:
402
+ logging.debug("TabbyAPI: Loading and validating configurations")
403
+ loaded_config_data = load_and_log_configs()
404
+ if loaded_config_data is None:
405
+ logging.error("Failed to load configuration data")
406
+ tabby_api_key = None
407
+ else:
408
+ # Prioritize the API key passed as a parameter
409
+ if api_key and api_key.strip():
410
+ tabby_api_key = api_key
411
+ logging.info("TabbyAPI: Using API key provided as parameter")
412
+ else:
413
+ # If no parameter is provided, use the key from the config
414
+ tabby_api_key = loaded_config_data['api_keys'].get('tabby')
415
+ if tabby_api_key:
416
+ logging.info("TabbyAPI: Using API key from config file")
417
+ else:
418
+ logging.warning("TabbyAPI: No API key found in config file")
419
+
420
+ tabby_api_ip = loaded_config_data['local_api_ip']['tabby']
421
+ tabby_model = loaded_config_data['models']['tabby']
422
+ if temp is None:
423
+ temp = 0.7
424
+
425
+ logging.debug(f"TabbyAPI: Using API Key: {tabby_api_key[:5]}...{tabby_api_key[-5:]}")
426
+
427
+ if isinstance(input_data, str) and os.path.isfile(input_data):
428
+ logging.debug("tabby: Loading json data for summarization")
429
+ with open(input_data, 'r') as file:
430
+ data = json.load(file)
431
+ else:
432
+ logging.debug("tabby: Using provided string data for summarization")
433
+ data = input_data
434
+
435
+ logging.debug(f"tabby: Loaded data: {data}")
436
+ logging.debug(f"tabby: Type of data: {type(data)}")
437
+
438
+ if isinstance(data, dict) and 'summary' in data:
439
+ # If the loaded data is a dictionary and already contains a summary, return it
440
+ logging.debug("tabby: Summary already exists in the loaded data")
441
+ return data['summary']
442
+
443
+ # If the loaded data is a list of segment dictionaries or a string, proceed with summarization
444
+ if isinstance(data, list):
445
+ segments = data
446
+ text = extract_text_from_segments(segments)
447
+ elif isinstance(data, str):
448
+ text = data
449
+ else:
450
+ raise ValueError("Invalid input data format")
451
+ if system_message is None:
452
+ system_message = "You are a helpful AI assistant."
453
+
454
+ headers = {
455
+ 'Authorization': f'Bearer {api_key}',
456
+ 'Content-Type': 'application/json'
457
+ }
458
+ data2 = {
459
+ 'max_tokens': 4096,
460
+ "min_tokens": 0,
461
+ 'temperature': temp,
462
+ #'top_p': 1.0,
463
+ #'top_k': 0,
464
+ #'frequency_penalty': 0,
465
+ #'presence_penalty': 0.0,
466
+ #"repetition_penalty": 1.0,
467
+ 'model': tabby_model,
468
+ 'user': custom_prompt_input,
469
+ 'messages': input_data
470
+ }
471
+
472
  response = requests.post(tabby_api_ip, headers=headers, json=data2)
473
+
474
+ if response.status_code == 200:
475
+ response_json = response.json()
476
+
477
+ # Validate the response structure
478
+ if all(key in response_json for key in ['id', 'choices', 'created', 'model', 'object', 'usage']):
479
+ logging.info("TabbyAPI: Received a valid 200 response")
480
+ summary = response_json['choices'][0].get('message', {}).get('content', '')
481
+ return summary
482
+ else:
483
+ logging.error("TabbyAPI: Received a 200 response, but the structure is invalid")
484
+ return "Error: Received an invalid response structure from TabbyAPI."
485
+
486
+ elif response.status_code == 422:
487
+ logging.error(f"TabbyAPI: Received a 422 error. Details: {response.json()}")
488
+ return "Error: Invalid request sent to TabbyAPI."
489
+
490
+ else:
491
+ response.raise_for_status() # This will raise an exception for other status codes
492
+
493
  except requests.exceptions.RequestException as e:
494
+ logging.error(f"Error summarizing with TabbyAPI: {e}")
495
+ return f"Error summarizing with TabbyAPI: {str(e)}"
496
+ except json.JSONDecodeError:
497
+ logging.error("TabbyAPI: Received an invalid JSON response")
498
+ return "Error: Received an invalid JSON response from TabbyAPI."
499
+ except Exception as e:
500
+ logging.error(f"Unexpected error in summarize_with_tabbyapi: {e}")
501
+ return f"Unexpected error in summarization process: {str(e)}"
502
+
503
+ def summarize_with_vllm(
504
+ input_data: Union[str, dict, list],
505
+ custom_prompt_input: str,
506
+ api_key: str = None,
507
+ vllm_api_url: str = "http://127.0.0.1:8000/v1/chat/completions",
508
+ model: str = None,
509
+ system_prompt: str = None,
510
+ temp: float = 0.7
511
+ ) -> str:
512
+ logging.debug("vLLM: Summarization process starting...")
513
+ try:
514
+ logging.debug("vLLM: Loading and validating configurations")
515
+ loaded_config_data = load_and_log_configs()
516
+ if loaded_config_data is None:
517
+ logging.error("Failed to load configuration data")
518
+ vllm_api_key = None
519
+ else:
520
+ # Prioritize the API key passed as a parameter
521
+ if api_key and api_key.strip():
522
+ vllm_api_key = api_key
523
+ logging.info("vLLM: Using API key provided as parameter")
524
+ else:
525
+ # If no parameter is provided, use the key from the config
526
+ vllm_api_key = loaded_config_data['api_keys'].get('vllm')
527
+ if vllm_api_key:
528
+ logging.info("vLLM: Using API key from config file")
529
+ else:
530
+ logging.warning("vLLM: No API key found in config file")
531
+
532
+ logging.debug(f"vLLM: Using API Key: {vllm_api_key[:5]}...{vllm_api_key[-5:]}")
533
+ # Process input data
534
+ if isinstance(input_data, str) and os.path.isfile(input_data):
535
+ logging.debug("vLLM: Loading json data for summarization")
536
+ with open(input_data, 'r') as file:
537
+ data = json.load(file)
538
+ else:
539
+ logging.debug("vLLM: Using provided data for summarization")
540
+ data = input_data
541
+
542
+ logging.debug(f"vLLM: Type of data: {type(data)}")
543
+
544
+ # Extract text for summarization
545
+ if isinstance(data, dict) and 'summary' in data:
546
+ logging.debug("vLLM: Summary already exists in the loaded data")
547
+ return data['summary']
548
+ elif isinstance(data, list):
549
+ text = extract_text_from_segments(data)
550
+ elif isinstance(data, str):
551
+ text = data
552
+ elif isinstance(data, dict):
553
+ text = json.dumps(data)
554
+ else:
555
+ raise ValueError("Invalid input data format")
556
+
557
+ logging.debug(f"vLLM: Extracted text (showing first 500 chars): {text[:500]}...")
558
+
559
+ if system_prompt is None:
560
+ system_prompt = "You are a helpful AI assistant."
561
+
562
+ model = model or loaded_config_data['models']['vllm']
563
+ if system_prompt is None:
564
+ system_prompt = "You are a helpful AI assistant."
565
+
566
+ # Prepare the API request
567
+ headers = {
568
+ "Content-Type": "application/json"
569
+ }
570
+
571
+ payload = {
572
+ "model": model,
573
+ "messages": [
574
+ {"role": "system", "content": system_prompt},
575
+ {"role": "user", "content": f"{custom_prompt_input}\n\n{text}"}
576
+ ]
577
+ }
578
+
579
+ # Make the API call
580
+ logging.debug(f"vLLM: Sending request to {vllm_api_url}")
581
+ response = requests.post(vllm_api_url, headers=headers, json=payload)
582
+
583
+ # Check for successful response
584
+ response.raise_for_status()
585
+
586
+ # Extract and return the summary
587
+ response_data = response.json()
588
+ if 'choices' in response_data and len(response_data['choices']) > 0:
589
+ summary = response_data['choices'][0]['message']['content']
590
+ logging.debug("vLLM: Summarization successful")
591
+ logging.debug(f"vLLM: Summary (first 500 chars): {summary[:500]}...")
592
+ return summary
593
+ else:
594
+ raise ValueError("Unexpected response format from vLLM API")
595
+
596
+ except requests.RequestException as e:
597
+ logging.error(f"vLLM: API request failed: {str(e)}")
598
+ return f"Error: vLLM API request failed - {str(e)}"
599
+ except json.JSONDecodeError as e:
600
+ logging.error(f"vLLM: Failed to parse API response: {str(e)}")
601
+ return f"Error: Failed to parse vLLM API response - {str(e)}"
602
+ except Exception as e:
603
+ logging.error(f"vLLM: Unexpected error during summarization: {str(e)}")
604
+ return f"Error: Unexpected error during vLLM summarization - {str(e)}"
605
+
606
+
607
+ def summarize_with_ollama(input_data, custom_prompt, api_url="http://127.0.0.1:11434/api/generate", api_key=None, temp=None, system_message=None, model=None):
608
+ try:
609
+ logging.debug("ollama: Loading and validating configurations")
610
+ loaded_config_data = load_and_log_configs()
611
+ if loaded_config_data is None:
612
+ logging.error("Failed to load configuration data")
613
+ ollama_api_key = None
614
+ else:
615
+ # Prioritize the API key passed as a parameter
616
+ if api_key and api_key.strip():
617
+ ollama_api_key = api_key
618
+ logging.info("Ollama: Using API key provided as parameter")
619
+ else:
620
+ # If no parameter is provided, use the key from the config
621
+ ollama_api_key = loaded_config_data['api_keys'].get('ollama')
622
+ if ollama_api_key:
623
+ logging.info("Ollama: Using API key from config file")
624
+ else:
625
+ logging.warning("Ollama: No API key found in config file")
626
+
627
+ model = loaded_config_data['models']['ollama']
628
+
629
+ # Load transcript
630
+ logging.debug("Ollama: Loading JSON data")
631
+ if isinstance(input_data, str) and os.path.isfile(input_data):
632
+ logging.debug("Ollama: Loading json data for summarization")
633
+ with open(input_data, 'r') as file:
634
+ data = json.load(file)
635
+ else:
636
+ logging.debug("Ollama: Using provided string data for summarization")
637
+ data = input_data
638
+
639
+ logging.debug(f"Ollama: Loaded data: {data}")
640
+ logging.debug(f"Ollama: Type of data: {type(data)}")
641
+
642
+ if isinstance(data, dict) and 'summary' in data:
643
+ # If the loaded data is a dictionary and already contains a summary, return it
644
+ logging.debug("Ollama: Summary already exists in the loaded data")
645
+ return data['summary']
646
+
647
+ # If the loaded data is a list of segment dictionaries or a string, proceed with summarization
648
+ if isinstance(data, list):
649
+ segments = data
650
+ text = extract_text_from_segments(segments)
651
+ elif isinstance(data, str):
652
+ text = data
653
+ else:
654
+ raise ValueError("Ollama: Invalid input data format")
655
+
656
+ headers = {
657
+ 'accept': 'application/json',
658
+ 'content-type': 'application/json',
659
+ }
660
+ if len(ollama_api_key) > 5:
661
+ headers['Authorization'] = f'Bearer {ollama_api_key}'
662
+
663
+ ollama_prompt = f"{custom_prompt} \n\n\n\n{text}"
664
+ if system_message == None:
665
+ system_message = "You are a helpful AI assistant."
666
+ logging.debug(f"llama: Prompt being sent is {ollama_prompt}")
667
+ if system_message is None:
668
+ system_message = "You are a helpful AI assistant."
669
+
670
+ data = {
671
+ "model": model,
672
+ "messages": [
673
+ {"role": "system",
674
+ "content": system_message
675
+ },
676
+ {"role": "user",
677
+ "content": ollama_prompt
678
+ }
679
+ ],
680
+ }
681
+
682
+ logging.debug("Ollama: Submitting request to API endpoint")
683
+ print("Ollama: Submitting request to API endpoint")
684
+ response = requests.post(api_url, headers=headers, json=data)
685
+ response_data = response.json()
686
+ logging.debug("API Response Data: %s", response_data)
687
+
688
+ if response.status_code == 200:
689
+ # if 'X' in response_data:
690
+ logging.debug(response_data)
691
+ summary = response_data['content'].strip()
692
+ logging.debug("Ollama: Summarization successful")
693
+ print("Summarization successful.")
694
+ return summary
695
+ else:
696
+ logging.error(f"Ollama: API request failed with status code {response.status_code}: {response.text}")
697
+ return f"Ollama: API request failed: {response.text}"
698
+
699
+ except Exception as e:
700
+ logging.error("Ollama: Error in processing: %s", str(e))
701
+ return f"Ollama: Error occurred while processing summary with ollama: {str(e)}"
702
 
703
 
704
  def save_summary_to_file(summary, file_path):