Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -11,9 +11,8 @@ from transformers import pipeline
|
|
| 11 |
|
| 12 |
MODEL_ID = "fakespot-ai/roberta-base-ai-text-detection-v1"
|
| 13 |
|
| 14 |
-
#
|
| 15 |
-
|
| 16 |
-
clf = pipeline("text-classification", model=MODEL_ID)
|
| 17 |
|
| 18 |
def clean_text(s: str) -> str:
|
| 19 |
s = s.strip()
|
|
@@ -44,11 +43,8 @@ def detect_ai(text: str) -> Tuple[str, float, str]:
|
|
| 44 |
return "—", 0.0, "Please paste some text to analyze."
|
| 45 |
|
| 46 |
chunks = [clean_text(c) for c in chunk_text(text, max_words=300)]
|
| 47 |
-
|
| 48 |
-
# Batch for speed and lower overhead
|
| 49 |
preds = clf(chunks)
|
| 50 |
|
| 51 |
-
# Aggregate AI likelihood: if a chunk label is 'AI', use score; if 'Human', use (1-score)
|
| 52 |
ai_probs = []
|
| 53 |
for p in preds:
|
| 54 |
label = str(p.get("label", "")).upper()
|
|
@@ -125,14 +121,13 @@ with gr.Blocks(title="AI Text Detector") as demo:
|
|
| 125 |
|
| 126 |
def _run(t: str):
|
| 127 |
label, score, expl = detect_ai(t)
|
| 128 |
-
# gr.Label expects a dict of {class_name: confidence} for pretty display
|
| 129 |
return {label_out: {label: 1.0}, score_out: score, explain: expl}
|
| 130 |
|
| 131 |
gr.Button("Analyze").click(_run, inputs=inp, outputs=[label_out, score_out, explain])
|
| 132 |
|
| 133 |
if __name__ == "__main__":
|
| 134 |
# For Spaces, PORT is provided by the environment
|
| 135 |
-
demo.queue(
|
| 136 |
server_name="0.0.0.0",
|
| 137 |
server_port=int(os.getenv("PORT", 7860))
|
| 138 |
)
|
|
|
|
| 11 |
|
| 12 |
MODEL_ID = "fakespot-ai/roberta-base-ai-text-detection-v1"
|
| 13 |
|
| 14 |
+
# Explicitly use CPU on Spaces
|
| 15 |
+
clf = pipeline("text-classification", model=MODEL_ID, device=-1)
|
|
|
|
| 16 |
|
| 17 |
def clean_text(s: str) -> str:
|
| 18 |
s = s.strip()
|
|
|
|
| 43 |
return "—", 0.0, "Please paste some text to analyze."
|
| 44 |
|
| 45 |
chunks = [clean_text(c) for c in chunk_text(text, max_words=300)]
|
|
|
|
|
|
|
| 46 |
preds = clf(chunks)
|
| 47 |
|
|
|
|
| 48 |
ai_probs = []
|
| 49 |
for p in preds:
|
| 50 |
label = str(p.get("label", "")).upper()
|
|
|
|
| 121 |
|
| 122 |
def _run(t: str):
|
| 123 |
label, score, expl = detect_ai(t)
|
|
|
|
| 124 |
return {label_out: {label: 1.0}, score_out: score, explain: expl}
|
| 125 |
|
| 126 |
gr.Button("Analyze").click(_run, inputs=inp, outputs=[label_out, score_out, explain])
|
| 127 |
|
| 128 |
if __name__ == "__main__":
|
| 129 |
# For Spaces, PORT is provided by the environment
|
| 130 |
+
demo.queue().launch(
|
| 131 |
server_name="0.0.0.0",
|
| 132 |
server_port=int(os.getenv("PORT", 7860))
|
| 133 |
)
|