Spaces:
Runtime error
Runtime error
iabualhaol
commited on
Commit
•
14d5f84
1
Parent(s):
3efa6f1
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from sentence_transformers import SentenceTransformer
|
2 |
+
import gradio as gr
|
3 |
+
import faiss
|
4 |
+
import numpy as np
|
5 |
+
model = SentenceTransformer('models/all-mpnet-base-v2')
|
6 |
+
model.to('cuda')
|
7 |
+
def EmdeddingVect(sentences):
|
8 |
+
return model.encode([sentences]) # Assume model.encode() function exists
|
9 |
+
|
10 |
+
# Load the arrays
|
11 |
+
all_embeddings = np.load('models/all_embeddings.npy')
|
12 |
+
all_labels = np.load('models/all_labels.npy')
|
13 |
+
|
14 |
+
index = faiss.IndexFlatL2(768)
|
15 |
+
index.add(all_embeddings.astype('float32'))
|
16 |
+
|
17 |
+
def search_query(k, query_sentence):
|
18 |
+
query_embeddings = EmdeddingVect(query_sentence)
|
19 |
+
distances, indices = index.search(query_embeddings.astype('float32'), int(k))
|
20 |
+
ai_count = np.sum(all_labels[indices[0]] == 'AI')
|
21 |
+
ai_probability = (ai_count / int(k)) * 100
|
22 |
+
human_probability = 100 - ai_probability
|
23 |
+
return f"Probability of being AI: {ai_probability:.2f}%", f"Probability of being Human: {human_probability:.2f}%"
|
24 |
+
|
25 |
+
iface = gr.Interface(
|
26 |
+
fn=search_query,
|
27 |
+
inputs=[
|
28 |
+
gr.inputs.Slider(minimum=1, maximum=10, default=3, label="k (Number of Neighbors)"),
|
29 |
+
gr.inputs.Textbox(label="Text to be Detected")
|
30 |
+
],
|
31 |
+
outputs=[
|
32 |
+
gr.outputs.Textbox(label="AI Probability"),
|
33 |
+
gr.outputs.Textbox(label="Human Probability")
|
34 |
+
]
|
35 |
+
)
|
36 |
+
|
37 |
+
iface.launch()
|