Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -77,24 +77,67 @@
|
|
| 77 |
# demo.launch()
|
| 78 |
|
| 79 |
|
| 80 |
-
import gradio as gr
|
| 81 |
import os
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
demo = gr.Interface(
|
| 91 |
-
fn=
|
| 92 |
-
inputs=gr.Textbox(label="Type
|
| 93 |
-
outputs=gr.Textbox(label="
|
| 94 |
-
title="Test
|
| 95 |
-
description="This
|
| 96 |
)
|
| 97 |
|
| 98 |
-
#
|
|
|
|
|
|
|
| 99 |
if __name__ == "__main__":
|
| 100 |
demo.launch()
|
|
|
|
|
|
| 77 |
# demo.launch()
|
| 78 |
|
| 79 |
|
|
|
|
| 80 |
import os
|
| 81 |
+
import gradio as gr
|
| 82 |
+
from transformers import AutoTokenizer, AutoModel
|
| 83 |
+
import torch
|
| 84 |
+
import numpy as np
|
| 85 |
+
|
| 86 |
+
# ----------------------------------------------------
|
| 87 |
+
# 1. HF_TOKEN (optional, for private models)
|
| 88 |
+
# ----------------------------------------------------
|
| 89 |
+
HF_TOKEN = os.getenv("HF_TOKEN") # Retrieved from Hugging Face Secrets
|
| 90 |
+
|
| 91 |
+
# ----------------------------------------------------
|
| 92 |
+
# 2. Load EmbeddingGemma-300M model
|
| 93 |
+
# ----------------------------------------------------
|
| 94 |
+
MODEL_NAME = "google/embeddinggemma-300m"
|
| 95 |
+
|
| 96 |
+
# Load tokenizer and model
|
| 97 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME, use_auth_token=HF_TOKEN)
|
| 98 |
+
model = AutoModel.from_pretrained(MODEL_NAME, use_auth_token=HF_TOKEN)
|
| 99 |
+
|
| 100 |
+
# Function to encode text into embeddings
|
| 101 |
+
def encode(texts):
|
| 102 |
+
"""
|
| 103 |
+
Encode a list of texts into vector embeddings using EmbeddingGemma-300M.
|
| 104 |
+
Mean pooling over token embeddings is used.
|
| 105 |
+
"""
|
| 106 |
+
inputs = tokenizer(
|
| 107 |
+
texts, return_tensors="pt", padding=True, truncation=True
|
| 108 |
+
)
|
| 109 |
+
with torch.no_grad():
|
| 110 |
+
outputs = model(**inputs)
|
| 111 |
+
# Mean pooling over tokens
|
| 112 |
+
embeddings = outputs.last_hidden_state.mean(dim=1)
|
| 113 |
+
# Convert to numpy float32
|
| 114 |
+
return embeddings.cpu().numpy().astype(np.float32)
|
| 115 |
+
|
| 116 |
+
# ----------------------------------------------------
|
| 117 |
+
# 3. Gradio test function
|
| 118 |
+
# ----------------------------------------------------
|
| 119 |
+
def test_encode(text):
|
| 120 |
+
"""
|
| 121 |
+
Simple test function to check if embeddings are generated correctly.
|
| 122 |
+
Returns the shape of the resulting embedding vector.
|
| 123 |
+
"""
|
| 124 |
+
emb = encode([text])
|
| 125 |
+
return f"Embedding shape: {emb.shape}"
|
| 126 |
+
|
| 127 |
+
# ----------------------------------------------------
|
| 128 |
+
# 4. Build Gradio Interface
|
| 129 |
+
# ----------------------------------------------------
|
| 130 |
demo = gr.Interface(
|
| 131 |
+
fn=test_encode,
|
| 132 |
+
inputs=gr.Textbox(label="Type some text"),
|
| 133 |
+
outputs=gr.Textbox(label="Embedding info"),
|
| 134 |
+
title="Test EmbeddingGemma-300M",
|
| 135 |
+
description="This Space tests whether the EmbeddingGemma-300M model can generate embeddings."
|
| 136 |
)
|
| 137 |
|
| 138 |
+
# ----------------------------------------------------
|
| 139 |
+
# 5. Launch App
|
| 140 |
+
# ----------------------------------------------------
|
| 141 |
if __name__ == "__main__":
|
| 142 |
demo.launch()
|
| 143 |
+
|