File size: 3,607 Bytes
8b5e60d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bf7a8c8
 
 
 
 
 
 
 
 
 
 
8b5e60d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import gradio as gr
import requests

def send_request(url):
    
    payload = [
            {"role": "user", "content": "Azərbaycan Neft-Qaz Sektorunda Ekoloji Risklər haqqında məlumat ver."}
        ]

    try:
        response = requests.post(url, json=payload)
        
    except Exception as e:
        
        return {
            "Error Message": f"Error occurred while sending request. Error message: {e}"
        }
        
    try: 
        
        response_json = response.json()
        
    except Exception as e:
        
        return {
            "Error Message": f"Error occurred while converting to json. Error message: {e}",
            "Response": response.content
        }
    
    if isinstance(response_json, dict):
        
        if "sources" not in response_json.keys() or "answer" not in response_json.keys():
            
            return {
                "Error Message": "Required keys are not included in dictionary. `sources` and `answer` should be included in response keys.",
                "Response": response_json
            }
            
        if isinstance(response_json['sources'], list) and isinstance(response_json['answer'], str):
            
            for source in response_json['sources']:
                
                if isinstance(source, dict):
                    
                    if "pdf_name" not in source.keys() or "page_number" not in source.keys() or "content" not in source.keys():
                        
                        return {
                            "Error Message": "Each source should have `pdf_name`, `page_number` and `content` as dictionary."
                        }
                        
                    if isinstance(source['pdf_name'], str) and isinstance(source['page_number'], int) and isinstance(source['content'], str):
                        
                        continue
                        
                    else:
                        
                        return {
                            "Error Message": "`pdf_name` should be of type str, `page_number` should be of type int, `content` should be of type str.",
                            "Response": response_json
                        }
                    
                else:
                    
                    return {
                        "Error Message": "Sources in the `sources` should be type of dict.",
                        "Response": response_json
                    }
        
        else:
            
            return {
                "Error Message": "`sources` should be type of list and `answer` should be type of str.",
                "Response": response_json
            }
    
    else:
        
        return {
            "Error Message": f"Response should be dictionary with keys `sources` and `answer`.",
            "Response": response_json
        }
    
    return response_json

with gr.Blocks() as demo:

    gr.Markdown(
        """
        # LLM Endpoint Response Validator

        Enter your endpoint below and click **Send** to get the result.

        Format:
        ```http://<host>/<endpoint>```
        """
    )

    output = gr.JSON(
        label="Output"
    )

    input_box = gr.Textbox(
        label="Input",
        lines=1,
        placeholder="Type your text here..."
    )

    send_btn = gr.Button("Send")

    send_btn.click(
        fn=send_request,
        inputs=input_box,
        outputs=output
    )

    input_box.submit(
        fn=send_request,
        inputs=input_box,
        outputs=output
    )

demo.launch()