ICTuniverse commited on
Commit
532bd4f
·
verified ·
1 Parent(s): 0000910

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -24
app.py CHANGED
@@ -1,29 +1,24 @@
1
- from fastapi import FastAPI, Request
2
- from sentence_transformers import SentenceTransformer
3
  import torch
4
 
5
- # Initialize FastAPI
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
- # Use GPU if available
12
- device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
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([text], device=device, convert_to_tensor=True)
25
-
26
- # Convert embeddings to list for JSON serialization
27
- embedding_list = embeddings.cpu().tolist()
28
-
29
- return {"embedding": embedding_list}
 
 
 
 
 
 
 
 
 
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()