Brasd99 commited on
Commit
39b2f30
1 Parent(s): a8c7fff

Code refactoring

Browse files
Files changed (1) hide show
  1. app.py +19 -19
app.py CHANGED
@@ -8,17 +8,17 @@ MAX_QUESTIONS_COUNT = 25
8
  MAX_TAGS_COUNT = 5
9
  MAX_ATTEMPS = 3
10
  WAIT_TIME = 3
11
- CHATGPT_URL = "https://free.churchless.tech/v1/chat/completions"
12
 
13
  def get_answer(question):
14
  headers = {
15
- "Content-Type": "application/json; charset=utf-8"
16
  }
17
  payload = {
18
- "model": "gpt-3.5-turbo",
19
- "messages": [{
20
- "role": "user",
21
- "content": question
22
  }]
23
  }
24
 
@@ -38,24 +38,24 @@ def get_answer(question):
38
  def format_results(results):
39
  output = ""
40
  for i, (question, answer) in enumerate(results):
41
- output += f"Question №{i+1}: {question}\n"
42
- output += f"Answer: {answer}\n"
43
  if i < len(results) - 1:
44
- output += "--------------------------------------\n\n"
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')
@@ -64,11 +64,11 @@ def find_answers(tags, questions):
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']:
@@ -82,12 +82,12 @@ def find_answers(tags, questions):
82
  return format_results(results)
83
 
84
  inputs = [
85
- Textbox(label="Enter tags (each line is a separate tag). Maximum: 5.", lines=5),
86
- Textbox(label="Enter questions (each line is a separate question). Maximum 25.", lines=25)
87
  ]
88
 
89
  outputs = [
90
- Textbox(label="Answers")
91
  ]
92
 
93
  title = 'AnswerMate'
 
8
  MAX_TAGS_COUNT = 5
9
  MAX_ATTEMPS = 3
10
  WAIT_TIME = 3
11
+ CHATGPT_URL = 'https://free.churchless.tech/v1/chat/completions'
12
 
13
  def get_answer(question):
14
  headers = {
15
+ 'Content-Type': 'application/json; charset=utf-8'
16
  }
17
  payload = {
18
+ 'model': 'gpt-3.5-turbo',
19
+ 'messages': [{
20
+ 'role': 'user',
21
+ 'content': question
22
  }]
23
  }
24
 
 
38
  def format_results(results):
39
  output = ""
40
  for i, (question, answer) in enumerate(results):
41
+ output += f'Question №{i+1}: {question}\n'
42
+ output += f'Answer: {answer}\n'
43
  if i < len(results) - 1:
44
+ output += '--------------------------------------\n\n'
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')
 
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']:
 
82
  return format_results(results)
83
 
84
  inputs = [
85
+ Textbox(label='Enter tags (each line is a separate tag). Maximum: 5.', lines=5),
86
+ Textbox(label='Enter questions (each line is a separate question). Maximum 25.', lines=25)
87
  ]
88
 
89
  outputs = [
90
+ Textbox(label='Answers')
91
  ]
92
 
93
  title = 'AnswerMate'