os1187 commited on
Commit
1ea5637
1 Parent(s): c0666b1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +5 -6
app.py CHANGED
@@ -6,6 +6,7 @@ from transformers import AutoTokenizer, AutoModel
6
  import torch
7
  from pdf2image import convert_from_path
8
  import io
 
9
 
10
  # CSS styles
11
  css = """
@@ -64,14 +65,12 @@ def get_text_embeddings(text, model_name='bert-base-uncased'):
64
  # Function to process PDF and generate a response
65
  def process_pdf_and_generate_response(pdf_file):
66
  try:
67
- # Save the uploaded PDF to a temporary file
68
- tmp_pdf_path = "/tmp/uploaded_file.pdf"
69
- with open(tmp_pdf_path, 'wb') as tmp_pdf:
70
- tmp_pdf.write(pdf_file.read())
71
 
72
  # Convert PDF to images
73
  img_dir = "pdf_images"
74
- pdf_to_images(tmp_pdf_path, img_dir)
75
 
76
  # Generate embeddings for each image
77
  image_embeddings = []
@@ -93,7 +92,7 @@ def process_pdf_and_generate_response(pdf_file):
93
 
94
  iface = gr.Interface(
95
  fn=process_pdf_and_generate_response,
96
- inputs=gr.File(label="Upload PDF", type="file"),
97
  outputs=gr.Textbox("Placeholder for PDF insights", label="Insights", type="text"),
98
  title="pdf-chatbot",
99
  description="Upload a PDF and receive insights based on its content.",
 
6
  import torch
7
  from pdf2image import convert_from_path
8
  import io
9
+ from io import BytesIO
10
 
11
  # CSS styles
12
  css = """
 
65
  # Function to process PDF and generate a response
66
  def process_pdf_and_generate_response(pdf_file):
67
  try:
68
+ # Convert the binary stream to a file-like object
69
+ pdf_file_stream = BytesIO(pdf_file)
 
 
70
 
71
  # Convert PDF to images
72
  img_dir = "pdf_images"
73
+ pdf_to_images(pdf_file_stream, img_dir)
74
 
75
  # Generate embeddings for each image
76
  image_embeddings = []
 
92
 
93
  iface = gr.Interface(
94
  fn=process_pdf_and_generate_response,
95
+ inputs=gr.File(label="Upload PDF", type="binary"), # Changed 'file' to 'binary'
96
  outputs=gr.Textbox("Placeholder for PDF insights", label="Insights", type="text"),
97
  title="pdf-chatbot",
98
  description="Upload a PDF and receive insights based on its content.",