Brasd99 commited on
Commit
eb3f110
·
1 Parent(s): 8b1bf94

Initial commit

Browse files
Files changed (1) hide show
  1. app.py +92 -0
app.py ADDED
@@ -0,0 +1,92 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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 = 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
+ print(response.content)
31
+ print(response.json())
32
+ return {
33
+ 'status': True,
34
+ 'content': response.json()['choices'][0]['message']['content']
35
+ }
36
+ except:
37
+ return {
38
+ 'status': False,
39
+ }
40
+
41
+ def format_results(results):
42
+ output = ""
43
+ for i, (question, answer) in enumerate(results):
44
+ output += f"Question №{i+1}: {question}\n"
45
+ output += f"Answer: {answer}\n"
46
+ output += "\n"
47
+ output = output.strip()
48
+ return output
49
+
50
+ def find_answers(tags, questions):
51
+ tags = tags.split('\n')
52
+ questions = questions.split('\n')
53
+ if len(tags) == 0:
54
+ raise gr.Error("Validation error. It is necessary to set at least one tag")
55
+ if len(tags) > MAX_TAGS_COUNT:
56
+ raise gr.Error(f"Validation error. The maximum allowed number of tags is {MAX_TAGS_COUNT}.")
57
+ if len(questions) == 0:
58
+ raise gr.Error("Validation error. It is necessary to ask at least one question")
59
+ if len(questions) > MAX_QUESTIONS_COUNT:
60
+ raise gr.Error(f"Validation error. The maximum allowed number of questions is {MAX_QUESTIONS_COUNT}.")
61
+
62
+ configured_tags = ['[' + tag + ']' for tag in tags]
63
+ results = []
64
+ for question in questions:
65
+ tagged_question = ''.join(configured_tags) + ' ' + question
66
+ attempt = 0
67
+ while attempt < MAX_ATTEMPS:
68
+ answer = get_answer(tagged_question)
69
+ if answer['status']:
70
+ results.append((question, answer['content']))
71
+ break
72
+ else:
73
+ attempt += 1
74
+ if attempt == MAX_ATTEMPS:
75
+ results.append((question, 'An error occurred while receiving data.'))
76
+ else:
77
+ time.sleep(WAIT_TIME)
78
+ continue
79
+ return format_results(results)
80
+
81
+ inputs = [
82
+ Textbox(label="Enter tags (each line is a separate tag). Maximum: 5.", lines=5),
83
+ Textbox(label="Enter questions (each line is a separate question). Maximum 25.", lines=25)
84
+ ]
85
+
86
+ outputs = [
87
+ Textbox(label="Answers")
88
+ ]
89
+
90
+ gradio_interface = gr.Interface(fn=find_answers, inputs=inputs, outputs=outputs)
91
+
92
+ gradio_interface.launch()