so there is this code 

import os
import tempfile
from typing import List, Dict
import faiss
import numpy as np
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.embeddings import HuggingFaceBgeEmbeddings
from langchain.docstore.document import Document
from langchain.vectorstores import FAISS
from langchain.chains.question_answering import load_qa_chain
from langchain.llms import Ollama
from paddleocr import PaddleOCR
from pdf2image import convert_from_path

# Initialize PaddleOCR
ocr = PaddleOCR(use_angle_cls=True, lang='en', use_gpu=False)

def perform_ocr_on_pdf(pdf_path):
  print(f"Processing PDF: {pdf_path}")

  try:
      images = convert_from_path(pdf_path)
  except Exception as e:
      print(f"Error converting {pdf_path} to images: {e}")
      return ""

  full_text = ''

  for i, image in enumerate(images):
      with tempfile.NamedTemporaryFile(delete=False, suffix='.jpg') as temp_image:
          image.save(temp_image.name, 'JPEG')

          try:
              result = ocr.ocr(temp_image.name, cls=True)

              if result is None or len(result) == 0:
                  print(f"Warning: OCR returned no results for page {i+1} of {pdf_path}")
                  continue

              page_text = ''
              for line in result:
                  if line is None:
                      print(f"Warning: Empty line in OCR result for page {i+1} of {pdf_path}")
                      continue
                  for word_info in line:
                      if word_info is None or len(word_info) < 2:
                          print(f"Warning: Invalid word_info in OCR result for page {i+1} of {pdf_path}")
                          continue
                      page_text += word_info[1][0] + ' '
                  page_text += '\n'

              full_text += f'--- Page {i+1} ---\n'
              full_text += page_text + '\n'

          except Exception as e:
              print(f"Error processing page {i+1} of {pdf_path}: {e}")

      os.unlink(temp_image.name)
      print(f'Processed page {i+1} of {pdf_path}')

  if not full_text:
      print(f"Warning: No text extracted from {pdf_path}")

  return full_text

def process_pdfs(pdf_directory, output_directory):
  documents = []

  # Create output directory if it doesn't exist
  os.makedirs(output_directory, exist_ok=True)

  for filename in os.listdir(pdf_directory):
      if filename.lower().endswith('.pdf'):
          pdf_path = os.path.join(pdf_directory, filename)
          print(f"Processing {filename}")

          # Perform OCR on the PDF
          extracted_text = perform_ocr_on_pdf(pdf_path)

          if extracted_text:
              # Create a text file for the extracted content
              output_file = os.path.join(output_directory, f"{os.path.splitext(filename)[0]}.txt")
              with open(output_file, 'w', encoding='utf-8') as f:
                  f.write(extracted_text)
              print(f"Saved extracted text to {output_file}")

              # Create a Document object with the extracted text
              doc = Document(page_content=extracted_text, metadata={"source": filename})
              documents.append(doc)
          else:
              print(f"Warning: No text extracted from {filename}")

  return documents

def create_and_save_faiss_index(documents: List[Document], index_path: str):
  # Split documents into chunks
  text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=100)
  texts = text_splitter.split_documents(documents)

  # Create embeddings
  embeddings = HuggingFaceBgeEmbeddings()

  # Check if embeddings are generated correctly
  try:
      db = FAISS.from_documents(texts, embeddings)
      print("FAISS index created successfully.")
  except Exception as e:
      print(f"Error creating FAISS index: {e}")
      return

  # Ensure the directory exists
  if not os.path.exists(index_path):
      os.makedirs(index_path)

  # Save the FAISS index
  try:
      db.save_local(index_path)
      print(f"FAISS index saved to {index_path}.")
  except Exception as e:
      print(f"Error saving FAISS index: {e}")

def load_faiss_index(index_path: str):
  # Load the FAISS index
  embeddings = HuggingFaceBgeEmbeddings()
  try:
      return FAISS.load_local(index_path, embeddings, allow_dangerous_deserialization=True)
  except Exception as e:
      print(f"Error loading FAISS index: {e}")
      return None

def query_documents(query: str, db: FAISS, k: int = 20):
  # Retrieve relevant documents
  docs = db.similarity_search(query, k=k)

  # Use Ollama for question answering
  llm = Ollama(base_url="https://artglobal-lisa.hf.space", model="phi3.5:latest")
  chain = load_qa_chain(llm, chain_type="stuff")

  # Run the chain
  result = chain({"input_documents": docs, "question": query}, return_only_outputs=True)

  return result['output_text']

# Separate script for processing documents and creating index
def process_documents_and_create_index():
  pdf_directory = "/home/ec2-user/lisa/api/apps/pdfs"
  output_directory = "/home/ec2-user/lisa/api/apps/pdfs/output"
  index_path = "/home/ec2-user/lisa/api/apps/pdfs/faiss_index"

  # Process PDFs
  documents = process_pdfs(pdf_directory, output_directory)

  # Create and save FAISS index
  create_and_save_faiss_index(documents, index_path)

  print("Documents processed and index created.")

# Main script for querying
def main_query_script():
  index_path = "/home/ec2-user/lisa/api/apps/pdfs/faiss_index"

  # Load the FAISS index
  db = load_faiss_index(index_path)
  if db is None:
      print("Failed to load FAISS index. Exiting.")
      return

  while True:
      query = input("Enter your query (or 'quit' to exit): ")
      if query.lower() == 'quit':
          break

      result = query_documents(query, db)
      print("Answer:", result)
      print()

if __name__ == "__main__":
  # Uncomment the following line to process documents and create index
#   process_documents_and_create_index()

  # Run the main query script
  main_query_script()


which perform ocr can you modify this code to fastapi code please but make sure that fast api has two endpoints one to upload multiple files and another to just query and make sure when i query it does not process file again from zero