Spaces:
Running
Running
Diego Carpintero
commited on
Commit
•
edfeb78
1
Parent(s):
552de80
implement Minerva class
Browse files- minerva.py +129 -0
minerva.py
ADDED
@@ -0,0 +1,129 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from typing import List, AsyncIterator
|
2 |
+
import os
|
3 |
+
|
4 |
+
from autogen_agentchat.agents import AssistantAgent
|
5 |
+
from autogen_agentchat.messages import MultiModalMessage
|
6 |
+
from autogen_agentchat.teams import RoundRobinGroupChat
|
7 |
+
from autogen_core import Image as AGImage
|
8 |
+
from autogen_core.tools import FunctionTool
|
9 |
+
from autogen_ext.models.openai import OpenAIChatCompletionClient
|
10 |
+
from dotenv import load_dotenv, find_dotenv
|
11 |
+
from PIL import Image
|
12 |
+
import yaml
|
13 |
+
|
14 |
+
from tools import Tools
|
15 |
+
|
16 |
+
class Minerva:
|
17 |
+
"""
|
18 |
+
AI Guardian for Scam Protection using multi-agent system for analyzing images
|
19 |
+
to identify scam attempts and provide personalized scam prevention.
|
20 |
+
"""
|
21 |
+
|
22 |
+
def __init__(self, config_path: str = "config/agents.yaml"):
|
23 |
+
"""
|
24 |
+
Initialize Minerva with configuration and setup agents.
|
25 |
+
"""
|
26 |
+
self.load_environment()
|
27 |
+
self.model = self.initialize_model()
|
28 |
+
self.config = self.load_config(config_path)
|
29 |
+
self.tools = Tools()
|
30 |
+
self.agents = self.create_agents()
|
31 |
+
self.team = self.create_team()
|
32 |
+
|
33 |
+
def load_environment(self):
|
34 |
+
"""Load environment variables"""
|
35 |
+
load_dotenv(find_dotenv())
|
36 |
+
|
37 |
+
def load_config(self, config_path: str) -> dict:
|
38 |
+
"""Load agent configurations from YAML file"""
|
39 |
+
with open(config_path, 'r') as file:
|
40 |
+
return yaml.safe_load(file)
|
41 |
+
|
42 |
+
def initialize_model(self) -> OpenAIChatCompletionClient:
|
43 |
+
"""Initialize OpenAI model"""
|
44 |
+
return OpenAIChatCompletionClient(
|
45 |
+
model="gpt-4o",
|
46 |
+
api_key=os.getenv("OPENAI_API_KEY")
|
47 |
+
)
|
48 |
+
|
49 |
+
def create_agents(self) -> List[AssistantAgent]:
|
50 |
+
"""Create all required agents with their specialized roles"""
|
51 |
+
|
52 |
+
ocr_tool = FunctionTool(
|
53 |
+
self.tools.ocr,
|
54 |
+
description="Extracts text from an image path"
|
55 |
+
)
|
56 |
+
url_checker_tool = FunctionTool(
|
57 |
+
self.tools.is_url_safe,
|
58 |
+
description="Checks if a URL is safe"
|
59 |
+
)
|
60 |
+
|
61 |
+
agents = []
|
62 |
+
|
63 |
+
agents.append(AssistantAgent(
|
64 |
+
name="OCR_Specialist",
|
65 |
+
description="Extracts text from an image",
|
66 |
+
system_message=self.config['ocr_agent']['assignment'],
|
67 |
+
model_client=self.model,
|
68 |
+
#tools=[ocr_tool] # Default OCR to GPT-4o vision capabilities. Uncomment to OCR with tool calling (requires pytesseract)
|
69 |
+
))
|
70 |
+
|
71 |
+
agents.append(AssistantAgent(
|
72 |
+
name="URL_Checker",
|
73 |
+
description="Checks if a URL is safe",
|
74 |
+
system_message=self.config['url_checker_agent']['assignment'],
|
75 |
+
model_client=self.model,
|
76 |
+
tools=[url_checker_tool]
|
77 |
+
))
|
78 |
+
|
79 |
+
agents.append(AssistantAgent(
|
80 |
+
name="Content_Analyst",
|
81 |
+
description="Analyzes the text for scam patterns",
|
82 |
+
system_message=self.config['content_agent']['assignment'],
|
83 |
+
model_client=self.model,
|
84 |
+
tools=[url_checker_tool]
|
85 |
+
))
|
86 |
+
|
87 |
+
agents.append(AssistantAgent(
|
88 |
+
name="Decision_Maker",
|
89 |
+
description="Synthesizes the analyses and make final determination",
|
90 |
+
system_message=self.config['decision_agent']['assignment'],
|
91 |
+
model_client=self.model
|
92 |
+
))
|
93 |
+
|
94 |
+
agents.append(AssistantAgent(
|
95 |
+
name="Summary_Agent",
|
96 |
+
description="Generate a summary of the final determination",
|
97 |
+
system_message=self.config['summary_agent']['assignment'],
|
98 |
+
model_client=self.model
|
99 |
+
))
|
100 |
+
|
101 |
+
agents.append(AssistantAgent(
|
102 |
+
name="Language_Translation_Agent",
|
103 |
+
description="Translate the summary to the user language",
|
104 |
+
system_message=self.config['language_translation_agent']['assignment'],
|
105 |
+
model_client=self.model
|
106 |
+
))
|
107 |
+
|
108 |
+
return agents
|
109 |
+
|
110 |
+
def create_team(self) -> RoundRobinGroupChat:
|
111 |
+
"""Create a team of agents that work together in Round Robin fashion"""
|
112 |
+
return RoundRobinGroupChat(
|
113 |
+
self.agents,
|
114 |
+
max_turns=6
|
115 |
+
)
|
116 |
+
|
117 |
+
def reset(self):
|
118 |
+
"""Reset team state"""
|
119 |
+
self.team.reset()
|
120 |
+
|
121 |
+
async def analyze_image(self, image_path: str) -> AsyncIterator:
|
122 |
+
"""
|
123 |
+
Analyze an image for potential scams.
|
124 |
+
"""
|
125 |
+
pil_image = Image.open(image_path)
|
126 |
+
img = AGImage(pil_image)
|
127 |
+
mm_message = MultiModalMessage(content=[img], source="User")
|
128 |
+
|
129 |
+
return self.team.run_stream(task=mm_message)
|