Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,29 +1,24 @@
|
|
| 1 |
-
|
| 2 |
-
from
|
| 3 |
import torch
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
app = FastAPI()
|
| 7 |
-
|
| 8 |
-
# Load your SentenceTransformer model with trust_remote_code=True
|
| 9 |
model = SentenceTransformer("ICTuniverse/tuned-bi-encoder", trust_remote_code=True)
|
| 10 |
|
| 11 |
-
#
|
| 12 |
-
|
| 13 |
-
model.to(device)
|
| 14 |
-
|
| 15 |
-
@app.post("/embed")
|
| 16 |
-
async def embed_text(request: Request):
|
| 17 |
-
data = await request.json()
|
| 18 |
-
text = data.get("text")
|
| 19 |
-
if not text:
|
| 20 |
-
return {"error": "Text is required"}
|
| 21 |
-
|
| 22 |
-
# Get embeddings
|
| 23 |
with torch.no_grad():
|
| 24 |
-
embeddings = model.encode(
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoTokenizer, AutoModel
|
| 3 |
import torch
|
| 4 |
|
| 5 |
+
# Load your model with trust_remote_code=True
|
|
|
|
|
|
|
|
|
|
| 6 |
model = SentenceTransformer("ICTuniverse/tuned-bi-encoder", trust_remote_code=True)
|
| 7 |
|
| 8 |
+
# Define a function to get embeddings
|
| 9 |
+
def get_embedding(text):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
with torch.no_grad():
|
| 11 |
+
embeddings = model.encode(text)
|
| 12 |
+
return embeddings
|
| 13 |
+
|
| 14 |
+
# Create a Gradio interface
|
| 15 |
+
iface = gr.Interface(
|
| 16 |
+
fn=get_embedding,
|
| 17 |
+
inputs=gr.Textbox(lines=2, placeholder="Enter text here..."),
|
| 18 |
+
outputs="json",
|
| 19 |
+
title="Embedding Generator",
|
| 20 |
+
description="Get embeddings using ICTuniverse/tuned-bi-encoder"
|
| 21 |
+
)
|
| 22 |
+
|
| 23 |
+
# Launch the Gradio app
|
| 24 |
+
iface.launch()
|