Brasd99 commited on
Commit
a8c7fff
·
1 Parent(s): 8916e27

Code refactoring

Browse files
Files changed (1) hide show
  1. app.py +20 -15
app.py CHANGED
@@ -45,35 +45,40 @@ def format_results(results):
45
  output = output.strip()
46
  return output
47
 
48
- def find_answers(tags, questions):
49
- tags = tags.split('\n')
50
- questions = questions.split('\n')
51
- if len(tags) == 0:
52
  raise gr.Error("Validation error. It is necessary to set at least one tag")
53
  if len(tags) > MAX_TAGS_COUNT:
54
  raise gr.Error(f"Validation error. The maximum allowed number of tags is {MAX_TAGS_COUNT}.")
55
- if len(questions) == 0:
 
 
56
  raise gr.Error("Validation error. It is necessary to ask at least one question")
57
  if len(questions) > MAX_QUESTIONS_COUNT:
58
  raise gr.Error(f"Validation error. The maximum allowed number of questions is {MAX_QUESTIONS_COUNT}.")
59
 
60
- configured_tags = ['[' + tag + ']' for tag in tags]
 
 
 
 
 
 
 
 
61
  results = []
62
  for question in questions:
63
- tagged_question = ''.join(configured_tags) + ' ' + question
64
- attempt = 0
65
- while attempt < MAX_ATTEMPS:
66
  answer = get_answer(tagged_question)
67
  if answer['status']:
68
  results.append((question, answer['content']))
69
  break
 
 
70
  else:
71
- attempt += 1
72
- if attempt == MAX_ATTEMPS:
73
- results.append((question, 'An error occurred while receiving data.'))
74
- else:
75
- time.sleep(WAIT_TIME)
76
- continue
77
  return format_results(results)
78
 
79
  inputs = [
 
45
  output = output.strip()
46
  return output
47
 
48
+ def validate_tags(tags):
49
+ if not tags:
 
 
50
  raise gr.Error("Validation error. It is necessary to set at least one tag")
51
  if len(tags) > MAX_TAGS_COUNT:
52
  raise gr.Error(f"Validation error. The maximum allowed number of tags is {MAX_TAGS_COUNT}.")
53
+
54
+ def validate_questions(questions):
55
+ if not questions:
56
  raise gr.Error("Validation error. It is necessary to ask at least one question")
57
  if len(questions) > MAX_QUESTIONS_COUNT:
58
  raise gr.Error(f"Validation error. The maximum allowed number of questions is {MAX_QUESTIONS_COUNT}.")
59
 
60
+ def find_answers(tags, questions):
61
+ tags = tags.split('\n')
62
+ questions = questions.split('\n')
63
+
64
+ validate_tags(tags)
65
+ validate_questions(questions)
66
+
67
+ tags_str = ''.join([f"[{tag}]" for tag in tags])
68
+
69
  results = []
70
  for question in questions:
71
+ tagged_question = f"{tags_str} {question}"
72
+ for attempt in range(MAX_ATTEMPS):
 
73
  answer = get_answer(tagged_question)
74
  if answer['status']:
75
  results.append((question, answer['content']))
76
  break
77
+ elif attempt == MAX_ATTEMPS - 1:
78
+ results.append((question, 'An error occurred while receiving data.'))
79
  else:
80
+ time.sleep(WAIT_TIME)
81
+
 
 
 
 
82
  return format_results(results)
83
 
84
  inputs = [