subhamb04 commited on
Commit
2bc91c3
·
verified ·
1 Parent(s): cb2a193

Upload folder using huggingface_hub

Browse files
Files changed (6) hide show
  1. .gitignore +122 -0
  2. README.md +2 -8
  3. agent.py +58 -0
  4. gemini_client.py +17 -0
  5. requirements.txt +6 -0
  6. server.py +31 -0
.gitignore ADDED
@@ -0,0 +1,122 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # ------------------------------
2
+ # Python
3
+ # ------------------------------
4
+ __pycache__/
5
+ *.py[cod]
6
+ *$py.class
7
+
8
+ # C extensions
9
+ *.so
10
+ *.pyd
11
+ *.dll
12
+
13
+ # ------------------------------
14
+ # Environments
15
+ # ------------------------------
16
+ .venv/
17
+ venv/
18
+ env/
19
+ ENV/
20
+ .venv*/
21
+ venv*/
22
+ env*/
23
+ ENV*/
24
+ .python-version
25
+
26
+ # ------------------------------
27
+ # Distribution / packaging
28
+ # ------------------------------
29
+ .Python
30
+ build/
31
+ dist/
32
+ downloads/
33
+ eggs/
34
+ .eggs/
35
+ sdist/
36
+ wheels/
37
+ share/python-wheels/
38
+ *.egg-info/
39
+ .installed.cfg
40
+ *.egg
41
+ MANIFEST
42
+ pip-wheel-metadata/
43
+ pip-log.txt
44
+ pip-delete-this-directory.txt
45
+
46
+ # ------------------------------
47
+ # Unit test / coverage reports
48
+ # ------------------------------
49
+ htmlcov/
50
+ .tox/
51
+ .nox/
52
+ .coverage
53
+ .coverage.*
54
+ .cache
55
+ nosetests.xml
56
+ coverage.xml
57
+ *.cover
58
+ *.py,cover
59
+ .pytest_cache/
60
+ junit*.xml
61
+
62
+ # ------------------------------
63
+ # Type checkers / linters
64
+ # ------------------------------
65
+ .mypy_cache/
66
+ .dmypy.json
67
+ dmypy.json
68
+ .pyre/
69
+ .pytype/
70
+ .ruff_cache/
71
+
72
+ # ------------------------------
73
+ # PyInstaller
74
+ # ------------------------------
75
+ *.manifest
76
+ *.spec
77
+
78
+ # ------------------------------
79
+ # Jupyter
80
+ # ------------------------------
81
+ .ipynb_checkpoints/
82
+
83
+ # ------------------------------
84
+ # Logs and runtime files
85
+ # ------------------------------
86
+ logs/
87
+ *.log
88
+ *.pid
89
+ *.pid.lock
90
+
91
+ # ------------------------------
92
+ # Local environment variables & secrets
93
+ # ------------------------------
94
+ .env
95
+ .env.*
96
+ !.env.example
97
+
98
+ # ------------------------------
99
+ # Editors / IDEs / Tooling
100
+ # ------------------------------
101
+ .idea/
102
+ *.iml
103
+ .vscode/
104
+ .history/
105
+ .cursor/
106
+ *.code-workspace
107
+
108
+ # ------------------------------
109
+ # OS-specific
110
+ # ------------------------------
111
+ .DS_Store
112
+ Thumbs.db
113
+ ehthumbs.db
114
+ Desktop.ini
115
+
116
+ # ------------------------------
117
+ # Optional local data & temp
118
+ # ------------------------------
119
+ tmp/
120
+ temp/
121
+ data/
122
+
README.md CHANGED
@@ -1,12 +1,6 @@
1
  ---
2
- title: Agent Mcp
3
- emoji: 🏆
4
- colorFrom: red
5
- colorTo: green
6
  sdk: gradio
7
  sdk_version: 5.47.1
8
- app_file: app.py
9
- pinned: false
10
  ---
11
-
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
+ title: agent-mcp
3
+ app_file: agent.py
 
 
4
  sdk: gradio
5
  sdk_version: 5.47.1
 
 
6
  ---
 
 
agent.py ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import asyncio
2
+ import gradio as gr
3
+ from agents.mcp import MCPServerStdio
4
+ from agents import Agent, Runner, function_tool
5
+ from gemini_client import client # <-- replace with your OpenAI/Gemini client
6
+ import sys, os
7
+
8
+ # --- MCP Servers ---
9
+ mcp_fetch = MCPServerStdio(params={
10
+ "command": "uvx",
11
+ "args": ["mcp-server-fetch"],
12
+ "client_session_timeout_seconds": 30
13
+ })
14
+
15
+ mcp_pii = MCPServerStdio(params={
16
+ "command": "python",
17
+ "args": ["server.py"], # your custom PII server
18
+ "client_session_timeout_seconds": 30
19
+ })
20
+
21
+
22
+ # --- Main logic ---
23
+ async def run_agent(url: str):
24
+ async with mcp_fetch, mcp_pii:
25
+ agent = Agent(
26
+ name="PrivacyAssistant",
27
+ model=client,
28
+ instructions="You are a privacy assistant. Fetch data from URL, scan for PII, and return a safe version.",
29
+ mcp_servers=[mcp_fetch, mcp_pii],
30
+ )
31
+
32
+ query = f"""
33
+ Fetch {url}, scan the content for PII, and give me a safe redacted version.
34
+ """
35
+ result = await Runner.run(agent, query)
36
+ return result.final_output
37
+
38
+
39
+ def process_url(url: str):
40
+ return asyncio.run(run_agent(url))
41
+
42
+
43
+ # --- Gradio UI ---
44
+ with gr.Blocks() as demo:
45
+ gr.Markdown("## 🔐 PII Privacy Checker with MCP + Agentic AI")
46
+
47
+ url_input = gr.Textbox(label="Enter URL to fetch data", placeholder="https://example.com/data.txt")
48
+ output = gr.Textbox(
49
+ label="Safe Redacted Output",
50
+ lines=15, # bigger height
51
+ max_lines=30, # allow scroll if too long
52
+ placeholder="Sanitized text will appear here..."
53
+ )
54
+
55
+ submit = gr.Button("Check for PII")
56
+ submit.click(fn=process_url, inputs=url_input, outputs=output)
57
+
58
+ demo.launch()
gemini_client.py ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from dotenv import load_dotenv
3
+ from agents import OpenAIChatCompletionsModel, AsyncOpenAI
4
+
5
+ load_dotenv(override=True)
6
+ google_api_key = os.getenv("GOOGLE_API_KEY")
7
+ base_url="https://generativelanguage.googleapis.com/v1beta/openai/"
8
+
9
+ asyncllm = AsyncOpenAI(
10
+ base_url=base_url,
11
+ api_key=google_api_key
12
+ )
13
+
14
+ client = OpenAIChatCompletionsModel(
15
+ openai_client=asyncllm,
16
+ model="gemini-2.5-flash"
17
+ )
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ requests
2
+ fastmcp
3
+ openai
4
+ openai-agents
5
+ gradio
6
+ python-dotenv
server.py ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import re
2
+ from fastmcp import FastMCP
3
+
4
+ mcp = FastMCP("PII Privacy Server")
5
+
6
+ patterns = {
7
+ "email": r"[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}",
8
+ "phone": r"\b\d{10}\b",
9
+ "credit_card": r"\b(?:\d[ -]*?){13,16}\b",
10
+ "ssn": r"\b\d{3}-\d{2}-\d{4}\b"
11
+ }
12
+
13
+ @mcp.tool
14
+ def scan_document(text: str) -> dict:
15
+ """Scan text and return sensitive info findings."""
16
+ findings = []
17
+ for label, pattern in patterns.items():
18
+ for match in re.finditer(pattern, text):
19
+ findings.append({"type": label, "value": match.group()})
20
+ return {"findings": findings}
21
+
22
+ @mcp.tool
23
+ def redact_document(text: str) -> str:
24
+ """Redact sensitive info from text."""
25
+ redacted = text
26
+ for label, pattern in patterns.items():
27
+ redacted = re.sub(pattern, f"[REDACTED-{label.upper()}]", redacted)
28
+ return redacted
29
+
30
+ if __name__ == "__main__":
31
+ mcp.run()