Spaces:
Runtime error
Runtime error
Upload 7 files
Browse files- README.md +83 -13
- agent.py +41 -0
- app.py +36 -0
- data/clean_data.csv +0 -0
- data/raw_data.csv +0 -0
- pipeline.py +18 -0
- requirements.txt +3 -0
README.md
CHANGED
|
@@ -1,13 +1,83 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 🚀 Data Pipeline với AI Agent (Gradio + Python)
|
| 2 |
+
|
| 3 |
+
## 🧠 Giới thiệu
|
| 4 |
+
|
| 5 |
+
Dự án này mô phông một hệ thống **Data Pipeline** tự động:
|
| 6 |
+
|
| 7 |
+
* **Thu thập dữ liệu** từ API công khai (Public APIs)
|
| 8 |
+
* **Làm sạch dữ liệu** bằng bộ lọc logic (chỉ giữ các API hỗ trợ HTTPS)
|
| 9 |
+
* **Tự động hóa xử lý** thông qua một **AI Agent đơn giản** phát hiện dữ liệu mới
|
| 10 |
+
* **Thông báo kết quả** xử lý qua **email Gmail**
|
| 11 |
+
* **Hiển thị giao diện tương tác** qua Gradio trên Hugging Face Spaces
|
| 12 |
+
|
| 13 |
+
---
|
| 14 |
+
|
| 15 |
+
## 📦 Chức năng chính
|
| 16 |
+
|
| 17 |
+
| Chức năng | Mô tả |
|
| 18 |
+
| ------------------------------ | ----------------------------------------------------- |
|
| 19 |
+
| 🛠 **Chạy pipeline thủ công** | Bấm nút để thu thập & xử lý dữ liệu từ API |
|
| 20 |
+
| 🤖 **Agent phát hiện tự động** | Agent kiểm tra xem có dữ liệu mới → chạy pipeline |
|
| 21 |
+
| 📄 **Xem dữ liệu đã xử lý** | Hiển thị dữ liệu sạch gần nhất trong bảng |
|
| 22 |
+
| ✉️ **Gửi email thông báo** | Khi Agent xử lý thành công → gửi Gmail đến người dùng |
|
| 23 |
+
|
| 24 |
+
---
|
| 25 |
+
|
| 26 |
+
## 🥪 Demo trực tiếp (Hugging Face Space)
|
| 27 |
+
|
| 28 |
+
👉 Truy cập giao diện tại:
|
| 29 |
+
`https://huggingface.co/spaces/<username>/data-pipeline-ai-agent`
|
| 30 |
+
|
| 31 |
+
---
|
| 32 |
+
|
| 33 |
+
## 🧠 Công nghệ sử dụng
|
| 34 |
+
|
| 35 |
+
* [x] **Python 3.10**
|
| 36 |
+
* [x] [Gradio](https://www.gradio.app/)
|
| 37 |
+
* [x] [Pandas](https://pandas.pydata.org/)
|
| 38 |
+
* [x] `smtplib` & `email.mime` – Gửi Gmail
|
| 39 |
+
* [x] API nguồn: [https://api.publicapis.org/entries](https://api.publicapis.org/entries)
|
| 40 |
+
|
| 41 |
+
---
|
| 42 |
+
|
| 43 |
+
## 🗂 Cấu trúc thư mục
|
| 44 |
+
|
| 45 |
+
```
|
| 46 |
+
data-pipeline-ai-agent/
|
| 47 |
+
├── app.py # Giao diện Gradio chính
|
| 48 |
+
├── pipeline.py # Thu thập & làm sạch dữ liệu
|
| 49 |
+
├── agent.py # Agent tự động trigger & gửi email
|
| 50 |
+
├── requirements.txt # Thư viện cần cài đặt
|
| 51 |
+
├── README.md # Mô tả chi tiết dự án
|
| 52 |
+
└── data/ # Lưu file CSV tạm thời
|
| 53 |
+
├── raw_data.csv
|
| 54 |
+
└── clean_data.csv
|
| 55 |
+
```
|
| 56 |
+
|
| 57 |
+
---
|
| 58 |
+
|
| 59 |
+
## 🔐 Cấu hình Gmail để gửi email
|
| 60 |
+
|
| 61 |
+
1. Bật 2 bước xác minh tại: [https://myaccount.google.com/security](https://myaccount.google.com/security)
|
| 62 |
+
2. Tạo App Password tại: [https://myaccount.google.com/apppasswords](https://myaccount.google.com/apppasswords)
|
| 63 |
+
3. Cập nhật email và mật khẩu app vào `agent.py`:
|
| 64 |
+
|
| 65 |
+
```python
|
| 66 |
+
SENDER_EMAIL = "youremail@gmail.com"
|
| 67 |
+
SENDER_PASSWORD = "your-16-char-app-password"
|
| 68 |
+
```
|
| 69 |
+
|
| 70 |
+
---
|
| 71 |
+
|
| 72 |
+
##Hướng dẫn sử dụng (cục bộ)
|
| 73 |
+
|
| 74 |
+
```bash
|
| 75 |
+
# Cài thư viện
|
| 76 |
+
pip install -r requirements.txt
|
| 77 |
+
|
| 78 |
+
# Chạy Gradio
|
| 79 |
+
python app.py
|
| 80 |
+
```
|
| 81 |
+
|
| 82 |
+
---
|
| 83 |
+
**Dự án thực hiện trong khóa học**: MLOps – Buổi 4: "Xây dựng Data Pipeline và Feature Store với AI Agent"
|
agent.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from datetime import datetime
|
| 2 |
+
from pipeline import fetch_data, clean_data
|
| 3 |
+
import smtplib
|
| 4 |
+
from email.mime.text import MIMEText
|
| 5 |
+
|
| 6 |
+
# Cấu hình email
|
| 7 |
+
SENDER_EMAIL = "dulichviet2406@gmail.com"
|
| 8 |
+
SENDER_PASSWORD = "ppedsoakefjyrbym"
|
| 9 |
+
RECEIVER_EMAIL = "vanhai123@gmail.com"
|
| 10 |
+
|
| 11 |
+
def send_email_report(content):
|
| 12 |
+
msg = MIMEText(content)
|
| 13 |
+
msg["Subject"] = "Kết quả từ AI Agent Pipeline"
|
| 14 |
+
msg["From"] = SENDER_EMAIL
|
| 15 |
+
msg["To"] = RECEIVER_EMAIL
|
| 16 |
+
|
| 17 |
+
try:
|
| 18 |
+
with smtplib.SMTP_SSL("smtp.gmail.com", 465) as server:
|
| 19 |
+
server.login(SENDER_EMAIL, SENDER_PASSWORD)
|
| 20 |
+
server.send_message(msg)
|
| 21 |
+
print("Email đã được gửi thành công.")
|
| 22 |
+
except Exception as e:
|
| 23 |
+
print("Gửi email thất bại:", e)
|
| 24 |
+
|
| 25 |
+
def has_new_data():
|
| 26 |
+
return datetime.now().second % 2 == 0
|
| 27 |
+
|
| 28 |
+
def run_pipeline():
|
| 29 |
+
fetch_data()
|
| 30 |
+
df_clean = clean_data()
|
| 31 |
+
return df_clean
|
| 32 |
+
|
| 33 |
+
def run_agent_once():
|
| 34 |
+
if has_new_data():
|
| 35 |
+
print("Có dữ liệu mới! Đang chạy pipeline...")
|
| 36 |
+
df = run_pipeline()
|
| 37 |
+
message = f"Agent đã xử lý {len(df)} dòng dữ liệu mới."
|
| 38 |
+
send_email_report(message)
|
| 39 |
+
return message
|
| 40 |
+
else:
|
| 41 |
+
return "Chưa phát hiện dữ liệu mới."
|
app.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from pipeline import fetch_data, clean_data
|
| 3 |
+
from agent import run_agent_once
|
| 4 |
+
import pandas as pd
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
def run_manual_pipeline():
|
| 8 |
+
fetch_data()
|
| 9 |
+
df = clean_data()
|
| 10 |
+
return f"Đã xử lý {len(df)} dòng dữ liệu", df.head()
|
| 11 |
+
|
| 12 |
+
def auto_agent_run():
|
| 13 |
+
return run_agent_once()
|
| 14 |
+
|
| 15 |
+
def view_data():
|
| 16 |
+
if os.path.exists("data/clean_data.csv"):
|
| 17 |
+
df = pd.read_csv("data/clean_data.csv")
|
| 18 |
+
return df.head()
|
| 19 |
+
return "Chưa có dữ liệu nào"
|
| 20 |
+
|
| 21 |
+
with gr.Blocks() as demo:
|
| 22 |
+
gr.Markdown("##Data Pipeline với AI Agent")
|
| 23 |
+
|
| 24 |
+
with gr.Row():
|
| 25 |
+
run_btn = gr.Button("Chạy Pipeline Thủ Công")
|
| 26 |
+
agent_btn = gr.Button("Agent Tự Động Phát Hiện Dữ Liệu")
|
| 27 |
+
view_btn = gr.Button("Xem Dữ Liệu Gần Nhất")
|
| 28 |
+
|
| 29 |
+
output = gr.Textbox(label="Thông báo")
|
| 30 |
+
data_table = gr.Dataframe(label="Dữ liệu đã xử lý")
|
| 31 |
+
|
| 32 |
+
run_btn.click(run_manual_pipeline, outputs=[output, data_table])
|
| 33 |
+
agent_btn.click(auto_agent_run, outputs=output)
|
| 34 |
+
view_btn.click(view_data, outputs=data_table)
|
| 35 |
+
|
| 36 |
+
demo.launch()
|
data/clean_data.csv
ADDED
|
File without changes
|
data/raw_data.csv
ADDED
|
File without changes
|
pipeline.py
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
import requests
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
def fetch_data():
|
| 6 |
+
print("Đang lấy dữ liệu từ API...")
|
| 7 |
+
url = "https://api.publicapis.org/entries"
|
| 8 |
+
df = pd.read_json(url)
|
| 9 |
+
os.makedirs("data", exist_ok=True)
|
| 10 |
+
df.to_csv("data/raw_data.csv", index=False)
|
| 11 |
+
return df
|
| 12 |
+
|
| 13 |
+
def clean_data():
|
| 14 |
+
print("Đang làm sạch dữ liệu...")
|
| 15 |
+
df = pd.read_csv("data/raw_data.csv")
|
| 16 |
+
df_clean = df[df["HTTPS"] == True]
|
| 17 |
+
df_clean.to_csv("data/clean_data.csv", index=False)
|
| 18 |
+
return df_clean
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
pandas
|
| 3 |
+
requests
|