Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from transformers import AutoModel, AutoTokenizer
|
3 |
+
import torch
|
4 |
+
|
5 |
+
# Load the model and tokenizer
|
6 |
+
model_name = "sentence-transformers/all-MiniLM-L6-v2"
|
7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
8 |
+
model = AutoModel.from_pretrained(model_name)
|
9 |
+
|
10 |
+
# Function to get embeddings
|
11 |
+
def get_embedding(text):
|
12 |
+
inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True)
|
13 |
+
with torch.no_grad():
|
14 |
+
outputs = model(**inputs)
|
15 |
+
embeddings = outputs.last_hidden_state.mean(dim=1)
|
16 |
+
return embeddings
|
17 |
+
|
18 |
+
st.title("Text Embedding with all-MiniLM-L6-v2")
|
19 |
+
st.write("Enter text to get its embedding:")
|
20 |
+
|
21 |
+
# Input text from the user
|
22 |
+
input_text = st.text_area("Input Text", "")
|
23 |
+
|
24 |
+
# If input text is provided, show the embeddings
|
25 |
+
if input_text:
|
26 |
+
embedding = get_embedding(input_text)
|
27 |
+
st.write("Embedding:")
|
28 |
+
st.write(embedding.numpy())
|