# Otherwise open the file in binary mode and make a POST request to "lcserve_host"/ask_file route with data _data and the file
else:
withopen(file.name,"rb")asf:
r=requests.post(
@ -40,50 +50,82 @@ def ask_api(
)
try:
# Raise an HTTPError if one occurs while making a request to the server
r.raise_for_status()
exceptrequests.exceptions.HTTPErrorase:
raiseValueError(
raiseValueError(# Throw a ValueError if the request fails
f"Request failed with status code {r.status_code}: {e}"
)frome
# Return the value of the "result" key in the JSON response
returnr.json()["result"]
# Define variables title and description which describe our Gradio interface
title="PDF GPT"
description=""" PDF GPT allows you to chat with your PDF file using Universal Sentence Encoder and Open AI. It gives hallucination free response than other tools as the embeddings are better than OpenAI. The returned response can even cite the page number in square brackets([]) where the information is located, adding credibility to the responses and helping to locate pertinent information quickly."""
# Define a Gradio Blocks object named demo
withgr.Blocks()asdemo:
# Add a Markdown heading and description to the Gradio interface
gr.Markdown(f"<center><h1>{title}</h1></center>")
gr.Markdown(description)
# Create two side-by-side Groups for input fields and outputs
withgr.Row():
withgr.Group():
# Add a Textbox widget to accept the API host URL from the user
lcserve_host=gr.Textbox(
label="Enter your API Host here",
value="http://localhost:8080",
placeholder="http://localhost:8080",
)
# Add a link to the OpenAI API key webpage and a Password textbox to get the user's API Key
gr.Markdown(
'<p style="text-align:center">Get your Open AI API key <a href="https://platform.openai.com/account/api-keys">here</a></p>'
)
openAI_key=gr.Textbox(label="Enter your OpenAI API key here",type="password")
openAI_key=gr.Textbox(
label="Enter your OpenAI API key here",type="password"
)
# Add a Text box that allows users to enter URL of the PDF file they want to chat with
pdf_url=gr.Textbox(label="Enter PDF URL here")
# Add a File Upload widget so that users can upload their PDF/Research Paper/Book
gr.Markdown("<center><h4>OR<h4></center>")
file=gr.File(label="Upload your PDF/ Research Paper / Book here",file_types=[".pdf"])
file=gr.File(
label="Upload your PDF/ Research Paper / Book here",file_types=[".pdf"]
)
# Add a field for the user to enter their question
question=gr.Textbox(label="Enter your question here")
# Add a submit button for user to trigger their API request
btn=gr.Button(value="Submit")
btn.style(full_width=True)
# Add another group for the output area where the answer will be shown
withgr.Group():
answer=gr.Textbox(label="The answer to your question is :")
# Define function onclick() which will be called when the user clicks the "submit" button