Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -41,20 +41,14 @@ def split_into_paragraphs(text: str):
|
|
| 41 |
return paragraphs
|
| 42 |
|
| 43 |
def analyze_text_block(text: str):
|
| 44 |
-
"""Analyze a
|
| 45 |
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
|
| 46 |
with torch.no_grad():
|
| 47 |
logits = model(**inputs).logits
|
| 48 |
probs = torch.softmax(logits, dim=1)[0].tolist()
|
| 49 |
-
|
| 50 |
return {
|
| 51 |
-
"label_scores": {
|
| 52 |
-
model.config.id2label[0]: round(probs[0], 4),
|
| 53 |
-
model.config.id2label[1]: round(probs[1], 4)
|
| 54 |
-
},
|
| 55 |
"ai_generated_score": probs[1],
|
| 56 |
-
"human_written_score": probs[0]
|
| 57 |
-
"is_ai": probs[1] > probs[0]
|
| 58 |
}
|
| 59 |
|
| 60 |
# =====================================================
|
|
@@ -70,24 +64,32 @@ async def analyze(data: InputText):
|
|
| 70 |
if not text:
|
| 71 |
return {"success": False, "code": 400, "message": "Empty input text"}
|
| 72 |
|
| 73 |
-
|
|
|
|
|
|
|
|
|
|
| 74 |
results = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
"human_written_score": res["human_written_score"]
|
| 84 |
-
})
|
| 85 |
|
| 86 |
-
|
| 87 |
-
total_words += word_count
|
| 88 |
-
ai_words += word_count * res["ai_generated_score"]
|
| 89 |
|
| 90 |
-
fake_percentage = round((ai_words / total_words) * 100, 2) if total_words > 0 else 0
|
| 91 |
feedback = (
|
| 92 |
"Most of Your Text is AI/GPT Generated"
|
| 93 |
if fake_percentage > 50
|
|
@@ -105,7 +107,7 @@ async def analyze(data: InputText):
|
|
| 105 |
"h": [r["paragraph"] for r in results],
|
| 106 |
"hi": [],
|
| 107 |
"textWords": total_words,
|
| 108 |
-
"aiWords":
|
| 109 |
"fakePercentage": fake_percentage,
|
| 110 |
"specialIndexes": [],
|
| 111 |
"specialSentences": [],
|
|
|
|
| 41 |
return paragraphs
|
| 42 |
|
| 43 |
def analyze_text_block(text: str):
|
| 44 |
+
"""Analyze a block of text and return AI/Human probabilities."""
|
| 45 |
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
|
| 46 |
with torch.no_grad():
|
| 47 |
logits = model(**inputs).logits
|
| 48 |
probs = torch.softmax(logits, dim=1)[0].tolist()
|
|
|
|
| 49 |
return {
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
"ai_generated_score": probs[1],
|
| 51 |
+
"human_written_score": probs[0]
|
|
|
|
| 52 |
}
|
| 53 |
|
| 54 |
# =====================================================
|
|
|
|
| 64 |
if not text:
|
| 65 |
return {"success": False, "code": 400, "message": "Empty input text"}
|
| 66 |
|
| 67 |
+
total_words = len(text.split())
|
| 68 |
+
full_text_result = analyze_text_block(text)
|
| 69 |
+
fake_percentage = round(full_text_result["ai_generated_score"] * 100, 2)
|
| 70 |
+
|
| 71 |
results = []
|
| 72 |
+
ai_words = int(total_words * (fake_percentage / 100))
|
| 73 |
+
|
| 74 |
+
# ✅ If overall text looks AI-generated, analyze paragraphs for details
|
| 75 |
+
if fake_percentage > 50:
|
| 76 |
+
paragraphs = split_into_paragraphs(text)
|
| 77 |
+
ai_words, total_words = 0, 0
|
| 78 |
|
| 79 |
+
for paragraph in paragraphs:
|
| 80 |
+
res = analyze_text_block(paragraph)
|
| 81 |
+
word_count = len(paragraph.split())
|
| 82 |
+
total_words += word_count
|
| 83 |
+
ai_words += word_count * res["ai_generated_score"]
|
| 84 |
|
| 85 |
+
results.append({
|
| 86 |
+
"paragraph": paragraph,
|
| 87 |
+
"ai_generated_score": res["ai_generated_score"],
|
| 88 |
+
"human_written_score": res["human_written_score"]
|
| 89 |
+
})
|
|
|
|
|
|
|
| 90 |
|
| 91 |
+
fake_percentage = round((ai_words / total_words) * 100, 2) if total_words > 0 else 0
|
|
|
|
|
|
|
| 92 |
|
|
|
|
| 93 |
feedback = (
|
| 94 |
"Most of Your Text is AI/GPT Generated"
|
| 95 |
if fake_percentage > 50
|
|
|
|
| 107 |
"h": [r["paragraph"] for r in results],
|
| 108 |
"hi": [],
|
| 109 |
"textWords": total_words,
|
| 110 |
+
"aiWords": ai_words,
|
| 111 |
"fakePercentage": fake_percentage,
|
| 112 |
"specialIndexes": [],
|
| 113 |
"specialSentences": [],
|