-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove embedding code from public repo
This code is pretty specific to our workflow, and adds a bunch of overhead to AnalyzedUrl's. We're going to bring this small part of the code into a private repo, similar to our ML modeling before it.
- Loading branch information
1 parent
e92f4ce
commit 3202a68
Showing
10 changed files
with
55 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
"""Backends for analyzing URLs for keywords and topics.""" | ||
from .eatopics import EthicalAdsTopicsBackend # noqa | ||
from .naive import NaiveKeywordAnalyzerBackend # noqa | ||
from .st import SentenceTransformerAnalyzerBackend # noqa | ||
from .textacynlp import TextacyAnalyzerBackend # noqa |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Generated by Django 4.2.11 on 2024-03-14 18:53 | ||
from django.db import migrations | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("adserver_analyzer", "0004_add_embeddings"), | ||
] | ||
|
||
operations = [ | ||
migrations.RemoveField( | ||
model_name="analyzedurl", | ||
name="embedding", | ||
), | ||
migrations.RemoveField( | ||
model_name="historicalanalyzedurl", | ||
name="embedding", | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,81 +1 @@ | ||
from urllib.parse import urlparse | ||
|
||
from django.conf import settings | ||
from pgvector.django import CosineDistance | ||
from rest_framework import status | ||
from rest_framework.permissions import AllowAny | ||
from rest_framework.renderers import StaticHTMLRenderer | ||
from rest_framework.response import Response | ||
from rest_framework.views import APIView | ||
|
||
from adserver.analyzer.backends.st import SentenceTransformerAnalyzerBackend | ||
from adserver.analyzer.models import AnalyzedUrl | ||
|
||
|
||
if "adserver.analyzer" in settings.INSTALLED_APPS: | ||
|
||
class EmbeddingViewSet(APIView): | ||
""" | ||
Returns a list of similar URLs and scores based on querying the AnalyzedURL embedding for an incoming URL. | ||
Example: http://localhost:5000/api/v1/similar/?url=https://www.gitbook.com/ | ||
.. http:get:: /api/v1/embedding/ | ||
Return a list of similar URLs and scores based on querying the AnalyzedURL embedding for an incoming URL | ||
:<json string url: **Required**. The URL to query for similar URLs and scores | ||
:>json int count: The number of similar URLs returned | ||
:>json array results: An array of similar URLs and scores | ||
""" | ||
|
||
permission_classes = [AllowAny] | ||
|
||
def get(self, request): | ||
"""Return a list of similar URLs and scores based on querying the AnalyzedURL embedding for an incoming URL.""" | ||
url = request.query_params.get("url") | ||
|
||
if not url: | ||
return Response( | ||
{"error": "url is required"}, status=status.HTTP_400_BAD_REQUEST | ||
) | ||
|
||
backend_instance = SentenceTransformerAnalyzerBackend(url) | ||
response = backend_instance.fetch() | ||
if not response: | ||
return Response( | ||
{"error": "Not able to fetch content from URL"}, | ||
status=status.HTTP_400_BAD_REQUEST, | ||
) | ||
processed_text = backend_instance.get_content(response) | ||
analyzed_embedding = backend_instance.embedding(response) | ||
|
||
unfiltered_urls = ( | ||
AnalyzedUrl.objects.filter(publisher__allow_paid_campaigns=True) | ||
.exclude(embedding=None) | ||
.annotate(distance=CosineDistance("embedding", analyzed_embedding)) | ||
.order_by("distance")[:25] | ||
) | ||
|
||
# Filter urls to ensure each domain is unique | ||
unique_domains = set() | ||
urls = [] | ||
for url in unfiltered_urls: | ||
domain = urlparse(url.url).netloc | ||
if domain not in unique_domains: | ||
unique_domains.add(domain) | ||
urls.append(url) | ||
|
||
if not len(urls) > 3: | ||
return Response( | ||
{"error": "No similar URLs found"}, status=status.HTTP_404_NOT_FOUND | ||
) | ||
|
||
return Response( | ||
{ | ||
"count": len(urls), | ||
"text": processed_text[:500], | ||
"results": [[url.url, url.distance] for url in urls], | ||
} | ||
) | ||
# Left blank |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters