eaglelandsonce commited on
Commit
276a3be
·
verified ·
1 Parent(s): 49f6834

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -0
app.py ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import pandas as pd
3
+ from sentence_transformers import SentenceTransformer
4
+
5
+ # 1. Load a pretrained SentenceTransformer model
6
+ model = SentenceTransformer('all-MiniLM-L6-v2')
7
+
8
+ def clean_and_embed(text: str):
9
+ # 2. Clean: remove non-ASCII & lowercase
10
+ clean = text.encode('ascii', 'ignore').decode().lower()
11
+
12
+ # 3. Tokenize via the model’s tokenizer
13
+ tokens = model.tokenizer.tokenize(clean)
14
+
15
+ # 4. Get the sentence embedding
16
+ emb = model.encode(clean, convert_to_numpy=True)
17
+
18
+ # 5. Build a DataFrame: one row (the sentence) × embedding dims
19
+ df = pd.DataFrame(
20
+ [emb],
21
+ index=["sentence_embedding"],
22
+ columns=[f"dim_{i}" for i in range(emb.shape[0])]
23
+ )
24
+
25
+ return " ".join(tokens), df
26
+
27
+ # 6. Gradio interface
28
+ iface = gr.Interface(
29
+ fn=clean_and_embed,
30
+ inputs=gr.Textbox(lines=2, placeholder="Type your text here…"),
31
+ outputs=[
32
+ gr.Textbox(label="Tokens"),
33
+ gr.Dataframe(label="Sentence Embedding Vector")
34
+ ],
35
+ title="ASCII‑Clean + SentenceTransformer",
36
+ description="Cleans input, tokenizes with a SentenceTransformer tokenizer, and shows the sentence embedding."
37
+ )
38
+
39
+ if __name__ == "__main__":
40
+ iface.launch()