mirror of
https://github.com/csd4ni3l/grass_touching_captcha.git
synced 2026-01-01 04:23:45 +01:00
add word vector cache with JSON and add a constant for the word to compare with
This commit is contained in:
@@ -7,5 +7,6 @@ JINA_HEADERS = {
|
|||||||
RICKROLL_LINK = "https://www.youtube.com/watch?v=xvFZjo5PgG0"
|
RICKROLL_LINK = "https://www.youtube.com/watch?v=xvFZjo5PgG0"
|
||||||
UPLOAD_DIR = "uploads"
|
UPLOAD_DIR = "uploads"
|
||||||
MINIMUM_COSINE_SIMILARITY = 0.7
|
MINIMUM_COSINE_SIMILARITY = 0.7
|
||||||
|
WORD_TO_COMPARE = "hand touching grass"
|
||||||
|
|
||||||
UPLOAD_DIR = os.path.join(os.getcwd(), UPLOAD_DIR)
|
UPLOAD_DIR = os.path.join(os.getcwd(), UPLOAD_DIR)
|
||||||
16
jina.py
16
jina.py
@@ -1,5 +1,5 @@
|
|||||||
from constants import JINA_URL, JINA_HEADERS
|
from constants import JINA_URL, JINA_HEADERS, WORD_TO_COMPARE
|
||||||
import requests, dotenv, os, sys, numpy as np
|
import requests, dotenv, os, sys, json, numpy as np
|
||||||
|
|
||||||
def get_embedding(input: list):
|
def get_embedding(input: list):
|
||||||
headers = JINA_HEADERS
|
headers = JINA_HEADERS
|
||||||
@@ -24,7 +24,17 @@ def get_embedding(input: list):
|
|||||||
return [jina_object["embedding"] for jina_object in response.json()["data"]]
|
return [jina_object["embedding"] for jina_object in response.json()["data"]]
|
||||||
|
|
||||||
def get_grass_touching_similarity(image_url):
|
def get_grass_touching_similarity(image_url):
|
||||||
grass_image_embedding, grass_word_embedding = get_embedding([{"image": image_url}, {"text": "hand touching grass"}])
|
if os.path.exists("compared_word_emb.json"):
|
||||||
|
with open("compared_word_emb.json", "r") as file:
|
||||||
|
grass_word_embedding = json.load(file)
|
||||||
|
|
||||||
|
grass_image_embedding = get_embedding([{"image": image_url}])
|
||||||
|
|
||||||
|
else:
|
||||||
|
grass_image_embedding, grass_word_embedding = get_embedding([{"image": image_url}, {"text": WORD_TO_COMPARE}])
|
||||||
|
|
||||||
|
with open("compared_word_emb.json", "w") as file:
|
||||||
|
file.write(json.dumps(grass_word_embedding))
|
||||||
|
|
||||||
grass_image_embedding = np.array(grass_image_embedding)
|
grass_image_embedding = np.array(grass_image_embedding)
|
||||||
grass_word_embedding = np.array(grass_word_embedding)
|
grass_word_embedding = np.array(grass_word_embedding)
|
||||||
|
|||||||
Reference in New Issue
Block a user