elvin.v.mammadov commited on
Commit
8b5e60d
·
1 Parent(s): 9a891af

Updated app.py

Browse files
Files changed (1) hide show
  1. app.py +111 -0
app.py ADDED
@@ -0,0 +1,111 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+
4
+ def send_request(url):
5
+
6
+ payload = [
7
+ {"role": "user", "content": "Azərbaycan Neft-Qaz Sektorunda Ekoloji Risklər haqqında məlumat ver."}
8
+ ]
9
+
10
+ try:
11
+ response = requests.post(url, json=payload)
12
+
13
+ except Exception as e:
14
+
15
+ return {
16
+ "Error Message": f"Error occurred while sending request. Error message: {e}"
17
+ }
18
+
19
+ try:
20
+
21
+ response_json = response.json()
22
+
23
+ except Exception as e:
24
+
25
+ return {
26
+ "Error Message": f"Error occurred while converting to json. Error message: {e}",
27
+ "Response": response.content
28
+ }
29
+
30
+ if isinstance(response_json, dict):
31
+
32
+ if "sources" not in response_json.keys() or "answer" not in response_json.keys():
33
+
34
+ return {
35
+ "Error Message": "Required keys are not included in dictionary. `sources` and `answer` should be included in response keys.",
36
+ "Response": response_json
37
+ }
38
+
39
+ if isinstance(response_json['sources'], list) and isinstance(response_json['answer'], str):
40
+
41
+ for source in response_json['sources']:
42
+
43
+ if isinstance(source, dict):
44
+
45
+ if "pdf_name" not in source.keys() or "page_number" not in source.keys() or "content" not in source.keys():
46
+
47
+ return {
48
+ "Error Message": "Each source should have `pdf_name`, `page_number` and `content` as dictionary."
49
+ }
50
+
51
+ if isinstance(source['pdf_name'], str) and isinstance(source['page_number'], int) and isinstance(source['content'], str):
52
+
53
+ continue
54
+
55
+ else:
56
+
57
+ return {
58
+ "Error Message": "`pdf_name` should be of type str, `page_number` should be of type int, `content` should be of type str.",
59
+ "Response": response_json
60
+ }
61
+
62
+ else:
63
+
64
+ return {
65
+ "Error Message": "Sources in the `sources` should be type of dict.",
66
+ "Response": response_json
67
+ }
68
+
69
+ else:
70
+
71
+ return {
72
+ "Error Message": "`sources` should be type of list and `answer` should be type of str.",
73
+ "Response": response_json
74
+ }
75
+
76
+ else:
77
+
78
+ return {
79
+ "Error Message": f"Response should be dictionary with keys `sources` and `answer`.",
80
+ "Response": response_json
81
+ }
82
+
83
+ return response_json
84
+
85
+ with gr.Blocks() as demo:
86
+
87
+ output = gr.JSON(
88
+ label="Output"
89
+ )
90
+
91
+ input_box = gr.Textbox(
92
+ label="Input",
93
+ lines=1,
94
+ placeholder="Type your text here..."
95
+ )
96
+
97
+ send_btn = gr.Button("Send")
98
+
99
+ send_btn.click(
100
+ fn=send_request,
101
+ inputs=input_box,
102
+ outputs=output
103
+ )
104
+
105
+ input_box.submit(
106
+ fn=send_request,
107
+ inputs=input_box,
108
+ outputs=output
109
+ )
110
+
111
+ demo.launch()