content-core 0.7.2__tar.gz → 0.8.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of content-core might be problematic. Click here for more details.
- {content_core-0.7.2 → content_core-0.8.0}/PKG-INFO +13 -4
- {content_core-0.7.2 → content_core-0.8.0}/README.md +9 -3
- {content_core-0.7.2 → content_core-0.8.0}/docs/processors.md +12 -1
- {content_core-0.7.2 → content_core-0.8.0}/docs/usage.md +15 -5
- {content_core-0.7.2 → content_core-0.8.0}/pyproject.toml +4 -1
- {content_core-0.7.2 → content_core-0.8.0}/src/content_core/common/state.py +6 -2
- content_core-0.8.0/src/content_core/common/types.py +21 -0
- {content_core-0.7.2 → content_core-0.8.0}/src/content_core/content/extraction/graph.py +18 -3
- {content_core-0.7.2 → content_core-0.8.0}/src/content_core/processors/audio.py +19 -11
- content_core-0.8.0/src/content_core/processors/url.py +248 -0
- {content_core-0.7.2 → content_core-0.8.0}/tests/integration/test_extraction.py +53 -4
- {content_core-0.7.2 → content_core-0.8.0}/uv.lock +327 -159
- content_core-0.7.2/src/content_core/processors/url.py +0 -252
- {content_core-0.7.2 → content_core-0.8.0}/.github/PULL_REQUEST_TEMPLATE.md +0 -0
- {content_core-0.7.2 → content_core-0.8.0}/.github/workflows/publish.yml +0 -0
- {content_core-0.7.2 → content_core-0.8.0}/.gitignore +0 -0
- {content_core-0.7.2 → content_core-0.8.0}/.python-version +0 -0
- {content_core-0.7.2 → content_core-0.8.0}/CONTRIBUTING.md +0 -0
- {content_core-0.7.2 → content_core-0.8.0}/LICENSE +0 -0
- {content_core-0.7.2 → content_core-0.8.0}/Makefile +0 -0
- {content_core-0.7.2 → content_core-0.8.0}/prompts/content/cleanup.jinja +0 -0
- {content_core-0.7.2 → content_core-0.8.0}/prompts/content/summarize.jinja +0 -0
- {content_core-0.7.2 → content_core-0.8.0}/src/content_core/__init__.py +0 -0
- {content_core-0.7.2 → content_core-0.8.0}/src/content_core/cc_config.yaml +0 -0
- {content_core-0.7.2 → content_core-0.8.0}/src/content_core/common/__init__.py +0 -0
- {content_core-0.7.2 → content_core-0.8.0}/src/content_core/common/exceptions.py +0 -0
- {content_core-0.7.2 → content_core-0.8.0}/src/content_core/common/utils.py +0 -0
- {content_core-0.7.2 → content_core-0.8.0}/src/content_core/config.py +0 -0
- {content_core-0.7.2 → content_core-0.8.0}/src/content_core/content/__init__.py +0 -0
- {content_core-0.7.2 → content_core-0.8.0}/src/content_core/content/cleanup/__init__.py +0 -0
- {content_core-0.7.2 → content_core-0.8.0}/src/content_core/content/cleanup/core.py +0 -0
- {content_core-0.7.2 → content_core-0.8.0}/src/content_core/content/extraction/__init__.py +0 -0
- {content_core-0.7.2 → content_core-0.8.0}/src/content_core/content/summary/__init__.py +0 -0
- {content_core-0.7.2 → content_core-0.8.0}/src/content_core/content/summary/core.py +0 -0
- {content_core-0.7.2 → content_core-0.8.0}/src/content_core/logging.py +0 -0
- {content_core-0.7.2 → content_core-0.8.0}/src/content_core/models.py +0 -0
- {content_core-0.7.2 → content_core-0.8.0}/src/content_core/models_config.yaml +0 -0
- {content_core-0.7.2 → content_core-0.8.0}/src/content_core/notebooks/run.ipynb +0 -0
- {content_core-0.7.2 → content_core-0.8.0}/src/content_core/processors/docling.py +0 -0
- {content_core-0.7.2 → content_core-0.8.0}/src/content_core/processors/office.py +0 -0
- {content_core-0.7.2 → content_core-0.8.0}/src/content_core/processors/pdf.py +0 -0
- {content_core-0.7.2 → content_core-0.8.0}/src/content_core/processors/text.py +0 -0
- {content_core-0.7.2 → content_core-0.8.0}/src/content_core/processors/video.py +0 -0
- {content_core-0.7.2 → content_core-0.8.0}/src/content_core/processors/youtube.py +0 -0
- {content_core-0.7.2 → content_core-0.8.0}/src/content_core/py.typed +0 -0
- {content_core-0.7.2 → content_core-0.8.0}/src/content_core/templated_message.py +0 -0
- {content_core-0.7.2 → content_core-0.8.0}/src/content_core/tools/__init__.py +0 -0
- {content_core-0.7.2 → content_core-0.8.0}/src/content_core/tools/cleanup.py +0 -0
- {content_core-0.7.2 → content_core-0.8.0}/src/content_core/tools/extract.py +0 -0
- {content_core-0.7.2 → content_core-0.8.0}/src/content_core/tools/summarize.py +0 -0
- {content_core-0.7.2 → content_core-0.8.0}/tests/input_content/file.docx +0 -0
- {content_core-0.7.2 → content_core-0.8.0}/tests/input_content/file.epub +0 -0
- {content_core-0.7.2 → content_core-0.8.0}/tests/input_content/file.md +0 -0
- {content_core-0.7.2 → content_core-0.8.0}/tests/input_content/file.mp3 +0 -0
- {content_core-0.7.2 → content_core-0.8.0}/tests/input_content/file.mp4 +0 -0
- {content_core-0.7.2 → content_core-0.8.0}/tests/input_content/file.pdf +0 -0
- {content_core-0.7.2 → content_core-0.8.0}/tests/input_content/file.pptx +0 -0
- {content_core-0.7.2 → content_core-0.8.0}/tests/input_content/file.txt +0 -0
- {content_core-0.7.2 → content_core-0.8.0}/tests/input_content/file.xlsx +0 -0
- {content_core-0.7.2 → content_core-0.8.0}/tests/input_content/file_audio.mp3 +0 -0
- {content_core-0.7.2 → content_core-0.8.0}/tests/unit/test_docling.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: content-core
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.8.0
|
|
4
4
|
Summary: Extract what matters from any media source
|
|
5
5
|
Author-email: LUIS NOVO <lfnovo@gmail.com>
|
|
6
6
|
License-File: LICENSE
|
|
@@ -10,6 +10,8 @@ Requires-Dist: aiohttp>=3.11
|
|
|
10
10
|
Requires-Dist: bs4>=0.0.2
|
|
11
11
|
Requires-Dist: dicttoxml>=1.7.16
|
|
12
12
|
Requires-Dist: esperanto[openai]>=1.2.0
|
|
13
|
+
Requires-Dist: firecrawl-py>=2.7.0
|
|
14
|
+
Requires-Dist: firecrawl>=2.7.0
|
|
13
15
|
Requires-Dist: jinja2>=3.1.6
|
|
14
16
|
Requires-Dist: langdetect>=1.0.9
|
|
15
17
|
Requires-Dist: langgraph>=0.3.29
|
|
@@ -22,6 +24,7 @@ Requires-Dist: python-docx>=1.1.2
|
|
|
22
24
|
Requires-Dist: python-dotenv>=1.1.0
|
|
23
25
|
Requires-Dist: python-magic>=0.4.27
|
|
24
26
|
Requires-Dist: python-pptx>=1.0.2
|
|
27
|
+
Requires-Dist: readability-lxml>=0.8.4.1
|
|
25
28
|
Requires-Dist: validators>=0.34.0
|
|
26
29
|
Requires-Dist: youtube-transcript-api>=1.0.3
|
|
27
30
|
Provides-Extra: docling
|
|
@@ -39,6 +42,8 @@ Description-Content-Type: text/markdown
|
|
|
39
42
|
|
|
40
43
|
## Overview
|
|
41
44
|
|
|
45
|
+
> **Note:** As of v0.8, the default extraction engine is `'auto'`. Content Core will automatically select the best extraction method based on your environment and available API keys, with a smart fallback order for both URLs and files. For files/documents, `'auto'` now tries Docling first, then falls back to simple extraction. You can override the engine if needed, but `'auto'` is recommended for most users.
|
|
46
|
+
|
|
42
47
|
The primary goal of Content Core is to simplify the process of ingesting content from diverse origins. Whether you have raw text, a URL pointing to an article, or a local file like a video or markdown document, Content Core aims to extract the meaningful content for further use.
|
|
43
48
|
|
|
44
49
|
## Key Features
|
|
@@ -48,6 +53,10 @@ The primary goal of Content Core is to simplify the process of ingesting content
|
|
|
48
53
|
* Web URLs (using robust extraction methods).
|
|
49
54
|
* Local files (including automatic transcription for video/audio files and parsing for text-based formats).
|
|
50
55
|
* **Intelligent Processing:** Applies appropriate extraction techniques based on the source type. See the [Processors Documentation](./docs/processors.md) for detailed information on how different content types are handled.
|
|
56
|
+
* **Smart Engine Selection:** By default, Content Core uses the `'auto'` engine, which:
|
|
57
|
+
* For URLs: Uses Firecrawl if `FIRECRAWL_API_KEY` is set, else tries Jina. Jina might fail because of rate limits, which can be fixed by adding `JINA_API_KEY`. If Jina failes, BeautifulSoup is used as a fallback.
|
|
58
|
+
* For files: Tries Docling extraction first (for robust document parsing), then falls back to simple extraction if needed.
|
|
59
|
+
* You can override this by specifying an engine, but `'auto'` is recommended for most users.
|
|
51
60
|
* **Content Cleaning (Optional):** Likely integrates with LLMs (via `prompter.py` and Jinja templates) to refine and clean the extracted content.
|
|
52
61
|
* **Asynchronous:** Built with `asyncio` for efficient I/O operations.
|
|
53
62
|
|
|
@@ -218,15 +227,15 @@ async def main():
|
|
|
218
227
|
text_data = await extract_content({"content": "This is my sample text content."})
|
|
219
228
|
print(text_data)
|
|
220
229
|
|
|
221
|
-
# Extract from a URL
|
|
230
|
+
# Extract from a URL (uses 'auto' engine by default)
|
|
222
231
|
url_data = await extract_content({"url": "https://www.example.com"})
|
|
223
232
|
print(url_data)
|
|
224
233
|
|
|
225
|
-
# Extract from a local video file (gets transcript)
|
|
234
|
+
# Extract from a local video file (gets transcript, engine='auto' by default)
|
|
226
235
|
video_data = await extract_content({"file_path": "path/to/your/video.mp4"})
|
|
227
236
|
print(video_data)
|
|
228
237
|
|
|
229
|
-
# Extract from a local markdown file
|
|
238
|
+
# Extract from a local markdown file (engine='auto' by default)
|
|
230
239
|
md_data = await extract_content({"file_path": "path/to/your/document.md"})
|
|
231
240
|
print(md_data)
|
|
232
241
|
|
|
@@ -6,6 +6,8 @@
|
|
|
6
6
|
|
|
7
7
|
## Overview
|
|
8
8
|
|
|
9
|
+
> **Note:** As of v0.8, the default extraction engine is `'auto'`. Content Core will automatically select the best extraction method based on your environment and available API keys, with a smart fallback order for both URLs and files. For files/documents, `'auto'` now tries Docling first, then falls back to simple extraction. You can override the engine if needed, but `'auto'` is recommended for most users.
|
|
10
|
+
|
|
9
11
|
The primary goal of Content Core is to simplify the process of ingesting content from diverse origins. Whether you have raw text, a URL pointing to an article, or a local file like a video or markdown document, Content Core aims to extract the meaningful content for further use.
|
|
10
12
|
|
|
11
13
|
## Key Features
|
|
@@ -15,6 +17,10 @@ The primary goal of Content Core is to simplify the process of ingesting content
|
|
|
15
17
|
* Web URLs (using robust extraction methods).
|
|
16
18
|
* Local files (including automatic transcription for video/audio files and parsing for text-based formats).
|
|
17
19
|
* **Intelligent Processing:** Applies appropriate extraction techniques based on the source type. See the [Processors Documentation](./docs/processors.md) for detailed information on how different content types are handled.
|
|
20
|
+
* **Smart Engine Selection:** By default, Content Core uses the `'auto'` engine, which:
|
|
21
|
+
* For URLs: Uses Firecrawl if `FIRECRAWL_API_KEY` is set, else tries Jina. Jina might fail because of rate limits, which can be fixed by adding `JINA_API_KEY`. If Jina failes, BeautifulSoup is used as a fallback.
|
|
22
|
+
* For files: Tries Docling extraction first (for robust document parsing), then falls back to simple extraction if needed.
|
|
23
|
+
* You can override this by specifying an engine, but `'auto'` is recommended for most users.
|
|
18
24
|
* **Content Cleaning (Optional):** Likely integrates with LLMs (via `prompter.py` and Jinja templates) to refine and clean the extracted content.
|
|
19
25
|
* **Asynchronous:** Built with `asyncio` for efficient I/O operations.
|
|
20
26
|
|
|
@@ -185,15 +191,15 @@ async def main():
|
|
|
185
191
|
text_data = await extract_content({"content": "This is my sample text content."})
|
|
186
192
|
print(text_data)
|
|
187
193
|
|
|
188
|
-
# Extract from a URL
|
|
194
|
+
# Extract from a URL (uses 'auto' engine by default)
|
|
189
195
|
url_data = await extract_content({"url": "https://www.example.com"})
|
|
190
196
|
print(url_data)
|
|
191
197
|
|
|
192
|
-
# Extract from a local video file (gets transcript)
|
|
198
|
+
# Extract from a local video file (gets transcript, engine='auto' by default)
|
|
193
199
|
video_data = await extract_content({"file_path": "path/to/your/video.mp4"})
|
|
194
200
|
print(video_data)
|
|
195
201
|
|
|
196
|
-
# Extract from a local markdown file
|
|
202
|
+
# Extract from a local markdown file (engine='auto' by default)
|
|
197
203
|
md_data = await extract_content({"file_path": "path/to/your/document.md"})
|
|
198
204
|
print(md_data)
|
|
199
205
|
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
# Content Core Processors
|
|
2
2
|
|
|
3
|
+
**Note:** As of vNEXT, the default extraction engine is now `'auto'`. This means Content Core will automatically select the best extraction method based on your environment and available API keys, with a smart fallback order for both URLs and files. For files/documents, `'auto'` now tries Docling first, then falls back to simple extraction. See details below.
|
|
4
|
+
|
|
3
5
|
This document provides an overview of the content processors available in Content Core. These processors are responsible for extracting and handling content from various sources and file types.
|
|
4
6
|
|
|
5
7
|
## Overview
|
|
@@ -19,6 +21,11 @@ Content Core uses a modular approach to process content from different sources.
|
|
|
19
21
|
- **Supported Input**: URLs (web pages).
|
|
20
22
|
- **Returned Data**: Extracted text content from the web page, often in a cleaned format.
|
|
21
23
|
- **Location**: `src/content_core/processors/url.py`
|
|
24
|
+
- **Default Engine (`auto`) Logic**:
|
|
25
|
+
- If `FIRECRAWL_API_KEY` is set, uses Firecrawl for extraction.
|
|
26
|
+
- Else it tries Jina until it fails because of rate limits (unless `JINA_API_KEY` is set).
|
|
27
|
+
- Else, falls back to BeautifulSoup-based extraction.
|
|
28
|
+
- You can explicitly specify an engine (`'firecrawl'`, `'jina'`, `'simple'`, etc.), but `'auto'` is now the default and recommended for most users.
|
|
22
29
|
|
|
23
30
|
### 3. **File Processor**
|
|
24
31
|
- **Purpose**: Processes local files of various types, extracting content based on file format.
|
|
@@ -40,10 +47,14 @@ Content Core uses a modular approach to process content from different sources.
|
|
|
40
47
|
- **Supported Input**: PDF, DOCX, XLSX, PPTX, Markdown, AsciiDoc, HTML, CSV, Images (PNG, JPEG, TIFF, BMP).
|
|
41
48
|
- **Returned Data**: Content converted to configured format (markdown, html, json).
|
|
42
49
|
- **Location**: `src/content_core/processors/docling.py`
|
|
50
|
+
- **Default Engine (`auto`) Logic for Files/Documents**:
|
|
51
|
+
- Tries the `'docling'` extraction method first (robust document parsing for supported types).
|
|
52
|
+
- If `'docling'` fails or is not supported, automatically falls back to simple extraction (fast, lightweight for supported types).
|
|
53
|
+
- You can explicitly specify `'docling'`, `'simple'`, or `'legacy'` as the engine, but `'auto'` is now the default and recommended for most users.
|
|
43
54
|
- **Configuration**: Activate the Docling engine in `cc_config.yaml` or custom config:
|
|
44
55
|
```yaml
|
|
45
56
|
extraction:
|
|
46
|
-
engine: docling # '
|
|
57
|
+
engine: docling # 'auto' (default), 'docling', or 'simple'
|
|
47
58
|
docling:
|
|
48
59
|
output_format: markdown # markdown | html | json
|
|
49
60
|
```
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
# Using the Content Core Library
|
|
2
2
|
|
|
3
|
+
> **Note:** As of vNEXT, the default extraction engine is `'auto'`. Content Core will automatically select the best extraction method based on your environment and available API keys, with a smart fallback order for both URLs and files. For files/documents, `'auto'` now tries Docling first, then falls back to simple extraction. You can override the engine if needed, but `'auto'` is recommended for most users.
|
|
4
|
+
|
|
3
5
|
This documentation explains how to configure and use the **Content Core** library in your projects. The library allows customization of AI model settings through a YAML file and environment variables.
|
|
4
6
|
|
|
5
7
|
## Environment Variable for Configuration
|
|
@@ -76,20 +78,28 @@ To simplify setup, we suggest copying the provided sample files:
|
|
|
76
78
|
|
|
77
79
|
This will allow you to quickly start with customized settings without needing to create the files from scratch.
|
|
78
80
|
|
|
79
|
-
###
|
|
81
|
+
### Extraction Engine Selection
|
|
82
|
+
|
|
83
|
+
By default, Content Core uses the `'auto'` engine for all extraction tasks. The logic is as follows:
|
|
84
|
+
- **For URLs**: Uses Firecrawl if `FIRECRAWL_API_KEY` is set, else Jina if `JINA_API_KEY` is set, else falls back to BeautifulSoup.
|
|
85
|
+
- **For files**: Tries Docling extraction first (for robust document parsing), then falls back to simple extraction if needed.
|
|
86
|
+
|
|
87
|
+
You can override this behavior by specifying an engine in your config or function call, but `'auto'` is recommended for most users.
|
|
88
|
+
|
|
89
|
+
#### Docling Engine
|
|
80
90
|
|
|
81
|
-
Content Core supports an optional Docling engine for advanced document parsing. To enable:
|
|
91
|
+
Content Core supports an optional Docling engine for advanced document parsing. To enable Docling explicitly:
|
|
82
92
|
|
|
83
|
-
|
|
93
|
+
##### In YAML config
|
|
84
94
|
Add under the `extraction` section:
|
|
85
95
|
```yaml
|
|
86
96
|
extraction:
|
|
87
|
-
engine: docling #
|
|
97
|
+
engine: docling # auto (default), docling, or simple
|
|
88
98
|
docling:
|
|
89
99
|
output_format: html # markdown | html | json
|
|
90
100
|
```
|
|
91
101
|
|
|
92
|
-
|
|
102
|
+
##### Programmatically in Python
|
|
93
103
|
```python
|
|
94
104
|
from content_core.config import set_extraction_engine, set_docling_output_format
|
|
95
105
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "content-core"
|
|
3
|
-
version = "0.
|
|
3
|
+
version = "0.8.0"
|
|
4
4
|
description = "Extract what matters from any media source"
|
|
5
5
|
readme = "README.md"
|
|
6
6
|
homepage = "https://github.com/lfnovo/content-core"
|
|
@@ -28,6 +28,9 @@ dependencies = [
|
|
|
28
28
|
"validators>=0.34.0",
|
|
29
29
|
"ai-prompter>=0.2.3",
|
|
30
30
|
"moviepy>=2.1.2",
|
|
31
|
+
"readability-lxml>=0.8.4.1",
|
|
32
|
+
"firecrawl>=2.7.0",
|
|
33
|
+
"firecrawl-py>=2.7.0",
|
|
31
34
|
]
|
|
32
35
|
|
|
33
36
|
[project.optional-dependencies]
|
|
@@ -2,6 +2,9 @@ from typing import Optional
|
|
|
2
2
|
|
|
3
3
|
from pydantic import BaseModel, Field
|
|
4
4
|
|
|
5
|
+
from content_core.common.types import Engine
|
|
6
|
+
from content_core.common.types import Engine
|
|
7
|
+
|
|
5
8
|
|
|
6
9
|
class ProcessSourceState(BaseModel):
|
|
7
10
|
file_path: Optional[str] = ""
|
|
@@ -13,8 +16,9 @@ class ProcessSourceState(BaseModel):
|
|
|
13
16
|
identified_provider: Optional[str] = ""
|
|
14
17
|
metadata: Optional[dict] = Field(default_factory=lambda: {})
|
|
15
18
|
content: Optional[str] = ""
|
|
16
|
-
engine: Optional[
|
|
17
|
-
default=None,
|
|
19
|
+
engine: Optional[Engine] = Field(
|
|
20
|
+
default=None,
|
|
21
|
+
description="Override extraction engine: 'auto', 'simple', 'legacy', 'firecrawl', 'jina', or 'docling'",
|
|
18
22
|
)
|
|
19
23
|
output_format: Optional[str] = Field(
|
|
20
24
|
default=None,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
from typing import Literal
|
|
2
|
+
import warnings
|
|
3
|
+
|
|
4
|
+
Engine = Literal[
|
|
5
|
+
"auto",
|
|
6
|
+
"simple",
|
|
7
|
+
"legacy",
|
|
8
|
+
"firecrawl",
|
|
9
|
+
"jina",
|
|
10
|
+
"docling",
|
|
11
|
+
]
|
|
12
|
+
|
|
13
|
+
DEPRECATED_ENGINES = {"legacy": "simple"}
|
|
14
|
+
|
|
15
|
+
def warn_if_deprecated_engine(engine: str):
|
|
16
|
+
if engine in DEPRECATED_ENGINES:
|
|
17
|
+
warnings.warn(
|
|
18
|
+
f"Engine '{engine}' is deprecated and will be removed in a future release. Use '{DEPRECATED_ENGINES[engine]}' instead.",
|
|
19
|
+
DeprecationWarning,
|
|
20
|
+
stacklevel=2,
|
|
21
|
+
)
|
|
@@ -2,6 +2,7 @@ import os
|
|
|
2
2
|
import tempfile
|
|
3
3
|
from typing import Any, Dict, Optional
|
|
4
4
|
from urllib.parse import urlparse
|
|
5
|
+
from content_core.common.types import warn_if_deprecated_engine
|
|
5
6
|
|
|
6
7
|
import aiohttp
|
|
7
8
|
import magic
|
|
@@ -114,14 +115,28 @@ async def download_remote_file(state: ProcessSourceState) -> Dict[str, Any]:
|
|
|
114
115
|
return {"file_path": tmp, "identified_type": mime}
|
|
115
116
|
|
|
116
117
|
|
|
118
|
+
|
|
117
119
|
async def file_type_router_docling(state: ProcessSourceState) -> str:
|
|
118
120
|
"""
|
|
119
|
-
Route to Docling if enabled and supported; otherwise use
|
|
121
|
+
Route to Docling if enabled and supported; otherwise use simple file type edge.
|
|
122
|
+
Supports 'auto', 'docling', 'simple', and 'legacy' (deprecated, alias for simple).
|
|
123
|
+
'auto' tries simple first, then falls back to docling if simple fails.
|
|
120
124
|
"""
|
|
121
|
-
|
|
122
|
-
|
|
125
|
+
engine = state.engine or CONFIG.get("extraction", {}).get("engine", "auto")
|
|
126
|
+
warn_if_deprecated_engine(engine)
|
|
127
|
+
if engine == "auto":
|
|
128
|
+
# Try docling first; if it fails or is not supported, fallback to simple
|
|
129
|
+
if state.identified_type in DOCLING_SUPPORTED:
|
|
130
|
+
try:
|
|
131
|
+
return "extract_docling"
|
|
132
|
+
except Exception as e:
|
|
133
|
+
logger.warning(f"Docling extraction failed in 'auto' mode, falling back to simple: {e}")
|
|
134
|
+
# Fallback to simple
|
|
135
|
+
return await file_type_edge(state)
|
|
136
|
+
|
|
123
137
|
if engine == "docling" and state.identified_type in DOCLING_SUPPORTED:
|
|
124
138
|
return "extract_docling"
|
|
139
|
+
# For 'simple' and 'legacy', use the default file type edge
|
|
125
140
|
return await file_type_edge(state)
|
|
126
141
|
|
|
127
142
|
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import asyncio
|
|
2
|
+
import math
|
|
2
3
|
import os
|
|
3
4
|
import tempfile
|
|
4
|
-
import math
|
|
5
5
|
import traceback
|
|
6
6
|
from functools import partial
|
|
7
|
+
|
|
7
8
|
from moviepy import AudioFileClip
|
|
8
9
|
|
|
9
10
|
from content_core.common import ProcessSourceState
|
|
@@ -64,7 +65,9 @@ async def split_audio(input_file, segment_length_minutes=15, output_prefix=None)
|
|
|
64
65
|
)
|
|
65
66
|
|
|
66
67
|
|
|
67
|
-
def extract_audio(
|
|
68
|
+
def extract_audio(
|
|
69
|
+
input_file: str, output_file: str, start_time: float = None, end_time: float = None
|
|
70
|
+
) -> None:
|
|
68
71
|
"""
|
|
69
72
|
Extract audio from a video or audio file and save it as an MP3 file.
|
|
70
73
|
If start_time and end_time are provided, only that segment of audio is extracted.
|
|
@@ -78,17 +81,17 @@ def extract_audio(input_file: str, output_file: str, start_time: float = None, e
|
|
|
78
81
|
try:
|
|
79
82
|
# Load the file as an AudioFileClip
|
|
80
83
|
audio_clip = AudioFileClip(input_file)
|
|
81
|
-
|
|
82
|
-
# If start_time and end_time are provided, trim the audio
|
|
84
|
+
|
|
85
|
+
# If start_time and/or end_time are provided, trim the audio using subclipped
|
|
83
86
|
if start_time is not None and end_time is not None:
|
|
84
|
-
audio_clip = audio_clip.
|
|
87
|
+
audio_clip = audio_clip.subclipped(start_time, end_time)
|
|
85
88
|
elif start_time is not None:
|
|
86
|
-
audio_clip = audio_clip.
|
|
89
|
+
audio_clip = audio_clip.subclipped(start_time)
|
|
87
90
|
elif end_time is not None:
|
|
88
|
-
audio_clip = audio_clip.
|
|
91
|
+
audio_clip = audio_clip.subclipped(0, end_time)
|
|
89
92
|
|
|
90
93
|
# Export the audio as MP3
|
|
91
|
-
audio_clip.write_audiofile(output_file, codec=
|
|
94
|
+
audio_clip.write_audiofile(output_file, codec="mp3")
|
|
92
95
|
audio_clip.close()
|
|
93
96
|
except Exception as e:
|
|
94
97
|
logger.error(f"Error extracting audio: {str(e)}")
|
|
@@ -117,7 +120,9 @@ async def extract_audio_data(data: ProcessSourceState):
|
|
|
117
120
|
output_files = []
|
|
118
121
|
|
|
119
122
|
if duration_s > segment_length_s:
|
|
120
|
-
logger.info(
|
|
123
|
+
logger.info(
|
|
124
|
+
f"Audio is longer than 10 minutes ({duration_s}s), splitting into {math.ceil(duration_s / segment_length_s)} segments"
|
|
125
|
+
)
|
|
121
126
|
for i in range(math.ceil(duration_s / segment_length_s)):
|
|
122
127
|
start_time = i * segment_length_s
|
|
123
128
|
end_time = min((i + 1) * segment_length_s, audio.duration)
|
|
@@ -134,15 +139,18 @@ async def extract_audio_data(data: ProcessSourceState):
|
|
|
134
139
|
|
|
135
140
|
# Transcribe audio files
|
|
136
141
|
from content_core.models import ModelFactory
|
|
142
|
+
|
|
137
143
|
speech_to_text_model = ModelFactory.get_model("speech_to_text")
|
|
138
144
|
transcriptions = []
|
|
139
145
|
for audio_file in output_files:
|
|
140
|
-
transcription = await transcribe_audio_segment(
|
|
146
|
+
transcription = await transcribe_audio_segment(
|
|
147
|
+
audio_file, speech_to_text_model
|
|
148
|
+
)
|
|
141
149
|
transcriptions.append(transcription)
|
|
142
150
|
|
|
143
151
|
return {
|
|
144
152
|
"metadata": {"audio_files": output_files},
|
|
145
|
-
"content": " ".join(transcriptions)
|
|
153
|
+
"content": " ".join(transcriptions),
|
|
146
154
|
}
|
|
147
155
|
except Exception as e:
|
|
148
156
|
logger.error(f"Error processing audio: {str(e)}")
|
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
import os
|
|
2
|
+
from io import BytesIO
|
|
3
|
+
from urllib.parse import urlparse
|
|
4
|
+
|
|
5
|
+
import aiohttp
|
|
6
|
+
import docx
|
|
7
|
+
from bs4 import BeautifulSoup
|
|
8
|
+
from readability import Document
|
|
9
|
+
|
|
10
|
+
from content_core.common import ProcessSourceState
|
|
11
|
+
from content_core.common.types import warn_if_deprecated_engine
|
|
12
|
+
from content_core.logging import logger
|
|
13
|
+
from content_core.processors.pdf import SUPPORTED_FITZ_TYPES
|
|
14
|
+
|
|
15
|
+
DOCX_MIME_TYPE = (
|
|
16
|
+
"application/vnd.openxmlformats-officedocument.wordprocessingml.document"
|
|
17
|
+
)
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
async def _extract_docx_content(docx_bytes: bytes, url: str):
|
|
21
|
+
"""
|
|
22
|
+
Extract content from DOCX file bytes.
|
|
23
|
+
"""
|
|
24
|
+
try:
|
|
25
|
+
logger.debug(f"Attempting to parse DOCX from URL: {url} with python-docx")
|
|
26
|
+
doc = docx.Document(BytesIO(docx_bytes))
|
|
27
|
+
content_parts = [p.text for p in doc.paragraphs if p.text]
|
|
28
|
+
full_content = "\n\n".join(content_parts)
|
|
29
|
+
|
|
30
|
+
# Try to get a title from document properties or first heading
|
|
31
|
+
title = doc.core_properties.title
|
|
32
|
+
if not title and doc.paragraphs:
|
|
33
|
+
# Look for a potential title in the first few paragraphs (e.g., if styled as heading)
|
|
34
|
+
for p in doc.paragraphs[:5]: # Check first 5 paragraphs
|
|
35
|
+
if p.style.name.startswith("Heading"):
|
|
36
|
+
title = p.text
|
|
37
|
+
break
|
|
38
|
+
if not title: # Fallback to first line if no heading found
|
|
39
|
+
title = (
|
|
40
|
+
doc.paragraphs[0].text.strip()
|
|
41
|
+
if doc.paragraphs[0].text.strip()
|
|
42
|
+
else None
|
|
43
|
+
)
|
|
44
|
+
|
|
45
|
+
# If no title found, use filename from URL
|
|
46
|
+
if not title:
|
|
47
|
+
title = urlparse(url).path.split("/")[-1]
|
|
48
|
+
|
|
49
|
+
logger.info(f"Successfully extracted content from DOCX: {url}, Title: {title}")
|
|
50
|
+
return {
|
|
51
|
+
"title": title,
|
|
52
|
+
"content": full_content,
|
|
53
|
+
"domain": urlparse(url).netloc,
|
|
54
|
+
"url": url,
|
|
55
|
+
}
|
|
56
|
+
except Exception as e:
|
|
57
|
+
logger.error(f"Failed to process DOCX content from {url}: {e}")
|
|
58
|
+
# Fallback or re-raise, depending on desired error handling
|
|
59
|
+
return {
|
|
60
|
+
"title": f"Error Processing DOCX: {urlparse(url).path.split('/')[-1]}",
|
|
61
|
+
"content": f"Failed to extract content from DOCX: {e}",
|
|
62
|
+
"domain": urlparse(url).netloc,
|
|
63
|
+
"url": url,
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
async def url_provider(state: ProcessSourceState):
|
|
68
|
+
"""
|
|
69
|
+
Identify the provider
|
|
70
|
+
"""
|
|
71
|
+
return_dict = {}
|
|
72
|
+
url = state.url
|
|
73
|
+
if url:
|
|
74
|
+
if "youtube.com" in url or "youtu.be" in url:
|
|
75
|
+
return_dict["identified_type"] = "youtube"
|
|
76
|
+
else:
|
|
77
|
+
# remote URL: check content-type to catch PDFs
|
|
78
|
+
try:
|
|
79
|
+
async with aiohttp.ClientSession() as session:
|
|
80
|
+
async with session.head(
|
|
81
|
+
url, timeout=10, allow_redirects=True
|
|
82
|
+
) as resp:
|
|
83
|
+
mime = resp.headers.get("content-type", "").split(";", 1)[0]
|
|
84
|
+
except Exception as e:
|
|
85
|
+
logger.debug(f"HEAD check failed for {url}: {e}")
|
|
86
|
+
mime = "article"
|
|
87
|
+
if mime in SUPPORTED_FITZ_TYPES:
|
|
88
|
+
return_dict["identified_type"] = mime
|
|
89
|
+
else:
|
|
90
|
+
return_dict["identified_type"] = "article"
|
|
91
|
+
return return_dict
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
async def extract_url_bs4(url: str) -> dict:
|
|
95
|
+
"""
|
|
96
|
+
Get the title and content of a URL using readability with a fallback to BeautifulSoup.
|
|
97
|
+
|
|
98
|
+
Args:
|
|
99
|
+
url (str): The URL of the webpage to extract content from.
|
|
100
|
+
|
|
101
|
+
Returns:
|
|
102
|
+
dict: A dictionary containing the 'title' and 'content' of the webpage.
|
|
103
|
+
"""
|
|
104
|
+
async with aiohttp.ClientSession() as session:
|
|
105
|
+
try:
|
|
106
|
+
# Fetch the webpage content
|
|
107
|
+
async with session.get(url, timeout=10) as response:
|
|
108
|
+
if response.status != 200:
|
|
109
|
+
raise Exception(f"HTTP error: {response.status}")
|
|
110
|
+
html = await response.text()
|
|
111
|
+
|
|
112
|
+
# Try extracting with readability
|
|
113
|
+
try:
|
|
114
|
+
doc = Document(html)
|
|
115
|
+
title = doc.title() or "No title found"
|
|
116
|
+
# Extract content as plain text by parsing the cleaned HTML
|
|
117
|
+
soup = BeautifulSoup(doc.summary(), "lxml")
|
|
118
|
+
content = soup.get_text(separator=" ", strip=True)
|
|
119
|
+
if not content.strip():
|
|
120
|
+
raise ValueError("No content extracted by readability")
|
|
121
|
+
except Exception as e:
|
|
122
|
+
print(f"Readability failed: {e}")
|
|
123
|
+
# Fallback to BeautifulSoup
|
|
124
|
+
soup = BeautifulSoup(html, "lxml")
|
|
125
|
+
# Extract title
|
|
126
|
+
title_tag = (
|
|
127
|
+
soup.find("title")
|
|
128
|
+
or soup.find("h1")
|
|
129
|
+
or soup.find("meta", property="og:title")
|
|
130
|
+
)
|
|
131
|
+
title = (
|
|
132
|
+
title_tag.get_text(strip=True) if title_tag else "No title found"
|
|
133
|
+
)
|
|
134
|
+
# Extract content from common content tags
|
|
135
|
+
content_tags = soup.select(
|
|
136
|
+
'article, .content, .post, main, [role="main"], div[class*="content"], div[class*="article"]'
|
|
137
|
+
)
|
|
138
|
+
content = (
|
|
139
|
+
" ".join(
|
|
140
|
+
tag.get_text(separator=" ", strip=True) for tag in content_tags
|
|
141
|
+
)
|
|
142
|
+
if content_tags
|
|
143
|
+
else soup.get_text(separator=" ", strip=True)
|
|
144
|
+
)
|
|
145
|
+
content = content.strip() or "No content found"
|
|
146
|
+
|
|
147
|
+
return {
|
|
148
|
+
"title": title,
|
|
149
|
+
"content": content,
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
except Exception as e:
|
|
153
|
+
print(f"Error processing URL {url}: {e}")
|
|
154
|
+
return {
|
|
155
|
+
"title": "Error",
|
|
156
|
+
"content": f"Failed to extract content: {str(e)}",
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
async def extract_url_jina(url: str):
|
|
161
|
+
"""
|
|
162
|
+
Get the content of a URL using Jina. Uses Bearer token if JINA_API_KEY is set.
|
|
163
|
+
"""
|
|
164
|
+
headers = {}
|
|
165
|
+
api_key = os.environ.get("JINA_API_KEY")
|
|
166
|
+
if api_key:
|
|
167
|
+
headers["Authorization"] = f"Bearer {api_key}"
|
|
168
|
+
async with aiohttp.ClientSession() as session:
|
|
169
|
+
async with session.get(f"https://r.jina.ai/{url}", headers=headers) as response:
|
|
170
|
+
text = await response.text()
|
|
171
|
+
if text.startswith("Title:") and "\n" in text:
|
|
172
|
+
title_end = text.index("\n")
|
|
173
|
+
title = text[6:title_end].strip()
|
|
174
|
+
content = text[title_end + 1 :].strip()
|
|
175
|
+
logger.debug(
|
|
176
|
+
f"Processed url: {url}, found title: {title}, content: {content[:100]}..."
|
|
177
|
+
)
|
|
178
|
+
return {"title": title, "content": content}
|
|
179
|
+
else:
|
|
180
|
+
logger.debug(
|
|
181
|
+
f"Processed url: {url}, does not have Title prefix, returning full content: {text[:100]}..."
|
|
182
|
+
)
|
|
183
|
+
return {"content": text}
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
async def extract_url_firecrawl(url: str):
|
|
187
|
+
"""
|
|
188
|
+
Get the content of a URL using Firecrawl.
|
|
189
|
+
Returns {"title": ..., "content": ...} or None on failure.
|
|
190
|
+
"""
|
|
191
|
+
try:
|
|
192
|
+
from firecrawl import AsyncFirecrawlApp
|
|
193
|
+
|
|
194
|
+
app = AsyncFirecrawlApp(api_key=os.environ.get("FIRECRAWL_API_KEY"))
|
|
195
|
+
scrape_result = await app.scrape_url(url, formats=["markdown", "html"])
|
|
196
|
+
return {
|
|
197
|
+
"title": scrape_result.metadata["title"] or scrape_result.title,
|
|
198
|
+
"content": scrape_result.markdown,
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
except Exception as e:
|
|
202
|
+
logger.error(f"Firecrawl extraction error for URL: {url}: {e}")
|
|
203
|
+
return None
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
async def extract_url(state: ProcessSourceState):
|
|
207
|
+
"""
|
|
208
|
+
Extract content from a URL using the engine specified in the state.
|
|
209
|
+
Supported engines: 'auto', 'simple', 'legacy' (deprecated), 'firecrawl', 'jina'.
|
|
210
|
+
"""
|
|
211
|
+
assert state.url, "No URL provided"
|
|
212
|
+
url = state.url
|
|
213
|
+
engine = state.engine or "auto"
|
|
214
|
+
warn_if_deprecated_engine(engine)
|
|
215
|
+
try:
|
|
216
|
+
if engine == "auto":
|
|
217
|
+
if os.environ.get("FIRECRAWL_API_KEY"):
|
|
218
|
+
logger.debug(
|
|
219
|
+
"Engine 'auto' selected: using Firecrawl (FIRECRAWL_API_KEY detected)"
|
|
220
|
+
)
|
|
221
|
+
return await extract_url_firecrawl(url)
|
|
222
|
+
else:
|
|
223
|
+
try:
|
|
224
|
+
logger.debug("Trying to use Jina to extract URL")
|
|
225
|
+
return await extract_url_jina(url)
|
|
226
|
+
except Exception as e:
|
|
227
|
+
logger.error(f"Jina extraction error for URL: {url}: {e}")
|
|
228
|
+
logger.debug("Falling back to BeautifulSoup")
|
|
229
|
+
return await extract_url_bs4(url)
|
|
230
|
+
elif engine == "simple" or engine == "legacy":
|
|
231
|
+
# 'legacy' is deprecated alias for 'simple'
|
|
232
|
+
return await extract_url_bs4(url)
|
|
233
|
+
elif engine == "firecrawl":
|
|
234
|
+
return await extract_url_firecrawl(url)
|
|
235
|
+
elif engine == "jina":
|
|
236
|
+
return await extract_url_jina(url)
|
|
237
|
+
elif engine == "docling":
|
|
238
|
+
from content_core.processors.docling import extract_with_docling
|
|
239
|
+
|
|
240
|
+
state.url = url
|
|
241
|
+
result_state = await extract_with_docling(state)
|
|
242
|
+
return {"title": None, "content": result_state.content}
|
|
243
|
+
else:
|
|
244
|
+
raise ValueError(f"Unknown engine: {engine}")
|
|
245
|
+
except Exception as e:
|
|
246
|
+
logger.error(f"URL extraction failed for URL: {url}")
|
|
247
|
+
logger.exception(e)
|
|
248
|
+
return None
|