Skip to content

Commit 39c8ca0

Browse files
Fix extract-anything example (#60)
1 parent 9dcaca7 commit 39c8ca0

10 files changed

Lines changed: 8880 additions & 480 deletions

File tree

extract-anything/backend/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ description = "Add your description here"
55
readme = "README.md"
66
requires-python = ">=3.10"
77
dependencies = [
8-
"baml-py==0.80.1",
8+
"baml-py==0.215.0",
99
"fastapi[standard]>=0.115.11",
1010
"httpx>=0.28.1",
1111
"pdf2image>=1.17.0",

extract-anything/backend/server.py

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import json
33
import base64
44
from typing import Any, Callable, Optional, TypeVar
5-
from baml_py import BamlStream, Image
5+
from baml_py import BamlStream, Image, Pdf
66

77
import httpx
88
from fastapi import FastAPI, UploadFile, File, Form, HTTPException
@@ -13,9 +13,6 @@
1313
from fastapi.middleware.cors import CORSMiddleware
1414
from baml_client.types import Schema
1515
from baml_py.errors import BamlError
16-
from pdf2image import convert_from_bytes
17-
from PIL import Image as PILImage
18-
import io
1916

2017
app = FastAPI()
2118

@@ -90,7 +87,7 @@ async def execute_baml(
9087
url: str = Form(None),
9188
baml_code: str = Form(...),
9289
return_type: str = Form(...),
93-
):
90+
):
9491
final_content = await read_input_content(file, content, url)
9592
tb = TypeBuilder()
9693
try:
@@ -130,16 +127,11 @@ async def stream_baml():
130127
yield json.dumps({ "error": str(e) }) + "\n\n"
131128
return StreamingResponse(stream_baml(), media_type="text/event-stream")
132129

133-
def convert_to_base64(img: PILImage):
134-
buffered = io.BytesIO()
135-
img.save(buffered, format="JPEG")
136-
return Image.from_base64(base64=base64.b64encode(buffered.getvalue()).decode("utf-8"), media_type="image/jpeg")
137-
138130
async def read_input_content(
139131
file: Optional[UploadFile] = None,
140132
content: Optional[str] = None,
141133
url: Optional[str] = None
142-
) -> str | Image | list[Image]:
134+
) -> str | Image | Pdf:
143135
"""
144136
Processes the input from one of the following:
145137
- file: an uploaded file (image, audio, PDF or text)
@@ -156,10 +148,9 @@ async def read_input_content(
156148
file_content = await file.read()
157149
return file_content.decode("utf-8")
158150
elif file.content_type == "application/pdf":
159-
# Convert PDF to images
160151
file_content = await file.read()
161-
images = convert_from_bytes(file_content)
162-
return [convert_to_base64(img) for img in images]
152+
file_content_base64 = base64.b64encode(file_content).decode("utf-8")
153+
return Pdf.from_base64(file_content_base64)
163154
else:
164155
file_content = await file.read()
165156
file_content_base64 = base64.b64encode(file_content).decode("utf-8")

extract-anything/backend/uv.lock

Lines changed: 454 additions & 452 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

extract-anything/baml_src/execute_baml.baml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ class Response {
33
@@dynamic
44
}
55

6-
function ExecuteBAML(content: string | image | audio | image[]) -> Response {
6+
function ExecuteBAML(content: string | image | audio | pdf) -> Response {
77
client "openai/gpt-4o"
88
prompt #"
99
Extract the data from the given content.
@@ -22,11 +22,11 @@ test vaibhav_resume {
2222
class Person {
2323
name string @description("The full name of the individual")
2424
email string @description("The email address of the individual")
25-
25+
2626
experience Experience[]
2727
skills string[]
2828
}
29-
29+
3030
class Experience {
3131
position string @description("The role held by the individual")
3232
company string @description("The company where the experience was gained")

extract-anything/baml_src/generate_baml.baml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ class Schema {
1515
other_code string
1616
}
1717

18-
function GenerateBAML(content: string | image | audio | image[]) -> Schema @stream.not_null {
19-
client CustomSonnet
18+
function GenerateBAML(content: string | image | audio | pdf) -> Schema @stream.not_null {
19+
client CustomGPT4o
2020
prompt #"
2121
Generate BAML schema for the given content.
2222

2323
{{ BAMLBackground() }}
2424

25-
{{ ctx.output_format(prefix="Answer with this format:\n") }}
25+
{{ ctx.output_format }}
2626

2727
{{ _.role('user') }}
2828
{{ content }}
@@ -41,17 +41,17 @@ template_string BAMLBackground() ##"
4141
// Optional string fields use ?
4242
// @description is optional, but if you include it, it goes after the field.
4343
name string? @description("The name of the object")
44-
44+
4545
// Arrays of primitives
4646
// arrays cannot be optional.
4747
tags string[]
48-
48+
4949
// Enums must be declared separately and are optional
5050
status MyEnum?
51-
51+
5252
// Union types
5353
type "success" | "error"
54-
54+
5555
// Primitive types
5656
count int
5757
enabled bool

extract-anything/baml_src/generators.baml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ generator py_target {
1010

1111
// The version of the BAML package you have installed (e.g. same version as your baml-py or @boundaryml/baml).
1212
// The BAML VSCode extension version should also match this version.
13-
version "0.207.1"
13+
version "0.215.0"
1414

1515
// Valid values: "sync", "async"
1616
// This controls what `b.FunctionName()` will be (sync or async).
@@ -26,7 +26,7 @@ generator ts_target {
2626

2727
// The version of the BAML package you have installed (e.g. same version as your baml-py or @boundaryml/baml).
2828
// The BAML VSCode extension version should also match this version.
29-
version "0.207.1"
29+
version "0.215.0"
3030

3131
// Valid values: "sync", "async"
3232
// This controls what `b.FunctionName()` will be (sync or async).

extract-anything/bill.pdf

21.9 KB
Binary file not shown.

extract-anything/bill.png

171 KB
Loading

extract-anything/frontend/components/error-message.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ interface ErrorMessageProps {
77
export function ErrorMessage({ error }: ErrorMessageProps) {
88
if (!error) return null
99

10-
return <div className="mt-6 p-4 bg-foreground text-white rounded-md">
10+
return <div className="mt-6 p-4 bg-red-500 text-white rounded-md">
1111
{/* {JSON.stringify(error)} */}
1212
<pre>
1313
<AnsiColorText text={error} />

0 commit comments

Comments
 (0)