Text_Analyzer / app.py
hashirlodhi's picture
Update app.py
ce981c0 verified
import gradio as gr
import openai
import os
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
# Initialize OpenAI
try:
from openai import OpenAI
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY").strip())
new_openai = True
except ImportError:
openai.api_key = os.getenv("OPENAI_API_KEY").strip()
new_openai = False
def analyze_text(text):
"""Forensic analysis focusing on 3-5 biggest logical proofs"""
if len(text.strip()) < 50:
return "πŸ”Write Your Text here for Analysis!"
prompt = f"""
You are a Text Analyzer Anna with experience of 40 Years in this field, and you are an expert in Analyzing Text, whether a text is written
by AI or not. Conduct a forensic analysis of this text and identify the 3-5 BIGGEST, MOST LOGICAL proofs
that definitively show whether it's human or AI-generated. Focus only on the most undeniable evidence.
TEXT:
{text}
RESPONSE FORMAT:
# πŸ” Ultimate Text Verdict
## 🎯 Top Irrefutable Proofs
1. **Proof** (Human/AI): "[Exact quote]"
β€’ **Why it's conclusive**: [Clear, logical explanation why this is impossible for AI/human to produce]
2. **Proof** (Human/AI): "[Exact quote]"
β€’ **Why it's conclusive**: [Bulletproof reasoning]
3. **Proof** (Human/AI): "[Exact quote]"
β€’ **Why it's conclusive**: [Air-tight argument]
## 🏁 Final Judgment
**Verdict**: [Human/AI/Uncertain]
**Confidence**: [XX% certainty]
**Final Note**: "In today's AI era, while this analysis presents strong evidence, the possibility of error remains."
"""
try:
if new_openai:
response = client.chat.completions.create(
model="gpt-4", # Using GPT-4 for stronger logical analysis
messages=[
{"role": "system", "content": "You are a forensic expert who only identifies the most undeniable proofs."},
{"role": "user", "content": prompt}
],
temperature=0.1,
max_tokens=500
)
return response.choices[0].message.content
else:
response = openai.ChatCompletion.create(
model="gpt-4",
messages=[
{"role": "system", "content": "You are a forensic expert who only identifies the most undeniable proofs."},
{"role": "user", "content": prompt}
],
temperature=0.1,
max_tokens=500
)
return response['choices'][0]['message']['content']
except Exception as e:
return f"πŸ”΄ Analysis failed. Error: {str(e)}"
# Create Gradio interface
with gr.Blocks(theme=gr.themes.Soft(primary_hue="emerald")) as app:
gr.Markdown("""# πŸ”¬ AI/Human Text Forensic Analyzer""")
with gr.Row():
with gr.Column():
input_text = gr.Textbox(label="πŸ“ Text to Analyze", lines=7,
placeholder="Paste text (min 50 chars)...")
analyze_btn = gr.Button("πŸ§ͺ Get Definitive Proofs", variant="primary")
with gr.Column():
output_text = gr.Markdown(label="πŸ“œ Irrefutable Evidence Report")
examples = [
["The way the moonlight danced... that's not something algorithms capture. It's in the imperfect pauses between words where humanity lives."],
["The optimal temperature for this chemical reaction is 37.5Β°C, with a tolerance of Β±2Β°C. This falls within the expected range according to published studies."],
["Lol, idk why, but when I try to explain it just comes out all wrong? Like my brain and mouth ain't connected today smh"]
]
gr.Examples(
examples=examples,
inputs=input_text,
outputs=output_text,
fn=analyze_text,
label="πŸ’‘ Try These Challenge Cases"
)
analyze_btn.click(analyze_text, inputs=input_text, outputs=output_text)
gr.Markdown("""
<div style="text-align: center; margin-top: 20px; color: #666;">
<small>Analyzes only the most undeniable evidence β€’ Confidence scores reflect logical certainty</small>
</div>
""")
if __name__ == "__main__":
app.launch()