hashirlodhi commited on
Commit
75413cc
·
verified ·
1 Parent(s): 8dc41a0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +96 -229
app.py CHANGED
@@ -2,248 +2,115 @@ import gradio as gr
2
  import openai
3
  import os
4
  from dotenv import load_dotenv
5
- from typing import Dict, List
6
- import json
7
 
8
  # Load environment variables
9
  load_dotenv()
10
 
11
- # Initialize OpenAI
12
  try:
 
13
  from openai import OpenAI
14
  client = OpenAI(api_key=os.getenv("OPENAI_API_KEY").strip())
15
  new_openai = True
16
  except ImportError:
 
17
  openai.api_key = os.getenv("OPENAI_API_KEY").strip()
18
  new_openai = False
19
 
20
- class ForensicAgent:
21
- """Base class for forensic analysis agents"""
22
- def __init__(self, role: str, expertise: str):
23
- self.role = role
24
- self.expertise = expertise
25
 
26
- def analyze(self, text: str) -> Dict:
27
- """Perform analysis and return findings"""
28
- raise NotImplementedError
29
-
30
- def _get_analysis(self, prompt: str) -> Dict:
31
- """Generic OpenAI request handler"""
32
- try:
33
- if new_openai:
34
- response = client.chat.completions.create(
35
- model="gpt-4",
36
- messages=[
37
- {"role": "system", "content": f"You are {self.role}, {self.expertise}."},
38
- {"role": "user", "content": prompt}
39
- ],
40
- temperature=0.1,
41
- response_format={"type": "json_object"}
42
- )
43
- return json.loads(response.choices[0].message.content)
44
- else:
45
- response = openai.ChatCompletion.create(
46
- model="gpt-4",
47
- messages=[
48
- {"role": "system", "content": f"You are {self.role}, {self.expertise}."},
49
- {"role": "user", "content": prompt}
50
- ],
51
- temperature=0.1,
52
- response_format={"type": "json_object"}
53
- )
54
- return json.loads(response['choices'][0]['message']['content'])
55
- except Exception as e:
56
- return {"error": str(e), "verdict": "Error", "confidence": "0%"}
57
-
58
- class StylometricAgent(ForensicAgent):
59
- """Analyzes writing style characteristics"""
60
- def __init__(self):
61
- super().__init__("Dr. Styles", "Stylometric Analysis Expert")
62
-
63
- def analyze(self, text: str) -> Dict:
64
- prompt = f"""
65
- Analyze this text for stylometric patterns:
66
- {text}
67
-
68
- Return JSON with: verdict (Human/AI/Uncertain), confidence (0-100%), and evidence list.
69
- Focus on:
70
- - Sentence length variation
71
- - Lexical diversity
72
- - Punctuation patterns
73
- - Paragraph structure
74
- """
75
- return self._get_analysis(prompt)
76
-
77
- class CognitiveAgent(ForensicAgent):
78
- """Analyzes cognitive and psychological markers"""
79
- def __init__(self):
80
- super().__init__("Prof. Cognitus", "Cognitive Linguistics Specialist")
81
-
82
- def analyze(self, text: str) -> Dict:
83
- prompt = f"""
84
- Analyze this text for cognitive markers:
85
- {text}
86
-
87
- Return JSON with: verdict (Human/AI/Uncertain), confidence (0-100%), and evidence list.
88
- Focus on:
89
- - Hedging language
90
- - Personal pronouns/anecdotes
91
- - Emotional expressions
92
- - Confidence markers
93
- """
94
- return self._get_analysis(prompt)
95
-
96
- class SemanticAgent(ForensicAgent):
97
- """Analyzes semantic and contextual patterns"""
98
- def __init__(self):
99
- super().__init__("Dr. Semantica", "Semantic Forensics Expert")
100
-
101
- def analyze(self, text: str) -> Dict:
102
- prompt = f"""
103
- Analyze this text for semantic patterns:
104
- {text}
105
-
106
- Return JSON with: verdict (Human/AI/Uncertain), confidence (0-100%), and evidence list.
107
- Focus on:
108
- - Metaphor/idiom density
109
- - Contextual references
110
- - Temporal references
111
- - Error patterns
112
- """
113
- return self._get_analysis(prompt)
114
-
115
- class ForensicCrew:
116
- """Orchestrates multiple forensic agents"""
117
- def __init__(self):
118
- self.agents = [
119
- StylometricAgent(),
120
- CognitiveAgent(),
121
- SemanticAgent()
122
- ]
123
-
124
- def analyze_text(self, text: str) -> str:
125
- """Coordinate multi-agent analysis"""
126
- if len(text.strip()) < 30:
127
- return "⚠️ Please provide at least 50 characters for accurate analysis."
128
-
129
- try:
130
- # Get all agent findings
131
- findings = []
132
- for agent in self.agents:
133
- result = agent.analyze(text)
134
- findings.append(result)
135
- if "error" in result:
136
- print(f"Agent {agent.role} error: {result['error']}")
137
-
138
- # Generate final report
139
- return self._synthesize_findings(text, findings)
140
- except Exception as e:
141
- return f"🔴 System Error: {str(e)}"
142
-
143
- def _synthesize_findings(self, text: str, findings: List[Dict]) -> str:
144
- """Generate final report"""
145
- try:
146
- prompt = f"""
147
- Text under analysis:
148
- {text}
149
-
150
- Agent findings:
151
- {json.dumps(findings, indent=2)}
152
-
153
- Compile a forensic report with:
154
- 1. Composite verdict (Human/AI/Uncertain)
155
- 2. Confidence score (average of agent confidences)
156
- 3. Key evidence from each agent
157
- 4. Final conclusion
158
- """
159
-
160
- if new_openai:
161
- response = client.chat.completions.create(
162
- model="gpt-4",
163
- messages=[
164
- {"role": "system", "content": "You are Dr. Lexica, Chief Forensic Analyst."},
165
- {"role": "user", "content": prompt}
166
- ],
167
- temperature=0.1,
168
- max_tokens=800
169
- )
170
- return response.choices[0].message.content
171
- else:
172
- response = openai.ChatCompletion.create(
173
- model="gpt-4",
174
- messages=[
175
- {"role": "system", "content": "You are Dr. Lexica, Chief Forensic Analyst."},
176
- {"role": "user", "content": prompt}
177
- ],
178
- temperature=0.1,
179
- max_tokens=800
180
- )
181
- return response['choices'][0]['message']['content']
182
- except Exception as e:
183
- return f"🔴 Report Generation Failed: {str(e)}"
184
-
185
- # Initialize the forensic crew
186
- crew = ForensicCrew()
187
-
188
- # Gradio Interface
189
- with gr.Blocks(theme=gr.themes.Soft(primary_hue="emerald")) as app:
190
- gr.Markdown("""
191
- # 🔬 AI/Human Text Forensic Analyzer (CrewAI)
192
- *Multi-agent analysis combining stylometric, cognitive, and semantic techniques*
193
- """)
194
-
195
- with gr.Row():
196
- with gr.Column():
197
- input_text = gr.Textbox(
198
- label="📝 Text to Analyze",
199
- lines=7,
200
- placeholder="Paste any text (50+ characters recommended)..."
201
  )
202
- with gr.Row():
203
- analyze_btn = gr.Button("🧪 Analyze Text", variant="primary")
204
- clear_btn = gr.Button("🔄 Clear")
205
-
206
- with gr.Column():
207
- output_text = gr.Markdown(
208
- label="📜 Forensic Analysis Report",
209
- elem_classes=["output-panel"]
 
 
210
  )
211
-
212
- # Example texts
213
- gr.Examples(
214
- examples=[
215
- ["The rain tapped gently against the window as I reminisced about childhood summers - those endless days that now seem like someone else's memory."],
216
- ["Large language models utilize transformer architectures to generate human-like text through probabilistic prediction mechanisms."],
217
- ["I dunno... it's kinda weird how phones these days track everything. Makes me uncomfortable tbh."]
218
- ],
219
- inputs=input_text,
220
- label="💡 Try these examples:"
221
- )
222
-
223
- # Button actions
224
- analyze_btn.click(
225
- fn=crew.analyze_text,
226
- inputs=input_text,
227
- outputs=output_text,
228
- api_name="analyze"
229
- )
230
-
231
- clear_btn.click(
232
- fn=lambda: ("", ""),
233
- inputs=None,
234
- outputs=[input_text, output_text]
235
- )
236
-
237
- # Launch with debug mode
238
- if __name__ == "__main__":
239
- print("🚀 Starting Forensic Analyzer...")
240
- try:
241
- # Test API connection
242
- test_prompt = "Hello, world!"
243
- test_result = crew.analyze_text(test_prompt)
244
- print(f"Test analysis result: {test_result[:100]}...")
245
-
246
- # Launch app
247
- app.launch(server_port=7860, show_error=True)
248
  except Exception as e:
249
- print(f" Failed to launch: {str(e)}")
 
 
 
2
  import openai
3
  import os
4
  from dotenv import load_dotenv
 
 
5
 
6
  # Load environment variables
7
  load_dotenv()
8
 
9
+ # Initialize OpenAI (compatible with both old and new versions)
10
  try:
11
+ # Try new OpenAI client first
12
  from openai import OpenAI
13
  client = OpenAI(api_key=os.getenv("OPENAI_API_KEY").strip())
14
  new_openai = True
15
  except ImportError:
16
+ # Fall back to old version
17
  openai.api_key = os.getenv("OPENAI_API_KEY").strip()
18
  new_openai = False
19
 
20
+ def analyze_text(text):
21
+ """Advanced forensic analysis with improved prompt and formatted output"""
 
 
 
22
 
23
+ expert_prompt = f"""
24
+ [ROLE]
25
+ You are Dr. Lexica, a world-renowned forensic linguistics expert specializing in AI/human text differentiation with 25 years of experience.
26
+
27
+ [ANALYSIS TASK]
28
+ Perform a comprehensive forensic analysis on the following text to determine its origin with maximum accuracy:
29
+
30
+ [TEXT TO ANALYZE]
31
+ {text}
32
+
33
+ [ANALYSIS FRAMEWORK]
34
+ 1. **Stylometric Analysis**:
35
+ - Sentence structure complexity (human: varied, AI: uniform)
36
+ - Lexical sophistication (human: contextual, AI: textbook-like)
37
+ - Punctuation patterns (human: emotional, AI: formulaic)
38
+
39
+ 2. **Cognitive Fingerprinting**:
40
+ - Presence of hedges ("maybe", "I think") → human
41
+ - Overconfidence markers ("clearly", "undoubtedly") → AI
42
+ - Personal anecdotes → human
43
+ - Generic statements → AI
44
+
45
+ 3. **Semantic Forensics**:
46
+ - Metaphor density (human: 3-5 per 100 words, AI: 0-2)
47
+ - Contextual anchoring (human: specific references, AI: vague)
48
+ - Error patterns (human: typos, AI: semantic inconsistencies)
49
+
50
+ 4. **Temporal Markers**:
51
+ - References to time/age (human: specific dates, AI: generic)
52
+ - Cultural references (human: nuanced, AI: stereotypical)
53
+
54
+ [REQUIRED OUTPUT FORMAT]
55
+ # 🕵️‍♂️ Forensic Text Analysis Report
56
+
57
+ ## 🔍 Verdict
58
+ **Origin:** {{Human/AI/Inconclusive}}
59
+ **Confidence Level:** {{XX%}}
60
+ **Detection Score:** {{X/10}}
61
+
62
+ ## 📊 Key Indicators
63
+ 🟢 **Human Markers Found:**
64
+ - {{Marker 1}}
65
+ - {{Marker 2}}
66
+
67
+ 🔴 **AI Markers Found:**
68
+ - {{Marker 1}}
69
+ - {{Marker 2}}
70
+
71
+ ## 🧐 Detailed Findings
72
+ ### 1️⃣ Stylometric Evidence
73
+ - {{Analysis point 1}}
74
+ - {{Analysis point 2}}
75
+
76
+ ### 2️⃣ Cognitive Patterns
77
+ - {{Analysis point 1}}
78
+ - {{Analysis point 2}}
79
+
80
+ ### 3️⃣ Semantic Fingerprints
81
+ - {{Analysis point 1}}
82
+ - {{Analysis point 2}}
83
+
84
+ ## 💡 Expert Conclusion
85
+ {{3-4 sentence authoritative conclusion with final determination. Highlight any interesting nuances or exceptional cases.}}
86
+
87
+ ⚠️ **Disclaimer:** This analysis has {{confidence}}% accuracy based on current forensic linguistics models.
88
+ """
89
+
90
+ try:
91
+ if new_openai:
92
+ response = client.chat.completions.create(
93
+ model="gpt-4", # Changed to GPT-4
94
+ messages=[
95
+ {"role": "system", "content": "You are a forensic text analysis AI specializing in detecting AI-generated content with extreme precision."},
96
+ {"role": "user", "content": expert_prompt}
97
+ ],
98
+ temperature=0.1,
99
+ max_tokens=500
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
100
  )
101
+ return response.choices[0].message.content
102
+ else:
103
+ response = openai.ChatCompletion.create(
104
+ model="gpt-4", # Changed to GPT-4
105
+ messages=[
106
+ {"role": "system", "content": "You are a forensic text analysis AI specializing in detecting AI-generated content with extreme precision."},
107
+ {"role": "user", "content": expert_prompt}
108
+ ],
109
+ temperature=0.1,
110
+ max_tokens=500
111
  )
112
+ return response['choices'][0]['message']['content']
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
113
  except Exception as e:
114
+ return f"🔴 Analysis failed. Error: {str(e)}"
115
+
116
+ # Rest of your Gradio interface remains the same...