File size: 3,055 Bytes
eb3f110
 
 
8916e27
eb3f110
 
 
 
 
 
 
 
 
8916e27
 
 
 
eb3f110
8916e27
 
 
 
 
eb3f110
 
8916e27
 
 
eb3f110
 
8916e27
eb3f110
 
 
8916e27
eb3f110
 
 
 
 
 
 
84fc647
 
eb3f110
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d308299
 
 
 
 
 
 
 
 
 
eb3f110
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import gradio as gr
from gradio.components import Textbox
import requests
import json
import time

MAX_QUESTIONS_COUNT = 25
MAX_TAGS_COUNT = 5
MAX_ATTEMPS = 3
WAIT_TIME = 3
CHATGPT_URL = "https://free.churchless.tech/v1/chat/completions"

def get_answer(question):
    headers = {
        "Content-Type": "application/json; charset=utf-8"
    }
    payload = {
        "model": "gpt-3.5-turbo",
        "messages": [{
            "role": "user",
            "content": question
        }]
    }

    try:
        response = requests.post(CHATGPT_URL, headers=headers, data=json.dumps(payload))
        response.raise_for_status()
        content = response.json()['choices'][0]['message']['content']
        return {
            'status': True,
            'content': content
        }
    except:
        return {
            'status': False
        }

def format_results(results):
    output = ""
    for i, (question, answer) in enumerate(results):
        output += f"Question №{i+1}: {question}\n"
        output += f"Answer: {answer}\n"
        if i < len(results) - 1:
            output += "--------------------------------------\n\n"
    output = output.strip()
    return output

def find_answers(tags, questions):
    tags = tags.split('\n')
    questions = questions.split('\n')
    if len(tags) == 0:
        raise gr.Error("Validation error. It is necessary to set at least one tag")
    if len(tags) > MAX_TAGS_COUNT:
        raise gr.Error(f"Validation error. The maximum allowed number of tags is {MAX_TAGS_COUNT}.")
    if len(questions) == 0:
        raise gr.Error("Validation error. It is necessary to ask at least one question")
    if len(questions) > MAX_QUESTIONS_COUNT:
        raise gr.Error(f"Validation error. The maximum allowed number of questions is {MAX_QUESTIONS_COUNT}.")

    configured_tags = ['[' + tag + ']' for tag in tags]
    results = []
    for question in questions:
        tagged_question = ''.join(configured_tags) + ' ' + question
        attempt = 0
        while attempt < MAX_ATTEMPS:
            answer = get_answer(tagged_question)
            if answer['status']:
                results.append((question, answer['content']))
                break
            else:
                attempt += 1
                if attempt == MAX_ATTEMPS:
                    results.append((question, 'An error occurred while receiving data.'))
                else:
                    time.sleep(WAIT_TIME)
                    continue
    return format_results(results)

inputs = [
    Textbox(label="Enter tags (each line is a separate tag). Maximum: 5.", lines=5),
    Textbox(label="Enter questions (each line is a separate question). Maximum 25.", lines=25)
]

outputs = [
    Textbox(label="Answers")
]

title = 'AnswerMate'
description = 'The service allows you to get answers to all questions on the specified topic.'

gradio_interface = gr.Interface(
    theme='soft',
    title=title,
    description=description,
    fn=find_answers, 
    inputs=inputs, 
    outputs=outputs)

gradio_interface.launch()