ScamVerifier / app.py
AayanSuleri's picture
Update app.py
4290ae1 verified
Raw
History Blame Contribute Delete
7.96 kB
import gradio as gr
import os
import time
from crewai import Agent, Task, Crew
from langchain_openai import ChatOpenAI
# Secure API key entry
os.getenv("OPENAI_API_KEY") # Enter your OpenAI API key
def analyze_scam(message_content):
if not message_content.strip():
return "❗ Please enter a message or URL to analyze."
loading_messages = [
"πŸ›‘οΈ Initializing AI security team...",
"πŸ” Claim Extractor analyzing suspicious content...",
"βš–οΈ Fact Checker verifying claims and patterns...",
"πŸ“‹ Safety Advisor preparing guidance report...",
"🚨 Generating comprehensive scam analysis..."
]
yield loading_messages[0]
try:
llm = ChatOpenAI(model_name="gpt-4o-mini", temperature=0.1)
for i, message in enumerate(loading_messages[1:], 1):
time.sleep(2)
yield message
extractor_agent = Agent(
role="Claim Extractor",
goal="Extract key claims, offers, and suspicious elements from messages or URLs",
backstory="Expert at parsing and identifying key claims in potentially fraudulent messages.",
llm=llm,
verbose=False
)
verifier_agent = Agent(
role="Fact Checker",
goal="Verify claims and assess scam probability using heuristic rules and pattern matching",
backstory="Cybersecurity expert who specializes in identifying scam patterns.",
llm=llm,
verbose=False
)
explainer_agent = Agent(
role="Safety Advisor",
goal="Provide clear, actionable guidance to users about potential scams",
backstory="Digital safety educator who explains complex security issues simply.",
llm=llm,
verbose=False
)
extract_task = Task(
description=f"Analyze and extract suspicious elements from: {message_content}",
expected_output="Structured summary of extracted claims, suspicious elements, and language patterns",
agent=extractor_agent
)
verify_task = Task(
description="Risk assessment with scam probability score, red flags, and evidence",
expected_output="Risk assessment with scam probability score, red flags, and evidence",
agent=verifier_agent,
context=[extract_task]
)
explain_task = Task(
description="User-friendly safety report with clear recommendations and next steps",
expected_output="User-friendly safety report with clear recommendations and next steps",
agent=explainer_agent,
context=[extract_task, verify_task]
)
crew = Crew(
agents=[extractor_agent, verifier_agent, explainer_agent],
tasks=[extract_task, verify_task, explain_task],
verbose=False,
process="sequential"
)
yield "🎯 AI agents collaborating on final analysis..."
result = crew.kickoff()
yield str(result)
except Exception as e:
yield f"❌ Scam analysis failed: {str(e)}"
def create_gradio_interface():
with gr.Blocks(title="Scam-Signal Verifier | AI Security Analysis", theme=gr.themes.Default()) as interface:
# Custom CSS for better contrast
gr.HTML("""
<style>
.team-section { background: #f8f9fa; padding: 2rem; border-radius: 15px; margin-bottom: 2rem; }
.team-title { text-align: center; margin-bottom: 1.5rem; font-size: 1.4rem; font-weight: bold; color: #1f2937; }
.team-role { font-weight: bold; color: #111827; }
.team-desc { font-size: 0.9rem; color: #374151; margin-top: 0.5rem; }
</style>
""")
# Header
gr.HTML("""
<div style="text-align: center; padding: 2rem 0;">
<h1 style="font-size: 3rem; margin-bottom: 1rem; color: #111827;">πŸ›‘οΈ Scam-Signal Verifier</h1>
<p style="font-size: 1.2rem; color: #4b5563;">
AI-powered protection against phishing and fraudulent messages
</p>
</div>
""")
# AI Security Team
gr.HTML("""
<div class="team-section">
<div class="team-title">πŸ€– Your AI Security Team</div>
<div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(250px, 1fr)); gap: 1rem;">
<div style="text-align: center; padding: 1rem;">
<div style="font-size: 2rem;">πŸ”</div>
<div class="team-role">Claim Extractor</div>
<div class="team-desc">Identifies suspicious content and claims</div>
</div>
<div style="text-align: center; padding: 1rem;">
<div style="font-size: 2rem;">βš–οΈ</div>
<div class="team-role">Fact Checker</div>
<div class="team-desc">Verifies claims and calculates risk</div>
</div>
<div style="text-align: center; padding: 1rem;">
<div style="font-size: 2rem;">πŸ“‹</div>
<div class="team-role">Safety Advisor</div>
<div class="team-desc">Provides clear guidance and next steps</div>
</div>
</div>
</div>
""")
# Input Section
gr.HTML("<h3 style='text-align: center; margin-bottom: 1rem;'>πŸ“± Analyze Suspicious Message or URL</h3>")
message_input = gr.Textbox(
label="🚨 Suspicious Message or URL",
placeholder="Paste the suspicious message, email content, or URL you want to verify...",
lines=5
)
analyze_btn = gr.Button("πŸ” Analyze for Scams", variant="primary", size="lg")
# Output Section β€” restored to original
output = gr.Textbox(
label="πŸ“Š Scam Analysis Report",
lines=25,
show_copy_button=True,
placeholder="Your detailed scam analysis will appear here..."
)
# Examples
gr.Examples(
examples=[
["URGENT: Your account will be suspended! Click here immediately to verify: http://suspicious-bank-verify.tk/login"],
["Congratulations! You've won $10,000! Claim your prize now by clicking this link and entering your bank details."],
["Your package is held at customs. Pay $50 shipping fee to release: https://bit.ly/customs-fee-pay"],
["FINAL NOTICE: IRS requires immediate payment of $2,500 or face legal action. Call now: 555-SCAM"],
["Hello dear, I am Prince Williams from Nigeria with $10 million to share with you..."],
],
inputs=message_input,
label="πŸ§ͺ Try These Example Scams"
)
# Footer
gr.HTML("""
<div style="text-align: center; padding: 2rem; margin-top: 2rem; border-top: 1px solid #eee;">
<p><strong>Scam-Signal Verifier</strong> β€’ AI-Powered Fraud Detection</p>
<p style="font-size: 0.9rem; color: #666;">
⚠️ This tool helps identify potential scams but is not 100% accurate. Always verify independently.
</p>
</div>
""")
# Event Handlers
analyze_btn.click(fn=analyze_scam, inputs=[message_input], outputs=output, show_progress=True)
message_input.submit(fn=analyze_scam, inputs=[message_input], outputs=output, show_progress=True)
return interface
if __name__ == "__main__":
app = create_gradio_interface()
app.launch(share=False, server_name="0.0.0.0", server_port=7860, show_api=False, inbrowser=False)