Introduction
Imagine you're a film director or a scriptwriter. You have a brand new movie script in your hands. You've read it, of course, but you're wondering if it could be better. What if there was an AI that could read your script and provide you with detailed scene-by-scene feedback? Enter our Movie Script Analyst, a web app powered by OpenAI and Streamlit!
The AI Film Critic
Our script analyst isn't just any regular software. It's powered by OpenAI's GPT-3, a highly sophisticated AI model that can generate human-like text. You give it a scene from your movie, and it'll give you feedback as if it were a seasoned film critic!
Let's start by importing the necessary libraries:
import os
import re
import streamlit as st
from PyPDF2 import PdfReader
from fpdf import FPDF
import openai
We're using os
and re
for operating system interactions and regular expressions, respectively. streamlit
is for creating our web app, PyPDF2
for reading PDF files, FPDF
for writing to PDF files, and openai
to interact with the OpenAI API.
The Tech
Let's set the title of our web app and create a text input field for entering the OpenAI API key.
st.title("Film Script Analysis")
openai_api_key = st.text_input("Enter your OpenAI API Key", type="password")
The type="password"
argument ensures that the input is hidden.
Converting PDF to Text
Next, let's convert our movie script from a PDF to text:
def pdf_to_text(file_path):
reader = PdfReader(file_path)
text = " ".join([page.extract_text() for page in reader.pages])
return text
This function uses the PyPDF2.PdfReader
class to read the PDF and the extract_text
method to get the text from each page.
Splitting the Script into Scenes
Our movie script is a long text. To analyze it properly, we need to split it into scenes:
def split_into_scenes(text):
text = re.sub(r'\\b(INT\\.|EXT\\.)', r' \\1', text)
scenes = re.split(r' (?=INT\\.|EXT\\.)', text)
return scenes
This function uses regular expressions to look for scene headings, which usually start with "INT." (for interior scenes) or "EXT." (for exterior scenes).
Analyzing the Scenes
Now comes the fun part - analyzing the scenes! For this, we need to prompt the OpenAI model properly:
def analyze_scene(scene_content, prompt=None, model="gpt-3.5-turbo-16k"):
# system_prompt and user_prompt are set here
response, cost = generate_response(system_prompt, user_prompt, model)
return response, cost
This function takes the scene content as input and generates a prompt for the OpenAI model. The prompt is a detailed instruction telling the model to analyze the scene from the perspective of a movie reviewer. The function then calls the generate_response
function to get the analysis from the OpenAI model.
Generating a Response from OpenAI
The generate_response
function sends a chat completion request to the OpenAI API:
def generate_response(system_prompt, user_prompt, model="gpt-3.5-turbo-16k"):
# messages and params are set here
response = openai.ChatCompletion.create(**params)
reply = response.choices[0]["message"]["content"]
cost = response['usage']['total_tokens']
return reply, cost
It returns the model's response and the cost in tokens.
Writing the Analysis to a PDF
Once we have our analysis, we want to save it to a PDF:
def write_to_pdf(text, filename="output.pdf"):
pdf = FPDF()
pdf.add_page()
pdf.set_font("Arial", size=12)
pdf.multi_cell(0, 10, txt=text)
pdf.output(filename)
This function takes the analysis text and a filename as input and writes the text to a PDF file.
Bringing it All Together
The main part of our app creates a file uploader for uploading the script PDF:
uploaded_file = st.file_uploader("Choose a PDF file", type="pdf")
If a file is uploaded, the app reads the script text, splits it into scenes, and analyzes each scene with the OpenAI model. The analysis and cost of each scene are written to a PDF file:
if uploaded_file is not None:
# the code that reads the script, splits it, analyzes the scenes,
# and writes to a PDF goes here
Finally, we create a download button so users can download the PDF:
st.download_button(label="Download PDF", data=open("analysis_output.pdf", "rb"), file_name="analysis_output.pdf", mime="application/pdf")
And there you have it! A film script analyst in a web app, powered by OpenAI and Streamlit. Whether you're a director, a scriptwriter, or just a film enthusiast, this app can provide you with a detailed analysis of any movie script!