Commit
Β·
0130234
1
Parent(s):
16bb651
fix: user api key
Browse files- app.py +11 -5
- similarity.py +10 -10
- test.py +5 -0
app.py
CHANGED
|
@@ -89,17 +89,22 @@ def load_examples(n: int = 3):
|
|
| 89 |
static_examples = load_examples()
|
| 90 |
|
| 91 |
# βββββββββ backend for user Run βββββββββ
|
| 92 |
-
def run_mapper(prompt: str, img_url: str):
|
|
|
|
| 93 |
if not img_url:
|
| 94 |
raise gr.Error("Please provide an image URL.")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
try:
|
| 96 |
r = requests.get(img_url, stream=True, timeout=10); r.raise_for_status()
|
| 97 |
img = Image.open(io.BytesIO(r.content)).convert("RGB")
|
| 98 |
except Exception as e:
|
| 99 |
raise gr.Error(f"Image load failed: {e}")
|
| 100 |
-
|
| 101 |
-
img_proc, _, _ =
|
| 102 |
-
toks, maps =
|
| 103 |
if not toks:
|
| 104 |
raise gr.Error("Mapper returned no tokens.")
|
| 105 |
|
|
@@ -127,6 +132,7 @@ with gr.Blocks(css=css) as demo:
|
|
| 127 |
# User input
|
| 128 |
prompt_in = gr.Textbox(label="Prompt", placeholder="Describe what to queryβ¦")
|
| 129 |
url_in = gr.Textbox(label="Image URL", placeholder="https://example.com/image.jpg")
|
|
|
|
| 130 |
run_btn = gr.Button("Run", elem_id="run-btn")
|
| 131 |
|
| 132 |
# Output area
|
|
@@ -137,7 +143,7 @@ with gr.Blocks(css=css) as demo:
|
|
| 137 |
img_out = gr.Image(label="Image + Heatmap", visible=False)
|
| 138 |
|
| 139 |
run_btn.click(run_mapper,
|
| 140 |
-
[prompt_in, url_in],
|
| 141 |
[token_sel, maps_st, img_st, img_out, info_md, prompt_in, url_in])
|
| 142 |
|
| 143 |
(token_sel.select if hasattr(token_sel,"select") else token_sel.change)(
|
|
|
|
| 89 |
static_examples = load_examples()
|
| 90 |
|
| 91 |
# βββββββββ backend for user Run βββββββββ
|
| 92 |
+
def run_mapper(prompt: str, img_url: str, api_key: str):
|
| 93 |
+
new_client = JinaV4SimilarityMapper(client_type="web")
|
| 94 |
if not img_url:
|
| 95 |
raise gr.Error("Please provide an image URL.")
|
| 96 |
+
if not prompt:
|
| 97 |
+
raise gr.Error("Please provide a prompt.")
|
| 98 |
+
if not api_key:
|
| 99 |
+
raise gr.Error("Please provide a valid API key.")
|
| 100 |
try:
|
| 101 |
r = requests.get(img_url, stream=True, timeout=10); r.raise_for_status()
|
| 102 |
img = Image.open(io.BytesIO(r.content)).convert("RGB")
|
| 103 |
except Exception as e:
|
| 104 |
raise gr.Error(f"Image load failed: {e}")
|
| 105 |
+
new_client.model.set_api_key(api_key)
|
| 106 |
+
img_proc, _, _ = new_client.process_image(img_url)
|
| 107 |
+
toks, maps = new_client.get_token_similarity_maps(prompt, img_proc)
|
| 108 |
if not toks:
|
| 109 |
raise gr.Error("Mapper returned no tokens.")
|
| 110 |
|
|
|
|
| 132 |
# User input
|
| 133 |
prompt_in = gr.Textbox(label="Prompt", placeholder="Describe what to queryβ¦")
|
| 134 |
url_in = gr.Textbox(label="Image URL", placeholder="https://example.com/image.jpg")
|
| 135 |
+
api_key_in = gr.Textbox(label="API Key", placeholder="Enter your Jina API key here")
|
| 136 |
run_btn = gr.Button("Run", elem_id="run-btn")
|
| 137 |
|
| 138 |
# Output area
|
|
|
|
| 143 |
img_out = gr.Image(label="Image + Heatmap", visible=False)
|
| 144 |
|
| 145 |
run_btn.click(run_mapper,
|
| 146 |
+
[prompt_in, url_in, api_key_in],
|
| 147 |
[token_sel, maps_st, img_st, img_out, info_md, prompt_in, url_in])
|
| 148 |
|
| 149 |
(token_sel.select if hasattr(token_sel,"select") else token_sel.change)(
|
similarity.py
CHANGED
|
@@ -24,22 +24,14 @@ class JinaEmbeddingsClient:
|
|
| 24 |
|
| 25 |
def __init__(
|
| 26 |
self,
|
| 27 |
-
api_key: str | None = None,
|
| 28 |
model: str = "jina-embeddings-v4",
|
| 29 |
return_multivector: bool = True,
|
| 30 |
task: str = "retrieval.query",
|
| 31 |
timeout: int = 30,
|
| 32 |
) -> None:
|
| 33 |
-
# Grab token from env if it wasnβt provided
|
| 34 |
-
api_key = api_key or os.getenv("JINA_TOKEN")
|
| 35 |
-
if not api_key:
|
| 36 |
-
raise ValueError(
|
| 37 |
-
"No API key supplied. Pass `api_key=` or set the env var JINA_TOKEN."
|
| 38 |
-
)
|
| 39 |
-
|
| 40 |
self.headers = {
|
| 41 |
"Content-Type": "application/json",
|
| 42 |
-
"Authorization": f"Bearer
|
| 43 |
}
|
| 44 |
self.base_payload = {
|
| 45 |
"model": model,
|
|
@@ -90,6 +82,14 @@ class JinaEmbeddingsClient:
|
|
| 90 |
resp.raise_for_status()
|
| 91 |
return resp.json()
|
| 92 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
@staticmethod
|
| 94 |
def _as_tensors(data: List[Dict[str, Any]]) -> List[torch.Tensor]:
|
| 95 |
"""
|
|
@@ -118,7 +118,7 @@ class JinaV4SimilarityMapper:
|
|
| 118 |
device: str = "cuda" if torch.cuda.is_available() else "cpu",
|
| 119 |
heatmap_alpha: float = 0.6,
|
| 120 |
num_vectors: int = 128,
|
| 121 |
-
client_type: str = "local"
|
| 122 |
):
|
| 123 |
"""
|
| 124 |
Initialize the mapper with Jina Embedding v4.
|
|
|
|
| 24 |
|
| 25 |
def __init__(
|
| 26 |
self,
|
|
|
|
| 27 |
model: str = "jina-embeddings-v4",
|
| 28 |
return_multivector: bool = True,
|
| 29 |
task: str = "retrieval.query",
|
| 30 |
timeout: int = 30,
|
| 31 |
) -> None:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
self.headers = {
|
| 33 |
"Content-Type": "application/json",
|
| 34 |
+
"Authorization": f"Bearer Not Set",
|
| 35 |
}
|
| 36 |
self.base_payload = {
|
| 37 |
"model": model,
|
|
|
|
| 82 |
resp.raise_for_status()
|
| 83 |
return resp.json()
|
| 84 |
|
| 85 |
+
def set_api_key(self, api_key: str) -> None:
|
| 86 |
+
"""
|
| 87 |
+
Set the API key for authentication.
|
| 88 |
+
"""
|
| 89 |
+
if not api_key:
|
| 90 |
+
raise ValueError("API key must not be empty.")
|
| 91 |
+
self.headers["Authorization"] = f"Bearer {api_key}"
|
| 92 |
+
|
| 93 |
@staticmethod
|
| 94 |
def _as_tensors(data: List[Dict[str, Any]]) -> List[torch.Tensor]:
|
| 95 |
"""
|
|
|
|
| 118 |
device: str = "cuda" if torch.cuda.is_available() else "cpu",
|
| 119 |
heatmap_alpha: float = 0.6,
|
| 120 |
num_vectors: int = 128,
|
| 121 |
+
client_type: str = "local",
|
| 122 |
):
|
| 123 |
"""
|
| 124 |
Initialize the mapper with Jina Embedding v4.
|
test.py
CHANGED
|
@@ -7,9 +7,14 @@ import subprocess
|
|
| 7 |
import requests
|
| 8 |
import gradio as gr
|
| 9 |
from PIL import Image
|
|
|
|
| 10 |
|
| 11 |
from app import MAPPER, save_run
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
inputs = [
|
| 14 |
("A person riding a bicycle near the ocean", "https://cdn.duvine.com/wp-content/uploads/2016/04/17095703/Slides_mallorca_FOR-WEB.jpg"),
|
| 15 |
("Computing jobs in the US", "https://www.ayresassociates.com/wp-content/uploads/2019/02/Career-Expo-Pie-Chart-Crop.jpg"),
|
|
|
|
| 7 |
import requests
|
| 8 |
import gradio as gr
|
| 9 |
from PIL import Image
|
| 10 |
+
import os
|
| 11 |
|
| 12 |
from app import MAPPER, save_run
|
| 13 |
|
| 14 |
+
api_key = os.getenv("JINA_TOKEN")
|
| 15 |
+
|
| 16 |
+
MAPPER.model.set_api_key(api_key)
|
| 17 |
+
|
| 18 |
inputs = [
|
| 19 |
("A person riding a bicycle near the ocean", "https://cdn.duvine.com/wp-content/uploads/2016/04/17095703/Slides_mallorca_FOR-WEB.jpg"),
|
| 20 |
("Computing jobs in the US", "https://www.ayresassociates.com/wp-content/uploads/2019/02/Career-Expo-Pie-Chart-Crop.jpg"),
|