Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,99 +1,65 @@
|
|
| 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 |
-
|
| 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
|
| 20 |
if len(text.strip()) < 50:
|
| 21 |
return "β οΈ Please provide at least 50 characters for accurate analysis."
|
| 22 |
|
| 23 |
-
#
|
| 24 |
expert_prompt = f"""
|
| 25 |
[ROLE]
|
| 26 |
-
You are Dr. Lexica, a forensic linguistics expert specializing in AI/human text differentiation. Your task is to analyze the provided text
|
| 27 |
|
| 28 |
[TEXT TO ANALYZE]
|
| 29 |
{text}
|
| 30 |
|
| 31 |
[INSTRUCTIONS]
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
| 36 |
|
| 37 |
[REQUIRED OUTPUT FORMAT]
|
| 38 |
# π΅οΈββοΈ Forensic Text Analysis Report
|
| 39 |
-
## π Verdict
|
| 40 |
-
**Origin:** {{Human/AI/Inconclusive}}
|
| 41 |
-
**Confidence Level:** {{XX%}}
|
| 42 |
-
**Detection Score:** {{X/10}}
|
| 43 |
|
| 44 |
-
##
|
| 45 |
-
|
| 46 |
-
-
|
| 47 |
-
-
|
| 48 |
|
| 49 |
-
|
| 50 |
-
- {{Marker 1}}
|
| 51 |
-
- {{Marker 2}}
|
| 52 |
|
| 53 |
-
##
|
| 54 |
-
{{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
"""
|
| 56 |
|
| 57 |
try:
|
| 58 |
if new_openai:
|
| 59 |
response = client.chat.completions.create(
|
| 60 |
-
model="gpt-3.5-turbo",
|
| 61 |
messages=[
|
| 62 |
-
{"role": "system", "content": "You are a forensic text analysis AI."},
|
| 63 |
{"role": "user", "content": expert_prompt}
|
| 64 |
],
|
| 65 |
-
temperature=0.1,
|
| 66 |
-
max_tokens=
|
| 67 |
)
|
| 68 |
return response.choices[0].message.content
|
| 69 |
else:
|
| 70 |
response = openai.ChatCompletion.create(
|
| 71 |
-
model="gpt-3.5-turbo",
|
| 72 |
messages=[
|
| 73 |
-
{"role": "system", "content": "You are a forensic text analysis AI."},
|
| 74 |
{"role": "user", "content": expert_prompt}
|
| 75 |
],
|
| 76 |
temperature=0.1,
|
| 77 |
-
max_tokens=
|
| 78 |
)
|
| 79 |
return response['choices'][0]['message']['content']
|
| 80 |
except Exception as e:
|
| 81 |
-
return f"π΄ Analysis failed. Error: {str(e)}"
|
| 82 |
-
|
| 83 |
-
# Create Gradio interface
|
| 84 |
-
with gr.Blocks(theme=gr.themes.Soft(primary_hue="emerald")) as app:
|
| 85 |
-
gr.Markdown("""# π¬ AI/Human Text Forensic Analyzer""")
|
| 86 |
-
|
| 87 |
-
with gr.Row():
|
| 88 |
-
with gr.Column():
|
| 89 |
-
input_text = gr.Textbox(label="π Text to Analyze", lines=7)
|
| 90 |
-
analyze_btn = gr.Button("π§ͺ Analyze Text", variant="primary")
|
| 91 |
-
|
| 92 |
-
with gr.Column():
|
| 93 |
-
output_text = gr.Markdown(label="π Analysis Report")
|
| 94 |
-
|
| 95 |
-
analyze_btn.click(analyze_text, inputs=input_text, outputs=output_text)
|
| 96 |
-
|
| 97 |
-
# Launch the app
|
| 98 |
-
if __name__ == "__main__":
|
| 99 |
-
app.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
def analyze_text(text):
|
| 2 |
+
"""Advanced forensic analysis with line-by-line proofs and modern conclusion"""
|
| 3 |
if len(text.strip()) < 50:
|
| 4 |
return "β οΈ Please provide at least 50 characters for accurate analysis."
|
| 5 |
|
| 6 |
+
# Enhanced prompt with line-by-line analysis requirement
|
| 7 |
expert_prompt = f"""
|
| 8 |
[ROLE]
|
| 9 |
+
You are Dr. Lexica, a forensic linguistics expert specializing in AI/human text differentiation. Your task is to analyze the provided text line by line and determine its origin, providing specific proofs for each finding.
|
| 10 |
|
| 11 |
[TEXT TO ANALYZE]
|
| 12 |
{text}
|
| 13 |
|
| 14 |
[INSTRUCTIONS]
|
| 15 |
+
1. Analyze each significant line/sentence showing either:
|
| 16 |
+
- "This can't be AI because..." with specific linguistic proof
|
| 17 |
+
- "This suggests AI because..." with specific markers
|
| 18 |
+
2. Provide line numbers or quote specific phrases
|
| 19 |
+
3. Include a modern conclusion about text origin uncertainty
|
| 20 |
+
4. Format output exactly as specified
|
| 21 |
|
| 22 |
[REQUIRED OUTPUT FORMAT]
|
| 23 |
# π΅οΈββοΈ Forensic Text Analysis Report
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
+
## π Line-by-Line Proofs
|
| 26 |
+
**Line X/Y:** "Quote specific text"
|
| 27 |
+
- π’ Human indicator: Explanation why this can't be AI-generated
|
| 28 |
+
- π΄ AI indicator: Explanation why this is unlikely from human
|
| 29 |
|
| 30 |
+
(Repeat for each significant line)
|
|
|
|
|
|
|
| 31 |
|
| 32 |
+
## π Final Assessment
|
| 33 |
+
**Verdict:** {{Human/AI/Uncertain}}
|
| 34 |
+
**Confidence:** {{XX%}}
|
| 35 |
+
**Score:** {{X/10}} (0=Human, 10=AI)
|
| 36 |
+
|
| 37 |
+
## π‘ Modern Conclusion
|
| 38 |
+
In today's digital age, distinguishing AI from human text has become increasingly challenging. While my analysis suggests [VERDICT], this conclusion can be challenged because [REASON]. The boundaries between human and machine writing continue to blur, making absolute certainty impossible. My suggestion is based on current linguistic patterns, but new AI models may overcome these limitations.
|
| 39 |
"""
|
| 40 |
|
| 41 |
try:
|
| 42 |
if new_openai:
|
| 43 |
response = client.chat.completions.create(
|
| 44 |
+
model="gpt-3.5-turbo",
|
| 45 |
messages=[
|
| 46 |
+
{"role": "system", "content": "You are a forensic text analysis AI that provides specific proofs."},
|
| 47 |
{"role": "user", "content": expert_prompt}
|
| 48 |
],
|
| 49 |
+
temperature=0.1,
|
| 50 |
+
max_tokens=700 # Increased for detailed analysis
|
| 51 |
)
|
| 52 |
return response.choices[0].message.content
|
| 53 |
else:
|
| 54 |
response = openai.ChatCompletion.create(
|
| 55 |
+
model="gpt-3.5-turbo",
|
| 56 |
messages=[
|
| 57 |
+
{"role": "system", "content": "You are a forensic text analysis AI that provides specific proofs."},
|
| 58 |
{"role": "user", "content": expert_prompt}
|
| 59 |
],
|
| 60 |
temperature=0.1,
|
| 61 |
+
max_tokens=700
|
| 62 |
)
|
| 63 |
return response['choices'][0]['message']['content']
|
| 64 |
except Exception as e:
|
| 65 |
+
return f"π΄ Analysis failed. Error: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|