Skip to content

Commit be37e61

Browse files
committed
Changed the handling of filenames
1 parent 544fe1e commit be37e61

File tree

3 files changed

+31
-6
lines changed

3 files changed

+31
-6
lines changed

src/entity_graph_api/config.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ def setup_logger(name: str) -> logging.Logger:
3636

3737
URL = Annotated[AnyUrl, AfterValidator(str)]
3838

39+
UPLOAD_DIR = "/app/uploads"
40+
3941
# Custom StrEnum is more handy than using integers that represent logging levels
4042
class LoggingLevels(StrEnum):
4143
DEBUG = "DEBUG"
@@ -44,8 +46,6 @@ class LoggingLevels(StrEnum):
4446
ERROR = "ERROR"
4547
CRITICAL = "CRITICAL"
4648

47-
48-
4949
class Settings(BaseSettings):
5050
model_config = SettingsConfigDict(
5151
env_file=(".env"), env_nested_delimiter="__", extra="ignore"
Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,38 @@
1+
import os
12
from typing import List, Dict, Any
2-
from pydantic import BaseModel
3+
from pydantic import BaseModel, field_validator
4+
from src.entity_graph_api.config import UPLOAD_DIR
35

46
class LoadTableRequest(BaseModel):
57
config: Dict[str, Any] | str
68
filename: str
79
table_name: str
810
instance_type: str
911

12+
@field_validator('config')
13+
@classmethod
14+
def config_must_have_filename(cls, v):
15+
if isinstance(v, dict):
16+
filename = v.get("filename")
17+
if not filename:
18+
raise ValueError('config dictionary must contain a "filename" field')
19+
full_path = os.path.join(UPLOAD_DIR, filename)
20+
if not os.path.exists(full_path):
21+
raise ValueError(f'File not found: {full_path}')
22+
v["filename"] = full_path
23+
return v
24+
if isinstance(v, str):
25+
full_path = os.path.join(UPLOAD_DIR, v)
26+
if not os.path.exists(full_path):
27+
raise ValueError(f'File not found: {full_path}')
28+
return full_path
29+
return v
30+
31+
class LoadTableRequestWithString(BaseModel):
32+
config: str
33+
filename: str
34+
table_name: str
35+
instance_type: str
36+
1037
class ExtractEntitiesRequest(BaseModel):
1138
steps: Dict[str, List[Any]]

src/entity_graph_api/routers/entity_graph.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import os
2+
from src.entity_graph_api.config import UPLOAD_DIR
23
from fastapi import APIRouter, UploadFile, File, HTTPException
34
from src.entity_graph_api.models.entity_graph import LoadTableRequest, ExtractEntitiesRequest
45
from entity_graph.graph_extractor.entities_graph_extractor import EntitiesGraphExtractor
56

67
extractor = EntitiesGraphExtractor()
78
entities = extractor.entities_graph_manager.entities
89

9-
UPLOAD_DIR = "/app/uploads"
10-
1110
os.makedirs(UPLOAD_DIR, exist_ok=True)
1211

1312
router = APIRouter(
@@ -47,7 +46,6 @@ async def load_table(request: LoadTableRequest):
4746
except Exception as e:
4847
raise HTTPException(status_code=500, detail=f"Failed to load table: {e}")
4948

50-
5149
@router.post("/extract-entities")
5250
async def extract_entities(request: ExtractEntitiesRequest):
5351
"""Extract entities and build the graph based on the provided steps."""

0 commit comments

Comments
 (0)