LLM_endpoint / app.py
elvin.v.mammadov
Added explanation
bf7a8c8
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()