11import logging
22from fastapi import APIRouter , HTTPException , UploadFile , File , Body , Request
3- from typing import Optional , List
4- import json
3+ from typing import List
54
6- from app .services .heuristic_engine import HeuristicEvaluationEngine
7- from app .core .config import settings , ALLOWED_IMAGE_TYPES
8- from app .services .exceptions import InvalidInputError
5+ from app .core .config import ALLOWED_IMAGE_TYPES
6+ from app .services .exceptions import InvalidInputError , ModelInferenceError , RAGKnowledgeBaseError
7+ from app .services .omniparser_client import UIElement
8+ from app .core .constants import HeuristicId , NIELSEN_HEURISTICS
99
1010logger = logging .getLogger (__name__ )
1111router = APIRouter ()
@@ -55,12 +55,13 @@ async def evaluate_heuristics(
5555 detection_client = request .app .state .omniparser_client
5656 contents = await image .read ()
5757
58- detection_result = await detection_client .detect_elements (contents )
59-
60- # Initialize evaluation engine and evaluate
61- evaluation_engine = HeuristicEvaluationEngine ()
62- await evaluation_engine .initialize ()
58+ detection_result = await detection_client .detect_elements (
59+ contents ,
60+ content_type = content_type
61+ )
6362
63+ # Use singleton evaluation engine and evaluate
64+ evaluation_engine = request .app .state .heuristic_engine
6465 evaluation_result = await evaluation_engine .evaluate_interface (detection_result )
6566
6667 return {
@@ -82,6 +83,7 @@ async def evaluate_heuristics(
8283
8384@router .post ("/evaluate-legacy/{heuristic_id}" )
8485async def evaluate_legacy_format (
86+ request : Request ,
8587 heuristic_id : str ,
8688 elements : List [dict ] = Body (...)
8789):
@@ -100,7 +102,6 @@ async def evaluate_legacy_format(
100102 500: Unexpected server error
101103 """
102104 try :
103- from app .core .constants import HeuristicId
104105
105106 # Normalize heuristic_id to uppercase
106107 normalized_id = heuristic_id .upper ()
@@ -113,10 +114,8 @@ async def evaluate_legacy_format(
113114 }
114115 )
115116
116- evaluation_engine = HeuristicEvaluationEngine ()
117- await evaluation_engine .initialize ()
117+ evaluation_engine = request .app .state .heuristic_engine
118118
119- from app .services .omniparser_client import UIElement
120119 try :
121120 ui_elements = [UIElement .from_dict (e ) for e in elements ]
122121 except Exception as e :
@@ -168,8 +167,6 @@ async def evaluate_legacy_format(
168167
169168@router .get ("/heuristics" )
170169async def get_heuristics ():
171- from app .core .constants import NIELSEN_HEURISTICS
172-
173170 return {
174171 "success" : True ,
175172 "data" : {
@@ -183,7 +180,7 @@ async def get_heuristics():
183180 }
184181
185182@router .get ("/knowledge-base/stats" )
186- async def get_knowledge_base_stats ():
183+ async def get_knowledge_base_stats (request : Request ):
187184 """Get statistics about the RAG knowledge base.
188185
189186 Returns:
@@ -194,10 +191,7 @@ async def get_knowledge_base_stats():
194191 500: Unexpected server error
195192 """
196193 try :
197- from app .services .rag_knowledge_base import RAGKnowledgeBase
198-
199- kb = RAGKnowledgeBase ()
200- await kb .initialize ()
194+ kb = request .app .state .rag_knowledge_base
201195 stats = await kb .get_stats ()
202196
203197 return {
0 commit comments