gschurck's picture
Update app.py
72b2cdd verified
raw
history blame
1.23 kB
from PIL import Image
import gradio as gr
import requests
from transformers import AutoTokenizer, AutoModel
def get_image_embedding(image):
return {"embedding": "img_emb.tolist()"}
def get_text_embedding(text):
# Load the tokenizer
tokenizer = AutoTokenizer.from_pretrained("Alibaba-NLP/gte-Qwen2-1.5B-instruct")
# Load the model
model = AutoModel.from_pretrained("Alibaba-NLP/gte-Qwen2-1.5B-instruct")
# Tokenize the input text
text = "Your input text goes here"
inputs = tokenizer(text, return_tensors='pt')
# Get embeddings from the model
with torch.no_grad():
outputs = model(**inputs)
embeddings = outputs.last_hidden_state
# Process embeddings (e.g., take the mean of all token embeddings)
sentence_embedding = embeddings.mean(dim=1)
return {"embedding": sentence_embedding}
image_embedding = gr.Interface(fn=get_image_embedding, inputs=gr.Image(type="pil"), outputs=gr.JSON(), title="Image Embedding")
text_embedding = gr.Interface(fn=get_text_embedding, inputs=gr.Textbox(), outputs=gr.JSON(), title="Text Embedding")
space = gr.TabbedInterface([image_embedding, text_embedding], ["Image Embedding", "Text Embedding"])
space.launch()