Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,171 +1,103 @@
|
|
| 1 |
-
# -*- coding: utf-8 -*-
|
| 2 |
-
from crewai import Agent, Task, Crew
|
| 3 |
-
from langchain_openai import ChatOpenAI
|
| 4 |
-
import os
|
| 5 |
import gradio as gr
|
| 6 |
-
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
temperature=0.3, # Lower temperature for more deterministic results
|
| 12 |
-
)
|
| 13 |
|
| 14 |
-
def
|
| 15 |
-
"""
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
backstory=(
|
| 22 |
-
"You're a leading expert in forensic linguistics with 15 years experience "
|
| 23 |
-
"analyzing writing styles. You specialize in identifying subtle patterns in "
|
| 24 |
-
"word choice, sentence structure, and rhetorical devices that reveal authorship."
|
| 25 |
-
),
|
| 26 |
-
llm=llm,
|
| 27 |
-
verbose=True
|
| 28 |
-
)
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
verbose=True
|
| 41 |
-
)
|
| 42 |
-
|
| 43 |
-
# 🧠 Enhanced Verdict Compiler
|
| 44 |
-
verdict_compiler = Agent(
|
| 45 |
-
role='Chief Detection Analyst',
|
| 46 |
-
goal='Synthesize all evidence into a weighted, probabilistic assessment',
|
| 47 |
-
backstory=(
|
| 48 |
-
"As head of the AI Detection Task Force, you've developed proprietary "
|
| 49 |
-
"algorithms that combine multiple linguistic dimensions into accurate "
|
| 50 |
-
"authorship attribution models with confidence scoring."
|
| 51 |
-
),
|
| 52 |
-
llm=llm,
|
| 53 |
-
verbose=True
|
| 54 |
-
)
|
| 55 |
-
|
| 56 |
-
return pattern_analyzer, coherence_evaluator, verdict_compiler
|
| 57 |
-
|
| 58 |
-
def analyze_text(input_text: str) -> str:
|
| 59 |
-
"""Analyze text with enhanced tasks and multi-stage verification"""
|
| 60 |
|
| 61 |
-
|
| 62 |
-
|
|
|
|
|
|
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
description=(
|
| 67 |
-
f"Conduct a thorough forensic analysis of this text:\n\n{input_text}\n\n"
|
| 68 |
-
"Examine: \n"
|
| 69 |
-
"1. Word rarity and sophistication distribution\n"
|
| 70 |
-
"2. Pronoun frequency and referential coherence\n"
|
| 71 |
-
"3. Sentence length variability\n"
|
| 72 |
-
"4. Presence of idiosyncratic phrasing\n"
|
| 73 |
-
"5. Burstiness in writing rhythm\n"
|
| 74 |
-
"Provide specific metrics and comparative analysis."
|
| 75 |
-
),
|
| 76 |
-
expected_output=(
|
| 77 |
-
"A detailed 4-5 sentence report with:\n"
|
| 78 |
-
"- Specific linguistic patterns identified\n"
|
| 79 |
-
"- Statistical observations\n"
|
| 80 |
-
"- Comparative human/AI likelihood percentages"
|
| 81 |
-
),
|
| 82 |
-
agent=pattern_analyzer
|
| 83 |
-
)
|
| 84 |
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
"Evaluate:\n"
|
| 90 |
-
"1. Idea progression naturalness\n"
|
| 91 |
-
"2. Contextual depth maintenance\n"
|
| 92 |
-
"3. Argumentative thread consistency\n"
|
| 93 |
-
"4. Error patterns (humans make different types)\n"
|
| 94 |
-
"5. Self-correction frequency"
|
| 95 |
-
),
|
| 96 |
-
expected_output=(
|
| 97 |
-
"A 4-5 sentence evaluation with:\n"
|
| 98 |
-
"- Flow consistency score (1-10)\n"
|
| 99 |
-
"- Notable cognitive patterns\n"
|
| 100 |
-
"- Human/AI probability estimate"
|
| 101 |
-
),
|
| 102 |
-
agent=coherence_evaluator,
|
| 103 |
-
depends_on=[task1]
|
| 104 |
-
)
|
| 105 |
|
| 106 |
-
#
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
"Consider:\n"
|
| 111 |
-
"1. Linguistic analysis confidence\n"
|
| 112 |
-
"2. Cognitive flow scores\n"
|
| 113 |
-
"3. Statistical deviations from human norms\n"
|
| 114 |
-
"4. Known AI generation patterns\n\n"
|
| 115 |
-
"Provide a confidence-weighted conclusion."
|
| 116 |
-
),
|
| 117 |
-
expected_output=(
|
| 118 |
-
"A comprehensive 5-6 line report containing:\n"
|
| 119 |
-
"- Final determination (Human/AI)\n"
|
| 120 |
-
"- Confidence percentage (0-100%)\n"
|
| 121 |
-
"- Key supporting evidence\n"
|
| 122 |
-
"- Limitations/uncertainties noted"
|
| 123 |
-
),
|
| 124 |
-
agent=verdict_compiler,
|
| 125 |
-
depends_on=[task1, task2]
|
| 126 |
-
)
|
| 127 |
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
|
| 136 |
try:
|
| 137 |
-
|
| 138 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 139 |
except Exception as e:
|
| 140 |
-
return f"
|
| 141 |
|
| 142 |
-
#
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
lines=
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 169 |
|
| 170 |
-
|
| 171 |
-
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import openai
|
| 3 |
+
import os
|
| 4 |
+
from dotenv import load_dotenv
|
| 5 |
|
| 6 |
+
# Load environment variables
|
| 7 |
+
load_dotenv()
|
| 8 |
+
openai.api_key = os.getenv("OPENAI_API_KEY") # Set in .env file
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
def analyze_text(text):
|
| 11 |
+
"""Advanced analysis using a sophisticated prompt"""
|
| 12 |
|
| 13 |
+
expert_prompt = f"""
|
| 14 |
+
[ROLE]
|
| 15 |
+
You are Dr. Textoria, a forensic linguistics expert with 20 years of experience in authorship attribution.
|
| 16 |
+
Your task is to analyze the following text to determine if it was written by a human or AI.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
+
[ANALYSIS CRITERIA]
|
| 19 |
+
1. Stylometric Features:
|
| 20 |
+
- Sentence length variation (human: high, AI: low)
|
| 21 |
+
- Lexical diversity (human: medium-high, AI: very high or very low)
|
| 22 |
+
- Punctuation patterns (human: irregular, AI: formulaic)
|
| 23 |
+
|
| 24 |
+
2. Cognitive Markers:
|
| 25 |
+
- Hedges ("perhaps", "maybe") - common in humans
|
| 26 |
+
- Boosters ("clearly", "obviously") - common in AI
|
| 27 |
+
- Self-references ("I think", "in my experience")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
+
3. Semantic Features:
|
| 30 |
+
- Contextual anchoring (human: specific, AI: generic)
|
| 31 |
+
- Metaphor density (human: moderate, AI: low)
|
| 32 |
+
- Error patterns (human: typos, AI: coherence breaks)
|
| 33 |
|
| 34 |
+
[TEXT TO ANALYZE]
|
| 35 |
+
{text}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
+
[OUTPUT FORMAT]
|
| 38 |
+
## Forensic Analysis Report
|
| 39 |
+
**Verdict:** {{Human/AI/Uncertain}}
|
| 40 |
+
**Confidence:** {{XX%}}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
+
### Key Evidence:
|
| 43 |
+
1. {{Most significant marker}}
|
| 44 |
+
2. {{Secondary marker}}
|
| 45 |
+
3. {{Tertiary marker}}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
+
**Diagnostic Notes:**
|
| 48 |
+
- {{Interesting observation 1}}
|
| 49 |
+
- {{Interesting observation 2}}
|
| 50 |
+
|
| 51 |
+
**Final Assessment:**
|
| 52 |
+
{{3-4 sentence expert conclusion}}
|
| 53 |
+
"""
|
| 54 |
|
| 55 |
try:
|
| 56 |
+
response = openai.ChatCompletion.create(
|
| 57 |
+
model="gpt-4",
|
| 58 |
+
messages=[
|
| 59 |
+
{"role": "system", "content": "You are a forensic text analysis expert."},
|
| 60 |
+
{"role": "user", "content": expert_prompt}
|
| 61 |
+
],
|
| 62 |
+
temperature=0.2,
|
| 63 |
+
max_tokens=400
|
| 64 |
+
)
|
| 65 |
+
return response.choices[0].message.content
|
| 66 |
except Exception as e:
|
| 67 |
+
return f"Error: {str(e)}"
|
| 68 |
|
| 69 |
+
# Gradio Interface
|
| 70 |
+
with gr.Blocks(theme=gr.themes.Soft()) as app:
|
| 71 |
+
gr.Markdown("# 🔍 Advanced AI/Human Text Analyzer")
|
| 72 |
+
gr.Markdown("Detect AI-generated content using forensic linguistics")
|
| 73 |
+
|
| 74 |
+
with gr.Row():
|
| 75 |
+
input_text = gr.Textbox(label="Input Text", lines=5, placeholder="Paste text here (min 100 characters)...")
|
| 76 |
+
output_text = gr.Markdown(label="Analysis Report")
|
| 77 |
+
|
| 78 |
+
with gr.Row():
|
| 79 |
+
analyze_btn = gr.Button("Analyze", variant="primary")
|
| 80 |
+
clear_btn = gr.Button("Clear")
|
| 81 |
+
|
| 82 |
+
examples = gr.Examples(
|
| 83 |
+
examples=[
|
| 84 |
+
["The quick brown fox jumps over the lazy dog. This simple sentence contains all letters of the English alphabet."],
|
| 85 |
+
["As I walked through the park yesterday, the crisp autumn leaves crunched beneath my feet, reminding me of childhood visits to my grandmother's house in Vermont."],
|
| 86 |
+
["While large language models demonstrate impressive capabilities in text generation, their outputs often lack the subtle cognitive markers characteristic of human authorship."]
|
| 87 |
+
],
|
| 88 |
+
inputs=input_text
|
| 89 |
+
)
|
| 90 |
+
|
| 91 |
+
analyze_btn.click(
|
| 92 |
+
fn=analyze_text,
|
| 93 |
+
inputs=input_text,
|
| 94 |
+
outputs=output_text
|
| 95 |
+
)
|
| 96 |
+
|
| 97 |
+
clear_btn.click(
|
| 98 |
+
fn=lambda: ("", ""),
|
| 99 |
+
inputs=None,
|
| 100 |
+
outputs=[input_text, output_text]
|
| 101 |
+
)
|
| 102 |
|
| 103 |
+
app.launch()
|
|
|