hashirlodhi commited on
Commit
c58b6d1
Β·
verified Β·
1 Parent(s): 10458da

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -63
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 improved prompt and formatted output using GPT-3.5 Turbo"""
20
  if len(text.strip()) < 50:
21
  return "⚠️ Please provide at least 50 characters for accurate analysis."
22
 
23
- # Optimized prompt for GPT-3.5 Turbo
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 and determine if it was written by a human or generated by an AI, providing a clear verdict, confidence level, and specific indicators.
27
 
28
  [TEXT TO ANALYZE]
29
  {text}
30
 
31
  [INSTRUCTIONS]
32
- - Analyze linguistic patterns such as word choice, sentence structure, coherence, and stylistic nuances.
33
- - Look for human markers (e.g., emotional tone, idiosyncratic phrasing, inconsistencies) and AI markers (e.g., formulaic patterns, overly polished language, predictable transitions).
34
- - Provide a confidence level (0-100%) and a detection score (0-10, where 0 is human, 10 is AI).
35
- - Format the output exactly as specified below.
 
 
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
- ## πŸ“Š Key Indicators
45
- 🟒 **Human Markers Found:**
46
- - {{Marker 1}}
47
- - {{Marker 2}}
48
 
49
- πŸ”΄ **AI Markers Found:**
50
- - {{Marker 1}}
51
- - {{Marker 2}}
52
 
53
- ## πŸ’‘ Expert Conclusion
54
- {{3-4 sentence conclusion summarizing the analysis}}
 
 
 
 
 
55
  """
56
 
57
  try:
58
  if new_openai:
59
  response = client.chat.completions.create(
60
- model="gpt-3.5-turbo", # Changed to 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, # Low temperature for consistent output
66
- max_tokens=500
67
  )
68
  return response.choices[0].message.content
69
  else:
70
  response = openai.ChatCompletion.create(
71
- model="gpt-3.5-turbo", # Changed to 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=500
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)}"