Download app/services/ai_processor.py from rastof9/ads: direct link, hf CLI and curl.
- Browser
- Download file 878 Bytes
-
https://huggingface.co/spaces/rastof9/ads/resolve/main/app/services/ai_processor.py
- Command line
-
hf download hf://spaces/rastof9/ads/app/services/ai_processor.py
-
curl -L -o ai_processor.py https://huggingface.co/spaces/rastof9/ads/resolve/main/app/services/ai_processor.py
878 Bytes
| import cv2 | |
| import pytesseract | |
| from transformers import pipeline | |
| class AIPipeline: | |
| def __init__(self): | |
| self.nlp = pipeline("text-classification", model="roberta-base") | |
| self.detector = cv2.dnn.readNet("yolov4.weights", "yolov4.cfg") | |
| def process_ad(self, ad): | |
| results = { | |
| "sentiment": self.nlp(ad.content), | |
| "ocr": self._extract_ocr(ad.media), | |
| "objects": self._detect_objects(ad.media) | |
| } | |
| return results | |
| def _extract_ocr(self, media): | |
| if media and media.type == "image": | |
| return pytesseract.image_to_string(media.path) | |
| return None | |
| def _detect_objects(self, media): | |
| if media and media.type == "image": | |
| img = cv2.imread(media.path) | |
| self.detector.setInput(img) | |
| return self.detector.forward() | |
| return None |