Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,54 +6,29 @@ from dotenv import load_dotenv
|
|
| 6 |
# Load environment variables
|
| 7 |
load_dotenv()
|
| 8 |
|
| 9 |
-
# Initialize OpenAI
|
| 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%}}
|
|
@@ -68,31 +43,16 @@ def analyze_text(text):
|
|
| 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
|
| 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",
|
| 94 |
messages=[
|
| 95 |
-
{"role": "system", "content": "You are a forensic text analysis AI
|
| 96 |
{"role": "user", "content": expert_prompt}
|
| 97 |
],
|
| 98 |
temperature=0.1,
|
|
@@ -101,9 +61,9 @@ def analyze_text(text):
|
|
| 101 |
return response.choices[0].message.content
|
| 102 |
else:
|
| 103 |
response = openai.ChatCompletion.create(
|
| 104 |
-
model="gpt-4",
|
| 105 |
messages=[
|
| 106 |
-
{"role": "system", "content": "You are a forensic text analysis AI
|
| 107 |
{"role": "user", "content": expert_prompt}
|
| 108 |
],
|
| 109 |
temperature=0.1,
|
|
@@ -113,4 +73,20 @@ def analyze_text(text):
|
|
| 113 |
except Exception as e:
|
| 114 |
return f"π΄ Analysis failed. Error: {str(e)}"
|
| 115 |
|
| 116 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
# Load environment variables
|
| 7 |
load_dotenv()
|
| 8 |
|
| 9 |
+
# Initialize OpenAI
|
| 10 |
try:
|
|
|
|
| 11 |
from openai import OpenAI
|
| 12 |
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY").strip())
|
| 13 |
new_openai = True
|
| 14 |
except ImportError:
|
|
|
|
| 15 |
openai.api_key = os.getenv("OPENAI_API_KEY").strip()
|
| 16 |
new_openai = False
|
| 17 |
|
| 18 |
def analyze_text(text):
|
| 19 |
"""Advanced forensic analysis with improved prompt and formatted output"""
|
| 20 |
+
if len(text.strip()) < 50:
|
| 21 |
+
return "β οΈ Please provide at least 50 characters for accurate analysis."
|
| 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 |
[TEXT TO ANALYZE]
|
| 28 |
{text}
|
| 29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
[REQUIRED OUTPUT FORMAT]
|
| 31 |
# π΅οΈββοΈ Forensic Text Analysis Report
|
|
|
|
| 32 |
## π Verdict
|
| 33 |
**Origin:** {{Human/AI/Inconclusive}}
|
| 34 |
**Confidence Level:** {{XX%}}
|
|
|
|
| 43 |
- {{Marker 1}}
|
| 44 |
- {{Marker 2}}
|
| 45 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
## π‘ Expert Conclusion
|
| 47 |
+
{{3-4 sentence conclusion}}
|
|
|
|
|
|
|
| 48 |
"""
|
| 49 |
|
| 50 |
try:
|
| 51 |
if new_openai:
|
| 52 |
response = client.chat.completions.create(
|
| 53 |
+
model="gpt-4",
|
| 54 |
messages=[
|
| 55 |
+
{"role": "system", "content": "You are a forensic text analysis AI."},
|
| 56 |
{"role": "user", "content": expert_prompt}
|
| 57 |
],
|
| 58 |
temperature=0.1,
|
|
|
|
| 61 |
return response.choices[0].message.content
|
| 62 |
else:
|
| 63 |
response = openai.ChatCompletion.create(
|
| 64 |
+
model="gpt-4",
|
| 65 |
messages=[
|
| 66 |
+
{"role": "system", "content": "You are a forensic text analysis AI."},
|
| 67 |
{"role": "user", "content": expert_prompt}
|
| 68 |
],
|
| 69 |
temperature=0.1,
|
|
|
|
| 73 |
except Exception as e:
|
| 74 |
return f"π΄ Analysis failed. Error: {str(e)}"
|
| 75 |
|
| 76 |
+
# Create Gradio interface
|
| 77 |
+
with gr.Blocks(theme=gr.themes.Soft(primary_hue="emerald")) as app:
|
| 78 |
+
gr.Markdown("""# π¬ AI/Human Text Forensic Analyzer""")
|
| 79 |
+
|
| 80 |
+
with gr.Row():
|
| 81 |
+
with gr.Column():
|
| 82 |
+
input_text = gr.Textbox(label="π Text to Analyze", lines=7)
|
| 83 |
+
analyze_btn = gr.Button("π§ͺ Analyze Text", variant="primary")
|
| 84 |
+
|
| 85 |
+
with gr.Column():
|
| 86 |
+
output_text = gr.Markdown(label="π Analysis Report")
|
| 87 |
+
|
| 88 |
+
analyze_btn.click(analyze_text, inputs=input_text, outputs=output_text)
|
| 89 |
+
|
| 90 |
+
# Launch the app
|
| 91 |
+
if __name__ == "__main__":
|
| 92 |
+
app.launch()
|