File tree Expand file tree Collapse file tree 6 files changed +96
-0
lines changed Expand file tree Collapse file tree 6 files changed +96
-0
lines changed Original file line number Diff line number Diff line change 1+ AZURE_AI_VISION_API_KEY="<your-api-key>"
2+ AZURE_AI_VISION_ENDPOINT="<your-endpoint>"
Original file line number Diff line number Diff line change 11from fastapi import FastAPI
22from fastapi .openapi .utils import get_openapi
33
4+ from backend .routers import azure_ai_vision as azure_ai_vision_router
45from backend .routers import azure_openai as azure_openai_router
56from backend .routers import azure_storage as azure_storage_router
67from backend .routers import document_intelligence as document_intelligence_router
1213app .include_router (azure_openai_router .router )
1314app .include_router (document_intelligence_router .router )
1415app .include_router (azure_storage_router .router )
16+ app .include_router (azure_ai_vision_router .router )
1517
1618
1719def custom_openapi ():
Original file line number Diff line number Diff line change 1+ from logging import getLogger
2+
3+ from azure .ai .vision .imageanalysis import ImageAnalysisClient
4+ from azure .ai .vision .imageanalysis .models import VisualFeatures
5+ from azure .core .credentials import AzureKeyCredential
6+
7+ from backend .settings .azure_ai_vision import Settings
8+
9+ logger = getLogger (__name__ )
10+
11+
12+ class AzureAiVisionClient :
13+ def __init__ (self , settings : Settings ) -> None :
14+ self .settings = settings
15+
16+ def get_image_analysis_client (self ) -> ImageAnalysisClient :
17+ return ImageAnalysisClient (
18+ endpoint = self .settings .azure_ai_vision_endpoint ,
19+ credential = AzureKeyCredential (self .settings .azure_ai_vision_api_key ),
20+ )
21+
22+ def analyze_image (
23+ self ,
24+ image : bytes ,
25+ ) -> dict :
26+ image_analysis_client = self .get_image_analysis_client ()
27+ result = image_analysis_client .analyze (
28+ image_data = image ,
29+ visual_features = [
30+ VisualFeatures .CAPTION ,
31+ VisualFeatures .READ ,
32+ ],
33+ )
34+ logger .info ("Analyzed image" )
35+ return result .as_dict ()
Original file line number Diff line number Diff line change 1+ from logging import getLogger
2+
3+ from fastapi import APIRouter , UploadFile
4+
5+ from backend .internals import azure_ai_vision
6+ from backend .schemas import azure_ai_vision as azure_ai_vision_schemas
7+ from backend .settings .azure_ai_vision import Settings
8+
9+ logger = getLogger (__name__ )
10+ client = azure_ai_vision .AzureAiVisionClient (
11+ settings = Settings (),
12+ )
13+
14+ router = APIRouter (
15+ prefix = "/azure_ai_vision" ,
16+ tags = ["azure_ai_vision" ],
17+ responses = {404 : {"description" : "Not found" }},
18+ )
19+
20+
21+ @router .post (
22+ "/image/analyze/" ,
23+ response_model = azure_ai_vision_schemas .ImageAnalysisResponse ,
24+ status_code = 200 ,
25+ )
26+ async def analyze_image (file : UploadFile ):
27+ try :
28+ content = await file .read ()
29+ result = client .analyze_image (
30+ image = content ,
31+ )
32+ except Exception as e :
33+ logger .error (f"Failed to analyze image: { e } " )
34+ raise
35+ return azure_ai_vision_schemas .ImageAnalysisResponse (
36+ result = result ,
37+ )
Original file line number Diff line number Diff line change 1+ from logging import getLogger
2+
3+ from pydantic import BaseModel
4+
5+ logger = getLogger (__name__ )
6+
7+
8+ class ImageAnalysisResponse (BaseModel ):
9+ result : dict
Original file line number Diff line number Diff line change 1+ from pydantic_settings import BaseSettings , SettingsConfigDict
2+
3+
4+ class Settings (BaseSettings ):
5+ azure_ai_vision_endpoint : str = "https://<name>.cognitiveservices.azure.com/"
6+ azure_ai_vision_api_key : str = "<api-key>"
7+
8+ model_config = SettingsConfigDict (
9+ env_file = "azure_ai_vision.env" ,
10+ env_file_encoding = "utf-8" ,
11+ )
You can’t perform that action at this time.
0 commit comments