Have you ever wondered, how to build a Ruby on Rails app for PDF analysis with OpenAI API?… imagine creating an application that not only handles file uploads but also leverages advanced AI to analyze and extract insights from your documents. Amazing! right? …
Today, we’ll explore how to do just that by building a small Rails application that allows users to upload PDFs, extract text, and analyze it using the OpenAI API.
Make sure:
Before we start, ensure you have the following installed:
- Ruby (version 2.7 or higher)
- Rails (version 7 or higher)
- Bundler
- OpenAI API key
A basic understanding of Ruby on Rails, are you ready?
Step 1: Create a new Rails application
First, create a new Rails application:
rails new pdf_analyzer --css=bootstrap
cd pdf_analyzer
Step 2: Add required gems
Add the following gems to your Gemfile
:
gem 'openai'
gem 'pdf-reader'
gem 'dotenv-rails', groups: [:development, :test]
Run bundle install
to install the gems.
About the added gems
OpenAI
OpenAI is a gem that provides a Ruby interface for interacting with OpenAI’s API, which includes powerful language models like GPT-3 and GPT-3.5-turbo. This tool simplifies the process of making requests to OpenAI’s API, enabling developers to incorporate advanced natural language processing capabilities into their Ruby applications.
Pdf-reader is a gem that enables reading and parsing of PDF files in Ruby. It allows you to extract text and metadata from PDF documents, which is essential for applications that need to process or analyze PDF content.
Dotenv-rails is a gem that loads environment variables from a .env file into the ENV hash in your Rails application. This is essential for managing configuration settings and sensitive information such as API keys.
Step 3: Set up environment variables
Create a .env
file in the root of your project and add your OpenAI API key:
OPENAI_API_KEY=your_openai_api_key
Make sure to add .env
to your .gitignore
to keep your API key secure.
Step 4: Generate a controller and views
Generate a controller for handling PDF uploads:
rails generate controller Pdfs new create show
Create the following views:
app/views/pdfs/new.html.erb
<h1>Upload a PDF</h1>
<%= form_with url: pdfs_path, local: true, multipart: true do |form| %>
<div class="mb-3">
<%= form.label :file, "Choose a PDF", class: "form-label" %>
<%= form.file_field :file, class: "form-control" %>
</div>
<%= form.submit "Upload", class: "btn btn-primary" %>
<% end %>
app/views/pdfs/show.html.erb
<h1>PDF Analysis</h1>
<p><strong>Extracted Text:</strong></p>
<pre><%= @text %></pre>
<p><strong>OpenAI Analysis:</strong></p>
<pre><%= @analysis %></pre>
<%= link_to 'Upload another PDF', new_pdf_path, class: "btn btn-secondary" %>
Step 5: Configure the controller
Update the PdfsController
to handle file uploads and interactions with the OpenAI API:
app/controllers/pdfs_controller.rb
require 'pdf-reader'
require 'openai'
class PdfsController < ApplicationController
def new
end
def create
@pdf = params[:pdf][:file]
if @pdf.content_type == 'application/pdf'
extracted_text = extract_text_from_pdf(@pdf.path)
analysis = analyze_text_with_openai(extracted_text)
render :show, locals: { text: extracted_text, analysis: analysis }
else
flash[:alert] = "Please upload a valid PDF file."
redirect_to new_pdf_path
end
end
private
def extract_text_from_pdf(pdf_path)
reader = PDF::Reader.new(pdf_path)
reader.pages.map(&:text).join("\n")
end
def analyze_text_with_openai(text)
client = OpenAI::Client.new(access_token: ENV['OPENAI_API_KEY'])
# Chunk the text if it's too long
chunks = chunk_text(text, max_tokens: 3000)
analyses = chunks.map do |chunk|
response = client.chat(
parameters: {
model: "gpt-3.5-turbo",
messages: [
{ role: "system", content: "You are an AI assistant that analyzes text from PDFs. Provide a concise summary, key points, and any notable insights." },
{ role: "user", content: chunk }
],
max_tokens: 500,
temperature: 0.7
}
)
response['choices'][0]['message']['content']
end
combine_analyses(analyses)
end
def chunk_text(text, max_tokens:)
words = text.split
chunks = []
current_chunk = []
words.each do |word|
if current_chunk.join(' ').length + word.length > max_tokens
chunks << current_chunk.join(' ')
current_chunk = []
end
current_chunk << word
end
chunks << current_chunk.join(' ') if current_chunk.any?
chunks
end
def combine_analyses(analyses)
"Combined Analysis:\n\n" + analyses.join("\n\n")
end
end
Step 6: Routes configuration
Update your config/routes.rb
to set up the necessary routes:
Rails.application.routes.draw do
root 'pdfs#new'
resources :pdfs, only: [:new, :create, :show]
end
Step 7: Run the application
Start your Rails server:
rails server
Visit http://localhost:3000
in your browser, and you should see the PDF upload form. Upload a PDF, and you’ll be able to see the extracted text and its analysis from OpenAI.
Wrap up
In this blog post, we walked through the steps to create a Ruby on Rails 7 application that allows users to upload PDFs, extract text from these PDFs, and analyze the text using the OpenAI API with the gpt-3.5-turbo
model. This is a powerful example of how "Ruby on Rails and AI", can be combined to automate and enhance text analysis tasks.
This basic application can be a foundation for more advanced projects. You might expand it to handle multiple pages, different file types, or more sophisticated OpenAI prompts to cater to specific needs. For instance, you could add features like text summarization, sentiment analysis, or keyword extraction to further enhance the analysis capabilities.
By leveraging Ruby on Rails for robust and scalable web application development and OpenAI’s advanced language models for intelligent text analysis, you’re equipped to tackle a wide range of automation and data processing challenges. Whether you’re looking to build tools for document processing, content analysis, or any other text-heavy application, the techniques covered here will serve as a strong starting point.
Happy coding, and may your applications become more powerful and insightful!
We know best!
If you need help integrating OpenAI or other language models into your applications, we’ll be happy to help! at MagmaLabs, we specialize in creating advanced AI solutions tailored to your needs.
Contact us today to learn more about how can we help you harness the power of AI!
Visit MagmaLabs.io for more information.