Spaces:
Runtime error
Runtime error
Added config
Browse files- app.py +22 -17
- config.json +7 -0
app.py
CHANGED
|
@@ -4,11 +4,14 @@ import json
|
|
| 4 |
import requests
|
| 5 |
import gradio as gr
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
def get_answer(question: str) -> Dict[str, Any]:
|
| 14 |
headers = {
|
|
@@ -23,7 +26,7 @@ def get_answer(question: str) -> Dict[str, Any]:
|
|
| 23 |
}
|
| 24 |
|
| 25 |
try:
|
| 26 |
-
response = requests.post(
|
| 27 |
response.raise_for_status()
|
| 28 |
content = response.json()['choices'][0]['message']['content']
|
| 29 |
return {
|
|
@@ -48,14 +51,14 @@ def format_results(results: List[Tuple[str, str]]) -> str:
|
|
| 48 |
def validate_tags(tags: str) -> None:
|
| 49 |
if not tags:
|
| 50 |
raise gr.Error('Validation error. It is necessary to set at least one tag')
|
| 51 |
-
if len(tags) >
|
| 52 |
-
raise gr.Error(f'Validation error. The maximum allowed number of tags is {
|
| 53 |
|
| 54 |
def validate_questions(questions: str) -> None:
|
| 55 |
if not questions:
|
| 56 |
raise gr.Error('Validation error. It is necessary to ask at least one question')
|
| 57 |
-
if len(questions) >
|
| 58 |
-
raise gr.Error(f'Validation error. The maximum allowed number of questions is {
|
| 59 |
|
| 60 |
def find_answers(tags: str, questions: str, progress=gr.Progress()) -> str:
|
| 61 |
tags = tags.split('\n')
|
|
@@ -69,15 +72,15 @@ def find_answers(tags: str, questions: str, progress=gr.Progress()) -> str:
|
|
| 69 |
results = []
|
| 70 |
for question in progress.tqdm(questions):
|
| 71 |
tagged_question = f'{tags_str} {question}'
|
| 72 |
-
for attempt in range(
|
| 73 |
answer = get_answer(tagged_question)
|
| 74 |
if answer['status']:
|
| 75 |
results.append((question, answer['content']))
|
| 76 |
break
|
| 77 |
-
elif attempt ==
|
| 78 |
results.append((question, 'An error occurred while receiving data.'))
|
| 79 |
else:
|
| 80 |
-
time.sleep(
|
| 81 |
|
| 82 |
return format_results(results)
|
| 83 |
|
|
@@ -88,13 +91,15 @@ with gr.Blocks(theme='soft', title='AnswerMate') as blocks:
|
|
| 88 |
gr.Markdown('The service allows you to get answers to all questions on the specified topic.')
|
| 89 |
with gr.Row():
|
| 90 |
tags_input = gr.Textbox(
|
| 91 |
-
label='Enter tags (each line is a separate tag). Maximum:
|
| 92 |
placeholder='.Net\nC#',
|
| 93 |
-
lines=
|
|
|
|
| 94 |
questions_input = gr.Textbox(
|
| 95 |
-
label='Enter questions (each line is a separate question). Maximum:
|
| 96 |
placeholder='What is inheritance, encapsulation, abstraction, polymorphism?\nWhat is CLR?',
|
| 97 |
-
lines=
|
|
|
|
| 98 |
process_button = gr.Button('Find answers')
|
| 99 |
outputs = gr.Textbox(label='Output', placeholder='Output will appear here')
|
| 100 |
process_button.click(fn=find_answers, inputs=[tags_input, questions_input], outputs=outputs)
|
|
|
|
| 4 |
import requests
|
| 5 |
import gradio as gr
|
| 6 |
|
| 7 |
+
with open("config.json", "r") as f:
|
| 8 |
+
config = json.load(f)
|
| 9 |
+
|
| 10 |
+
max_questions_count = config["MAX_QUESTIONS_COUNT"]
|
| 11 |
+
max_tags_count = config["MAX_TAGS_COUNT"]
|
| 12 |
+
max_attempts = config["MAX_ATTEMPS"]
|
| 13 |
+
wait_time = config["WAIT_TIME"]
|
| 14 |
+
chatgpt_url = config["CHATGPT_URL"]
|
| 15 |
|
| 16 |
def get_answer(question: str) -> Dict[str, Any]:
|
| 17 |
headers = {
|
|
|
|
| 26 |
}
|
| 27 |
|
| 28 |
try:
|
| 29 |
+
response = requests.post(chatgpt_url, headers=headers, data=json.dumps(payload))
|
| 30 |
response.raise_for_status()
|
| 31 |
content = response.json()['choices'][0]['message']['content']
|
| 32 |
return {
|
|
|
|
| 51 |
def validate_tags(tags: str) -> None:
|
| 52 |
if not tags:
|
| 53 |
raise gr.Error('Validation error. It is necessary to set at least one tag')
|
| 54 |
+
if len(tags) > max_tags_count:
|
| 55 |
+
raise gr.Error(f'Validation error. The maximum allowed number of tags is {max_tags_count}.')
|
| 56 |
|
| 57 |
def validate_questions(questions: str) -> None:
|
| 58 |
if not questions:
|
| 59 |
raise gr.Error('Validation error. It is necessary to ask at least one question')
|
| 60 |
+
if len(questions) > max_questions_count:
|
| 61 |
+
raise gr.Error(f'Validation error. The maximum allowed number of questions is {max_questions_count}.')
|
| 62 |
|
| 63 |
def find_answers(tags: str, questions: str, progress=gr.Progress()) -> str:
|
| 64 |
tags = tags.split('\n')
|
|
|
|
| 72 |
results = []
|
| 73 |
for question in progress.tqdm(questions):
|
| 74 |
tagged_question = f'{tags_str} {question}'
|
| 75 |
+
for attempt in range(max_attempts):
|
| 76 |
answer = get_answer(tagged_question)
|
| 77 |
if answer['status']:
|
| 78 |
results.append((question, answer['content']))
|
| 79 |
break
|
| 80 |
+
elif attempt == max_attempts - 1:
|
| 81 |
results.append((question, 'An error occurred while receiving data.'))
|
| 82 |
else:
|
| 83 |
+
time.sleep(wait_time)
|
| 84 |
|
| 85 |
return format_results(results)
|
| 86 |
|
|
|
|
| 91 |
gr.Markdown('The service allows you to get answers to all questions on the specified topic.')
|
| 92 |
with gr.Row():
|
| 93 |
tags_input = gr.Textbox(
|
| 94 |
+
label=f'Enter tags (each line is a separate tag). Maximum: {max_tags_count}.',
|
| 95 |
placeholder='.Net\nC#',
|
| 96 |
+
lines=max_tags_count
|
| 97 |
+
)
|
| 98 |
questions_input = gr.Textbox(
|
| 99 |
+
label=f'Enter questions (each line is a separate question). Maximum: {max_questions_count}.',
|
| 100 |
placeholder='What is inheritance, encapsulation, abstraction, polymorphism?\nWhat is CLR?',
|
| 101 |
+
lines=max_questions_count
|
| 102 |
+
)
|
| 103 |
process_button = gr.Button('Find answers')
|
| 104 |
outputs = gr.Textbox(label='Output', placeholder='Output will appear here')
|
| 105 |
process_button.click(fn=find_answers, inputs=[tags_input, questions_input], outputs=outputs)
|
config.json
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"MAX_QUESTIONS_COUNT": 25,
|
| 3 |
+
"MAX_TAGS_COUNT": 5,
|
| 4 |
+
"MAX_ATTEMPS": 3,
|
| 5 |
+
"WAIT_TIME": 3,
|
| 6 |
+
"CHATGPT_URL": "https://free.churchless.tech/v1/chat/completions"
|
| 7 |
+
}
|