Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -49,19 +49,59 @@ def encode_pdf_to_base64(pdf_path):
|
|
| 49 |
|
| 50 |
|
| 51 |
def ocr_pdf_with_gemini(pdf_path, model_name):
|
| 52 |
-
"""GeminiモデルでPDFをOCR
|
| 53 |
pdf_base64 = encode_pdf_to_base64(pdf_path)
|
| 54 |
model = genai.GenerativeModel(model_name)
|
|
|
|
| 55 |
prompt = """
|
| 56 |
You are an expert document processing assistant. Your task is to extract text from the provided PDF using OCR and convert it into a highly readable and visually appealing Markdown format.
|
| 57 |
|
| 58 |
**Crucial Instructions:**
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
"""
|
|
|
|
| 65 |
try:
|
| 66 |
response = model.generate_content(
|
| 67 |
[
|
|
|
|
| 49 |
|
| 50 |
|
| 51 |
def ocr_pdf_with_gemini(pdf_path, model_name):
|
| 52 |
+
"""GeminiモデルでPDFをOCRしてマークダウンに変換する関数"""
|
| 53 |
pdf_base64 = encode_pdf_to_base64(pdf_path)
|
| 54 |
model = genai.GenerativeModel(model_name)
|
| 55 |
+
|
| 56 |
prompt = """
|
| 57 |
You are an expert document processing assistant. Your task is to extract text from the provided PDF using OCR and convert it into a highly readable and visually appealing Markdown format.
|
| 58 |
|
| 59 |
**Crucial Instructions:**
|
| 60 |
+
|
| 61 |
+
1. **Consistency:** Maintain a consistent Markdown style (headings, lists, tables, font sizes, etc.) throughout the entire output, even if the PDF is split into multiple parts. Avoid variations in formatting between different sections.
|
| 62 |
+
2. **Visual Fidelity:** Strive to reproduce the original visual appearance of the PDF as closely as possible in Markdown. Pay close attention to:
|
| 63 |
+
* **Tables:** Recreate tables with accurate borders, cell alignment, and row/column spans. Use extended Markdown table syntax if needed.
|
| 64 |
+
* **Lists:** Preserve the original list style (numbered, bulleted, nested).
|
| 65 |
+
* **Font Sizes:** Use Markdown heading levels (#, ##, ###) and relative font size adjustments (if possible in your Markdown flavor) to approximate the visual hierarchy of the original text. Larger text should generally correspond to higher-level headings.
|
| 66 |
+
* **Layout:** Try to maintain the spatial relationships between elements (e.g., paragraphs, images). If there are multiple columns, consider representing that structure in Markdown, perhaps using tables.
|
| 67 |
+
3. **OCR Correction:** The OCR process may introduce errors (typos, misspellings, incorrect characters). Apply your language understanding capabilities to correct these errors and produce grammatically correct and semantically meaningful text. Do *not* blindly output the raw OCR result if it contains obvious mistakes.
|
| 68 |
+
4. **Content Filtering (Screenshots):** If the PDF primarily contains screenshots (e.g., of software interfaces), focus *exclusively* on extracting text from the *main content area* of the screenshots. *Do not* include text from:
|
| 69 |
+
* Window title bars
|
| 70 |
+
* Operating system toolbars (e.g., Windows taskbar)
|
| 71 |
+
* Menu bars *unless* they are directly related to the primary content (e.g. describing a software's menu options)
|
| 72 |
+
* Any other UI elements that are not part of the core content being displayed.
|
| 73 |
+
5. **Accuracy and Clarity:** Prioritize providing accurate and clear information to the user. Do not simply reproduce OCR output verbatim if it is nonsensical or misleading. Use your understanding of the content to present information in a user-friendly way.
|
| 74 |
+
6. **Output:** Only provide the extracted text in Markdown.
|
| 75 |
+
|
| 76 |
+
**Example (Illustrative - Adapt to the specific PDF):**
|
| 77 |
+
|
| 78 |
+
**Input PDF (Screenshot of a webpage):**
|
| 79 |
+
|
| 80 |
+
```
|
| 81 |
+
[Screenshot of a webpage with a large heading "Welcome", a paragraph of text, a bulleted list, and a table.]
|
| 82 |
+
```
|
| 83 |
+
|
| 84 |
+
**Desired Markdown Output:**
|
| 85 |
+
|
| 86 |
+
```markdown
|
| 87 |
+
# Welcome
|
| 88 |
+
|
| 89 |
+
This is a paragraph of introductory text. It explains the purpose of the webpage and provides some context.
|
| 90 |
+
|
| 91 |
+
* This is the first bullet point.
|
| 92 |
+
* This is the second bullet point.
|
| 93 |
+
* This is a nested bullet point.
|
| 94 |
+
|
| 95 |
+
| Feature | Description | Price |
|
| 96 |
+
|--------------|----------------------------|---------|
|
| 97 |
+
| Feature A | Description of Feature A | $10 |
|
| 98 |
+
| Feature B | Description of Feature B | $20 |
|
| 99 |
+
|
| 100 |
+
```
|
| 101 |
+
|
| 102 |
+
**Do NOT include text like "File Edit View" (from a menu bar) or "[X] Minimize Maximize Close" (from a window title bar).**
|
| 103 |
"""
|
| 104 |
+
|
| 105 |
try:
|
| 106 |
response = model.generate_content(
|
| 107 |
[
|