Spaces:
Build error
Build error
Ilia Tambovtsev commited on
Commit ·
adf2c4a
1
Parent(s): 9324958
feat: return chain outputs
Browse files- src/chains/pipelines.py +19 -9
src/chains/pipelines.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
from typing import List, Dict, Any, Optional
|
| 2 |
from pydantic import BaseModel, Field
|
| 3 |
from pathlib import Path
|
| 4 |
import json
|
|
@@ -67,6 +67,7 @@ class SingleSlidePipeline(Chain):
|
|
| 67 |
llm: Optional[ChatOpenAI] = None,
|
| 68 |
vision_prompt: str = "Describe this slide in detail",
|
| 69 |
dpi: int = 72,
|
|
|
|
| 70 |
**kwargs
|
| 71 |
):
|
| 72 |
"""Initialize pipeline for single slide processing
|
|
@@ -75,16 +76,16 @@ class SingleSlidePipeline(Chain):
|
|
| 75 |
llm: Language model with vision capabilities
|
| 76 |
vision_prompt: Prompt for slide analysis
|
| 77 |
dpi: Resolution for PDF rendering
|
|
|
|
| 78 |
"""
|
| 79 |
super().__init__(**kwargs)
|
| 80 |
-
|
| 81 |
-
# Create processing pipeline using pipe operator
|
| 82 |
self._chain = (
|
| 83 |
LoadPageChain()
|
| 84 |
| Page2ImageChain(default_dpi=dpi)
|
| 85 |
| ImageEncodeChain()
|
| 86 |
| VisionAnalysisChain(llm=llm, prompt=vision_prompt)
|
| 87 |
)
|
|
|
|
| 88 |
|
| 89 |
@property
|
| 90 |
def input_keys(self) -> List[str]:
|
|
@@ -94,7 +95,10 @@ class SingleSlidePipeline(Chain):
|
|
| 94 |
@property
|
| 95 |
def output_keys(self) -> List[str]:
|
| 96 |
"""Output keys provided by the chain"""
|
| 97 |
-
|
|
|
|
|
|
|
|
|
|
| 98 |
|
| 99 |
def _call(
|
| 100 |
self,
|
|
@@ -109,17 +113,23 @@ class SingleSlidePipeline(Chain):
|
|
| 109 |
- page_num: Page number to process
|
| 110 |
|
| 111 |
Returns:
|
| 112 |
-
Dictionary with SlideAnalysis object
|
| 113 |
"""
|
| 114 |
-
|
| 115 |
-
|
|
|
|
| 116 |
slide_analysis=SlideAnalysis(
|
| 117 |
page_num=inputs["page_num"],
|
| 118 |
-
vision_prompt=
|
| 119 |
-
content=
|
| 120 |
)
|
| 121 |
)
|
| 122 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
|
| 124 |
class PresentationPipeline(Chain):
|
| 125 |
"""Pipeline for processing entire PDF presentation"""
|
|
|
|
| 1 |
+
from typing import List, Dict, Any, Optional, Tuple, Union
|
| 2 |
from pydantic import BaseModel, Field
|
| 3 |
from pathlib import Path
|
| 4 |
import json
|
|
|
|
| 67 |
llm: Optional[ChatOpenAI] = None,
|
| 68 |
vision_prompt: str = "Describe this slide in detail",
|
| 69 |
dpi: int = 72,
|
| 70 |
+
return_steps: bool = False,
|
| 71 |
**kwargs
|
| 72 |
):
|
| 73 |
"""Initialize pipeline for single slide processing
|
|
|
|
| 76 |
llm: Language model with vision capabilities
|
| 77 |
vision_prompt: Prompt for slide analysis
|
| 78 |
dpi: Resolution for PDF rendering
|
| 79 |
+
return_steps: Whether to return intermediate chain outputs
|
| 80 |
"""
|
| 81 |
super().__init__(**kwargs)
|
|
|
|
|
|
|
| 82 |
self._chain = (
|
| 83 |
LoadPageChain()
|
| 84 |
| Page2ImageChain(default_dpi=dpi)
|
| 85 |
| ImageEncodeChain()
|
| 86 |
| VisionAnalysisChain(llm=llm, prompt=vision_prompt)
|
| 87 |
)
|
| 88 |
+
self._return_steps = return_steps
|
| 89 |
|
| 90 |
@property
|
| 91 |
def input_keys(self) -> List[str]:
|
|
|
|
| 95 |
@property
|
| 96 |
def output_keys(self) -> List[str]:
|
| 97 |
"""Output keys provided by the chain"""
|
| 98 |
+
keys = ["slide_analysis"]
|
| 99 |
+
if self._return_steps:
|
| 100 |
+
keys.append("chain_outputs")
|
| 101 |
+
return keys
|
| 102 |
|
| 103 |
def _call(
|
| 104 |
self,
|
|
|
|
| 113 |
- page_num: Page number to process
|
| 114 |
|
| 115 |
Returns:
|
| 116 |
+
Dictionary with SlideAnalysis object and optionally chain outputs
|
| 117 |
"""
|
| 118 |
+
chain_outputs = self._chain.invoke(inputs)
|
| 119 |
+
|
| 120 |
+
result = dict(
|
| 121 |
slide_analysis=SlideAnalysis(
|
| 122 |
page_num=inputs["page_num"],
|
| 123 |
+
vision_prompt=chain_outputs["vision_prompt"],
|
| 124 |
+
content=chain_outputs["llm_output"]
|
| 125 |
)
|
| 126 |
)
|
| 127 |
|
| 128 |
+
if self._return_steps:
|
| 129 |
+
result["chain_outputs"] = chain_outputs
|
| 130 |
+
|
| 131 |
+
return result
|
| 132 |
+
|
| 133 |
|
| 134 |
class PresentationPipeline(Chain):
|
| 135 |
"""Pipeline for processing entire PDF presentation"""
|