dlflannery commited on
Commit
af8963c
·
verified ·
1 Parent(s): 7388e17

Update app.py

Browse files

Prompt into LLM and results from LLM are always English, translated as needed.

Files changed (1) hide show
  1. app.py +12 -6
app.py CHANGED
@@ -65,8 +65,8 @@ def update_translation_count(count, language):
65
  return 0
66
 
67
 
68
- def azure_translate_text(text, target_language):
69
- if target_language == 'en':
70
  return text
71
  path = '/translate'
72
  endpoint = 'https://api.cognitive.microsofttranslator.com'
@@ -462,9 +462,12 @@ def chat(prompt, user_window, pwd_window, past, response, gptModel, clip_text, d
462
  finish_reason = 'ok'
463
  rag_txt = ''
464
  prompt_bare = prompt
 
 
465
  first_time = False
466
  prompt_tokens = 0
467
  total_tokens = 0
 
468
  if len(past) == 0:
469
  first_time = True
470
  (results, prompt_tokens, total_tokens) = do_search(prompt, db_name, start_date, end_date)
@@ -504,12 +507,16 @@ def chat(prompt, user_window, pwd_window, past, response, gptModel, clip_text, d
504
  md(f'\n\n<h5>{name} ({upload_date})</h5><h6>At seek time: {seek_colons}</h6>[YouTube Link: ]({yt_url})\n\n{pure_text}\n================')
505
  rag_txt += pure_text
506
  prompt = rag_txt + '.\n ' + prompt + '\nGive higher priority to the information just provided.'
507
- if language != 'en':
508
- prompt += f' Provide the response in the {languages[language]} language'
 
 
509
  past.append({"role":"user", "content":prompt})
510
  completion = Client().chat.completions.create(model=gptModel, messages=past)
511
  reporting_model = gptModel
512
  reply = completion.choices[0].message.content
 
 
513
  tokens_in = completion.usage.prompt_tokens
514
  tokens_out = completion.usage.completion_tokens
515
  tokens = completion.usage.total_tokens
@@ -518,7 +525,6 @@ def chat(prompt, user_window, pwd_window, past, response, gptModel, clip_text, d
518
  if translation_count > 0:
519
  with open(dataDir + user_window + '_translation.txt','a') as f:
520
  f.write(f'Translation:{translation_count}\n')
521
-
522
  if len(clip_list) > 0:
523
  response += md(' '.join(map(str, clip_list)))
524
  else:
@@ -639,7 +645,7 @@ def list_permanent_files():
639
  def show_help():
640
  txt = '''
641
  MTOI Search scans a database you select that contains transcripts of MTOI video teachings, finding sections that
642
- relate to the question or topic you enter. It formulates a response based on that text and then lists up to ten
643
  text clips and YouTube links to the video at the point when the relevant text is spoken. Responses will be
644
  given in the selected translation language.
645
  1. Gemeral:
 
65
  return 0
66
 
67
 
68
+ def azure_translate_text(text, target_language, source_language = 'en'):
69
+ if target_language == source_language:
70
  return text
71
  path = '/translate'
72
  endpoint = 'https://api.cognitive.microsofttranslator.com'
 
462
  finish_reason = 'ok'
463
  rag_txt = ''
464
  prompt_bare = prompt
465
+ translation_count += update_translation_count(len(prompt), language)
466
+ prompt = azure_translate_text(prompt, "en", language)
467
  first_time = False
468
  prompt_tokens = 0
469
  total_tokens = 0
470
+ clip_list = []
471
  if len(past) == 0:
472
  first_time = True
473
  (results, prompt_tokens, total_tokens) = do_search(prompt, db_name, start_date, end_date)
 
507
  md(f'\n\n<h5>{name} ({upload_date})</h5><h6>At seek time: {seek_colons}</h6>[YouTube Link: ]({yt_url})\n\n{pure_text}\n================')
508
  rag_txt += pure_text
509
  prompt = rag_txt + '.\n ' + prompt + '\nGive higher priority to the information just provided.'
510
+ else:
511
+ prompt += '\nGive higher priority to the information just provided.'
512
+ # if language != 'en':
513
+ # prompt += f' Provide the response in the {languages[language]} language'
514
  past.append({"role":"user", "content":prompt})
515
  completion = Client().chat.completions.create(model=gptModel, messages=past)
516
  reporting_model = gptModel
517
  reply = completion.choices[0].message.content
518
+ reply = azure_translate_text(reply, language)
519
+ translation_count += update_translation_count(len(reply), language)
520
  tokens_in = completion.usage.prompt_tokens
521
  tokens_out = completion.usage.completion_tokens
522
  tokens = completion.usage.total_tokens
 
525
  if translation_count > 0:
526
  with open(dataDir + user_window + '_translation.txt','a') as f:
527
  f.write(f'Translation:{translation_count}\n')
 
528
  if len(clip_list) > 0:
529
  response += md(' '.join(map(str, clip_list)))
530
  else:
 
645
  def show_help():
646
  txt = '''
647
  MTOI Search scans a database you select that contains transcripts of MTOI video teachings, finding sections that
648
+ relate to the question or topic you enter. It formulates a response based on that text and then lists up to five
649
  text clips and YouTube links to the video at the point when the relevant text is spoken. Responses will be
650
  given in the selected translation language.
651
  1. Gemeral: