manager-agent / app.py
SuccessfulCrab's picture
Update app.py
fd6df86 verified
Raw
History Blame Contribute Delete
6.46 kB
import os
import spaces
from huggingface_hub import login
import gradio as gr
from smolagents import HfApiModel, CodeAgent, LiteLLMModel
from PIL import Image
import firecrawler
import rag
# Get secret for Antropic API
claude = os.getenv('claude')
#Fetch tools
execute_firecrawl = firecrawler.FireCrawlTool()
retriever_tool = rag.retriever_tool
agent = CodeAgent(
tools=[execute_firecrawl, retriever_tool],
model = LiteLLMModel(model_id="anthropic/claude-3-5-sonnet-latest", api_key=claude),
max_steps=5
)
def get_answer(image, url, text):
"""
A function that takes any question as input and returns the answer using agent.run()
Args:
url (str): The URL to investigate
text (str): Additional context about the situation
image (PIL): An image to investigate
Returns:
str: Detailed analysis report
"""
# Check if image is None or not provided
if image is None:
return "Please upload an image before processing.", "Please upload an image before processing.", "Please upload an image before processing."
# Ensure image is in PIL format
if not isinstance(image, Image.Image):
try:
image = Image.open(image)
except Exception as e:
return f"Error opening image: {str(e)}", "Image processing failed.", "Please upload a valid image file."
images = []
images.append(image)
# Enhanced prompt with more specific instruction for detailed output
full_prompt = f'''
COMPREHENSIVE SCAM DETECTION ANALYSIS
OBJECTIVE
Provide a meticulously detailed, structured assessment of potential online risks.
INPUT CONTEXT
URL under investigation: {url}
User-provided situation description: {text}
Attachment Image: Attached to the VLM submission (Optional supplementary context)
ANALYSIS TOOLS
retriever_tool : RAG tool that utilizes comprehensive scam information repository
execute_firecrawl: Scrapes the contents of the url
Access to verified scam databases
Contextual information retrieval
Up-to-date assistance resources
Comparative scam pattern analysis
ANALYSIS FRAMEWORK:
RISK LEVEL
Explicitly state an overall risk assessment (Low/Medium/High)
INCLUDE image analysis insights if an image is provided
Cross-reference with RAG tool scam repository.
URL ANALYSIS
Domain reputation assessment
Technical red flags
Registrar and hosting information insights
Cross-reference with RAG tool database
Verify against known scam patterns
CONTENT EVALUATION
Content quality assessment
Linguistic and communication pattern analysis
Consistency and professionalism evaluation
Visual content analysis (if image attached)
Compare against RAG tool's communication red flags
SPECIFIC RED FLAGS
List at least 5 concrete indicators of potential scam
Utilize RAG tool to:
Validate identified red flags
Provide historical scam context
Match against known scam signatures
Categorize red flags (Technical, Financial, Communication)
Incorporate visual evidence analysis if image provided
RECOMMENDED ACTIONS
Specific, actionable steps for user protection
Leverage RAG tool for:
Verified reporting channels
Local and national assistance resources
Recommended verification methods
Personalized safety guidelines
Image-specific caution recommendations if relevant
ADDITIONAL INSIGHTS
Contextual background information
Potential motivations behind suspicious activity
Broader pattern recognition
Visual context interpretation (if image available)
RAG tool-sourced trend analysis
CRITICAL ANALYSIS GUIDELINES:
Maintain objective, evidence-based analysis
Talk in terms of risks rather than certainties
Focus on user empowerment and protection
Provide comprehensive yet clear recommendations
Utilize RAG tool as primary reference and validation source
IMAGE ANALYSIS PROTOCOL (IF APPLICABLE)
Metadata examination
Content authenticity assessment
Potential manipulation indicators
Contextual relevance to overall risk assessment
RAG tool image forensics cross-reference
ASSISTANCE RESOURCES
Compile comprehensive list of support resources from RAG tool
National fraud reporting centers
Cybercrime units
Consumer protection agencies
Mental health and support services for scam victims
'''
answer = agent.run(full_prompt, images=images)
print("Final output:")
print(answer)
return answer
# Gradio Interface (rest of the code remains the same)
with gr.Blocks(theme=gr.themes.Monochrome()) as demo:
theme=gr.themes.Monochrome()
with gr.Row():
with gr.Column(scale=1, min_width=300):
gr.Markdown(
"""
# ScamShield (agent edition)
🛡️ A tool to help users identify scam red flags.
""")
with gr.Row():
with gr.Column(scale=1, min_width=300):
input_image = gr.Image(label="Upload a suspicious screenshot (email, text message, advertisement etc)", type="pil")
input_url = gr.Textbox(label="URLs", info="Please enter a suspicious URL", lines=3, value="https://fliojinews.xyz/?_lp=1&_token=uuid_28pp9h04s5la_28pp9h04s5la67e5d957781a19.77269004&product=Mirflect%20Gain&advertiser=Mirflect%20Gain%20i230")
input_text = gr.Textbox(label="Description", info="Please describe your concerns regarding the situation", lines=3, value="Is this website a reliable source of investment information? I read about it on a news page (screenshot attached)")
btn = gr.Button("Process submission")
with gr.Column(scale=2, min_width=300):
t3 = gr.Textbox(label="Advice", lines=10)
btn.click(
fn=get_answer,
inputs=[input_image, input_url, input_text],
outputs=[t3]
)
# Launch
demo.launch()