rastof9 commited on
Commit
d6e387b
·
1 Parent(s): 4883166

new update

Browse files
Files changed (2) hide show
  1. app.py +1 -2
  2. app/services/ai_processor.py +17 -4
app.py CHANGED
@@ -6,8 +6,7 @@ from config import get_config
6
  migrate = Migrate()
7
 
8
  def create_flask_app():
9
- app = create_app()
10
- app.config.from_object(get_config())
11
  migrate.init_app(app, db)
12
  return app
13
 
 
6
  migrate = Migrate()
7
 
8
  def create_flask_app():
9
+ app = create_app(get_config())
 
10
  migrate.init_app(app, db)
11
  return app
12
 
app/services/ai_processor.py CHANGED
@@ -12,16 +12,15 @@ class ProcessingError(Exception):
12
 
13
  class AIPipeline:
14
  def __init__(self):
 
 
15
  try:
16
- self.nlp = pipeline("text-classification", model="roberta-base")
17
-
18
  model_dir = Path("app/models")
19
  weights_path = model_dir / "yolov4.weights"
20
  config_path = model_dir / "yolov4.cfg"
21
 
22
  if not (weights_path.exists() and config_path.exists()):
23
  logger.warning("YOLOv4 files not found. Please run setup_yolo.py first.")
24
- self.detector = None
25
  else:
26
  self.detector = cv2.dnn.readNet(str(weights_path), str(config_path))
27
 
@@ -29,6 +28,17 @@ class AIPipeline:
29
  logger.error(f"Error initializing AI Pipeline: {e}")
30
  raise
31
 
 
 
 
 
 
 
 
 
 
 
 
32
  def process_ad(self, ad):
33
  if not ad:
34
  raise ValueError("Ad content cannot be empty")
@@ -45,8 +55,11 @@ class AIPipeline:
45
  raise ProcessingError(f"Failed to process ad: {str(e)}")
46
 
47
  def _analyze_sentiment(self, text):
 
 
48
  try:
49
- return self.nlp(text)[0] if text else None
 
50
  except Exception as e:
51
  logger.error(f"Sentiment analysis error: {e}")
52
  return None
 
12
 
13
  class AIPipeline:
14
  def __init__(self):
15
+ self.nlp = None # Initialize as None
16
+ self.detector = None
17
  try:
 
 
18
  model_dir = Path("app/models")
19
  weights_path = model_dir / "yolov4.weights"
20
  config_path = model_dir / "yolov4.cfg"
21
 
22
  if not (weights_path.exists() and config_path.exists()):
23
  logger.warning("YOLOv4 files not found. Please run setup_yolo.py first.")
 
24
  else:
25
  self.detector = cv2.dnn.readNet(str(weights_path), str(config_path))
26
 
 
28
  logger.error(f"Error initializing AI Pipeline: {e}")
29
  raise
30
 
31
+ def _ensure_nlp_loaded(self):
32
+ """Ensure NLP model is loaded before use."""
33
+ if self.nlp is None:
34
+ try:
35
+ logger.info("Loading NLP model...")
36
+ self.nlp = pipeline("text-classification", model="roberta-base")
37
+ logger.info("NLP model loaded successfully")
38
+ except Exception as e:
39
+ logger.error(f"Error loading NLP model: {e}")
40
+ raise
41
+
42
  def process_ad(self, ad):
43
  if not ad:
44
  raise ValueError("Ad content cannot be empty")
 
55
  raise ProcessingError(f"Failed to process ad: {str(e)}")
56
 
57
  def _analyze_sentiment(self, text):
58
+ if not text:
59
+ return None
60
  try:
61
+ self._ensure_nlp_loaded() # Load model if needed
62
+ return self.nlp(text)[0]
63
  except Exception as e:
64
  logger.error(f"Sentiment analysis error: {e}")
65
  return None