countbigger commited on
Commit
3783441
·
verified ·
1 Parent(s): ad5cac4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -12
app.py CHANGED
@@ -2,26 +2,30 @@ import gradio as gr
2
  from sentence_transformers import SentenceTransformer
3
  import torch
4
 
5
- # Load model with trust_remote_code=True for perplexity models
6
  model = SentenceTransformer(
7
  'perplexity-ai/pplx-embed-v1-4b',
8
  trust_remote_code=True,
9
- device='cpu' # Force CPU for free tier
10
  )
11
 
12
- def embed_text(texts):
13
- if isinstance(texts, str):
14
- texts = [texts]
15
- embeddings = model.encode(texts, normalize_embeddings=True)
16
- return embeddings.tolist()
 
17
 
18
  demo = gr.Interface(
19
  fn=embed_text,
20
- inputs=gr.Textbox(label="Enter text(s), one per line", lines=3, placeholder="Your text here..."),
21
- outputs=gr.Textbox(label="Embeddings (JSON)", lines=10),
22
- title="pplx-embed-v1-4b API ✅",
23
- description="Free CPU embedding inference"
 
 
 
 
24
  )
25
 
26
  if __name__ == "__main__":
27
- demo.launch()
 
2
  from sentence_transformers import SentenceTransformer
3
  import torch
4
 
 
5
  model = SentenceTransformer(
6
  'perplexity-ai/pplx-embed-v1-4b',
7
  trust_remote_code=True,
8
+ device='cpu'
9
  )
10
 
11
+ def embed_text(text):
12
+ if not text or text.strip() == "":
13
+ return "Please enter single text input"
14
+
15
+ embedding = model.encode([text], normalize_embeddings=True)[0]
16
+ return embedding.tolist()
17
 
18
  demo = gr.Interface(
19
  fn=embed_text,
20
+ inputs=gr.Textbox(
21
+ label="Single Text Input",
22
+ lines=1,
23
+ placeholder="enter here..."
24
+ ),
25
+ outputs=gr.Textbox(label="Embedding (JSON)", lines=10),
26
+ title="text-to-embedding",
27
+ description="simple text-to-embedding, using pplx-embed-v1-4b"
28
  )
29
 
30
  if __name__ == "__main__":
31
+ demo.launch(share=False, show_error=True)