hashirlodhi commited on
Commit
0059c02
·
verified ·
1 Parent(s): 276462a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +88 -156
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
- from typing import Tuple
 
 
7
 
8
- # Initialize the LLM - Using GPT-4o-mini for cost-effectiveness
9
- llm = ChatOpenAI(
10
- model='gpt-4-0125-preview', # GPT-4 Turbo (most cost-effective while powerful)
11
- temperature=0.3, # Lower temperature for more deterministic results
12
- )
13
 
14
- def setup_agents() -> Tuple[Agent, Agent, Agent]:
15
- """Setup and return the three analysis agents with enhanced prompts"""
16
 
17
- # 📝 Enhanced Linguistic Pattern Analyzer
18
- pattern_analyzer = Agent(
19
- role='Senior Linguistic Forensic Analyst',
20
- goal='Detect subtle linguistic markers that distinguish human from AI writing',
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
- # 🔍 Enhanced Coherence Evaluator
31
- coherence_evaluator = Agent(
32
- role='Cognitive Flow Specialist',
33
- goal='Evaluate text for natural human thought progression vs algorithmic patterns',
34
- backstory=(
35
- "With a PhD in cognitive linguistics, you've published numerous papers on "
36
- "how humans naturally organize thoughts in writing. You can spot the "
37
- "overly-perfect structure of AI text versus organic human flow."
38
- ),
39
- llm=llm,
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
- # Setup agents
62
- pattern_analyzer, coherence_evaluator, verdict_compiler = setup_agents()
 
 
63
 
64
- # Enhanced Task 1 - Deep Linguistic Analysis
65
- task1 = Task(
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
- # Enhanced Task 2 - Cognitive Flow Analysis
86
- task2 = Task(
87
- description=(
88
- f"Analyze the cognitive flow of this text:\n\n{input_text}\n\n"
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
- # Enhanced Task 3 - Weighted Verdict
107
- task3 = Task(
108
- description=(
109
- "Synthesize all forensic evidence into a final verdict.\n"
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
- # Create and run the Crew with execution timeout
129
- crew = Crew(
130
- agents=[pattern_analyzer, coherence_evaluator, verdict_compiler],
131
- tasks=[task1, task2, task3],
132
- memory=True, # Enable agent memory for context
133
- verbose=2 # Detailed execution logging
134
- )
135
 
136
  try:
137
- result = crew.kickoff(inputs={'input_text': input_text})
138
- return result
 
 
 
 
 
 
 
 
139
  except Exception as e:
140
- return f"Analysis failed: {str(e)}"
141
 
142
- # Enhanced Gradio Interface
143
- iface = gr.Interface(
144
- fn=analyze_text,
145
- inputs=gr.Textbox(
146
- label="📝 Text to Analyze",
147
- placeholder="Paste any text to check if it was written by human or AI...",
148
- lines=7
149
- ),
150
- outputs=gr.Textbox(
151
- label="🔍 Analysis Result",
152
- lines=10,
153
- interactive=False
154
- ),
155
- title="🤖 Advanced AI Text Detector",
156
- description=(
157
- "This forensic analysis tool examines writing patterns to determine "
158
- "if text was likely written by a human or AI. "
159
- "Uses multi-agent analysis with GPT-4o-mini for cost-effective accuracy."
160
- ),
161
- examples=[
162
- ["The quick brown fox jumps over the lazy dog. This simple sentence contains all letters of the English alphabet."],
163
- ["While the geopolitical ramifications of decentralized governance structures remain contested, their ontological underpinnings suggest..."],
164
- ["I was walking down the street when I saw this cute dog. It made me remember my childhood pet, Sparky, who always..."]
165
- ],
166
- allow_flagging="never",
167
- theme="soft"
168
- )
 
 
 
 
 
 
169
 
170
- if __name__ == "__main__":
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()