Peiiiiiiiiru's picture
Update app.py
56f4034 verified
# 安裝需要的庫
# 在 Hugging Face Spaces 中運行時,這些依賴應放在 requirements.txt 文件中
import fitz # PyMuPDF
import gradio as gr
from openai import OpenAI
# Function to extract text from the PDF
def extract_text_from_pdf(pdf_path):
document = fitz.open(pdf_path)
all_text = []
for page_num in range(len(document)):
page = document.load_page(page_num)
page_text = page.get_text("text")
all_text.append(page_text)
return "\n".join(all_text)
# Function to calculate movie protagonist potential score
def calculate_movie_potential(extracted_text, api_key):
client = OpenAI(api_key=api_key)
prompt = (
"請根據以下內容分析這個人是否適合成為電影主角,並給出百分比評分 (滿分100%)。請依以下分析維度評估:\n"
"1. 戲劇性(是否使用強烈的情緒用詞,如「驚喜」、「崩潰」)。\n"
"2. 成長潛力(是否提到挑戰或困難)。\n"
"3. 幽默感(是否有幽默的描述或笑話)。\n"
"4. 英雄氣質(是否有「幫助他人」的內容)。\n"
"5. 劇情代入感(是否有豐富的細節描述)。\n\n"
f"日記或聊天內容:\n{extracted_text[:4000]}\n\n"
"請給出總評分(百分比)並分項說明每個維度的得分與理由:"
)
response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": prompt}],
max_tokens=1000,
temperature=0.7
)
response_text = response.choices[0].message.content.strip()
return response_text
# Function to process PDF, generate content and calculate movie potential
def process_pdf_and_generate(api_key, pdf_file):
try:
# Extract text from the uploaded PDF file
extracted_text = extract_text_from_pdf(pdf_file)
# Check if extracted text is empty
if not extracted_text.strip():
return "Error: The uploaded PDF appears to be empty or contains unreadable text. Please upload a valid document."
# Calculate movie protagonist potential
movie_potential = calculate_movie_potential(extracted_text, api_key)
# Return the movie potential analysis
return movie_potential
except Exception as e:
return f"Error: {e}"
# Define the Gradio interface
interface = gr.Interface(
fn=process_pdf_and_generate,
inputs=[
gr.Textbox(label="API Key", type="password"),
gr.File(label="Upload PDF (Diary, Notes, Chat Logs)", type="filepath"),
],
outputs=gr.Textbox(label="Movie Protagonist Potential Analysis"),
title="Movie Protagonist Analyzer with RAG",
description=(
"Upload a PDF (diary, notes, or chat logs) and provide your OpenAI API key. The system will analyze the content "
"and evaluate how suitable you are to be a movie protagonist based on key dimensions using LangChain (RAG)."
)
)
# Launch the Gradio app
interface.launch()