testS3PdfUpload / app.py
farukkr's picture
Update app.py
3af7a47 verified
import gradio as gr
import requests
import json
API_URL = "https://jj78yliqd7.execute-api.eu-central-1.amazonaws.com/dev/chatbot"
def get_answer(prompt, message):
params = {"prompt": prompt}
response = requests.get(API_URL, params=params)
if response.status_code == 200:
response_body = response.json().get('body')
answer = response_body.get('answer')
return answer
else:
return "Sorry, something went wrong. Please try again."
def upload_file(file):
pdf_file_path = file.name
with open(pdf_file_path, 'rb') as pdf_file:
pdf_binary_data = pdf_file.read()
API_ENDPOINT = "https://jj78yliqd7.execute-api.eu-central-1.amazonaws.com/dev/chatbot"
headers = {
'Content-Type': 'application/pdf'
}
pdf_file_path = pdf_file_path.replace("\\", "/").split("/")[-1]
params = {'filename': pdf_file_path}
response = requests.post(API_ENDPOINT, data=pdf_binary_data, headers=headers, params=params)
if response.status_code == 200:
print("File successfully uploaded")
else:
print(f"Failed to upload file. Status code: {response.status_code}, Response: {response.text}")
chat = gr.ChatInterface(
get_answer,
chatbot=gr.Chatbot(height=700),
textbox=gr.Textbox(placeholder="Ask me a question", container=False, scale=7),
title="Wissensguru - IDTA",
theme="soft",
examples=[
"When was the first version of this document produced?"
],
cache_examples=False,
retry_btn=None,
undo_btn="Delete Previous",
clear_btn="Clear",
)
with gr.Blocks() as bot:
gr.Markdown("Upload pdf files")
with gr.Tab("Pdf Upload"):
file_output = gr.File(file_types=["file"])
file_output.upload(upload_file, file_output)
with gr.Tab("Chatbot"):
chat.render()
bot.launch()