Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,21 +1,32 @@
|
|
| 1 |
from sentence_transformers import SentenceTransformer
|
| 2 |
import gradio as gr
|
| 3 |
-
import
|
| 4 |
import numpy as np
|
| 5 |
|
| 6 |
# Load the pre-trained model
|
| 7 |
embedding_model = SentenceTransformer('all-MiniLM-L6-v2')
|
| 8 |
|
| 9 |
# Define the function to process requests
|
| 10 |
-
def generate_embeddings(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
embeddings = embedding_model.encode(chunks, convert_to_tensor=False)
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
# Define the Gradio interface
|
| 16 |
interface = gr.Interface(
|
| 17 |
fn=generate_embeddings,
|
| 18 |
-
inputs=gr.Textbox(lines=5, placeholder="Enter text chunks here..."
|
| 19 |
outputs=[gr.JSON(label="Embeddings"), gr.Label(label="Shape")],
|
| 20 |
title="Sentence Transformer Embeddings",
|
| 21 |
description="Generate embeddings for input text chunks."
|
|
|
|
| 1 |
from sentence_transformers import SentenceTransformer
|
| 2 |
import gradio as gr
|
| 3 |
+
import torch
|
| 4 |
import numpy as np
|
| 5 |
|
| 6 |
# Load the pre-trained model
|
| 7 |
embedding_model = SentenceTransformer('all-MiniLM-L6-v2')
|
| 8 |
|
| 9 |
# Define the function to process requests
|
| 10 |
+
def generate_embeddings(text):
|
| 11 |
+
# Split the input text into chunks (if needed)
|
| 12 |
+
chunks = text.split('\n') # Assuming chunks are separated by new lines
|
| 13 |
+
|
| 14 |
+
# Encode the input chunks to get the embeddings
|
| 15 |
embeddings = embedding_model.encode(chunks, convert_to_tensor=False)
|
| 16 |
+
|
| 17 |
+
# Convert the embeddings to a PyTorch tensor
|
| 18 |
+
embeddings_tensor = torch.tensor(embeddings)
|
| 19 |
+
|
| 20 |
+
# Add batch dimension to the tensor (if needed)
|
| 21 |
+
embeddings_tensor = embeddings_tensor.unsqueeze(0) # Uncomment if a batch dimension is required
|
| 22 |
+
|
| 23 |
+
# Return the embeddings tensor and its shape
|
| 24 |
+
return embeddings_tensor.tolist(), embeddings_tensor.shape
|
| 25 |
|
| 26 |
# Define the Gradio interface
|
| 27 |
interface = gr.Interface(
|
| 28 |
fn=generate_embeddings,
|
| 29 |
+
inputs=gr.Textbox(lines=5, placeholder="Enter text chunks here..."),
|
| 30 |
outputs=[gr.JSON(label="Embeddings"), gr.Label(label="Shape")],
|
| 31 |
title="Sentence Transformer Embeddings",
|
| 32 |
description="Generate embeddings for input text chunks."
|