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

Attemp to refactor code

Browse files
Files changed (1) hide show
  1. app.py +15 -16
app.py CHANGED
@@ -1,7 +1,7 @@
1
  import gradio as gr
2
  from gradio.components import Textbox
3
  import requests
4
- from requests.structures import CaseInsensitiveDict
5
  import time
6
 
7
  MAX_QUESTIONS_COUNT = 25
@@ -11,29 +11,28 @@ WAIT_TIME = 3
11
  CHATGPT_URL = "https://free.churchless.tech/v1/chat/completions"
12
 
13
  def get_answer(question):
14
- headers = CaseInsensitiveDict()
15
- headers["Content-Type"] = "application/json; charset=utf-8"
16
- data = """
17
- {{
18
  "model": "gpt-3.5-turbo",
19
- "messages": [
20
- {{
21
- "role": "user",
22
- "content": "{}"
23
- }}
24
- ]
25
- }}
26
- """.format(question).encode('utf-8')
27
 
28
  try:
29
- response = requests.post(CHATGPT_URL, headers=headers, data=data)
 
 
30
  return {
31
  'status': True,
32
- 'content': response.json()['choices'][0]['message']['content']
33
  }
34
  except:
35
  return {
36
- 'status': False,
37
  }
38
 
39
  def format_results(results):
 
1
  import gradio as gr
2
  from gradio.components import Textbox
3
  import requests
4
+ import json
5
  import time
6
 
7
  MAX_QUESTIONS_COUNT = 25
 
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
 
25
  try:
26
+ response = requests.post(CHATGPT_URL, headers=headers, data=json.dumps(payload))
27
+ response.raise_for_status()
28
+ content = response.json()['choices'][0]['message']['content']
29
  return {
30
  'status': True,
31
+ 'content': content
32
  }
33
  except:
34
  return {
35
+ 'status': False
36
  }
37
 
38
  def format_results(results):