| Thông tin | Chi tiết |
|---|---|
| Họ tên | Nguyễn Duy Hào |
| MSSV | 24120306 |
| Lớp | 24CTT5 |
| Môn học | Tư Duy Tính Toán |
- Tên mô hình: Salesforce/blip-vqa-base
- Hugging Face: https://huggingface.co/Salesforce/blip-vqa-base
Hệ thống cho phép người dùng gửi ảnh kèm câu hỏi, mô hình AI sẽ phân tích ảnh và trả lời câu hỏi bằng ngôn ngữ tự nhiên (Visual Question Answering). Có thể để trống phần câu hỏi. Mô hình hiện tại chỉ hỗ trợ tiếng Anh.
Ví dụ: Gửi ảnh một con mèo + câu hỏi "What animal is this?" → AI trả lời "cat".
Hệ thống gồm 2 thành phần:
| Thành phần | Mô tả |
|---|---|
| Server (Colab) | Chạy model AI trên Google Colab, expose API qua Cloudflare Tunnel |
| Client CLI | Ứng dụng dòng lệnh Python để gọi API |
Image_Caption_API/
├── client/ # Module client
│ ├── __init__.py
│ ├── config.py # Quản lý biến môi trường
│ ├── api.py # HTTP client gọi server AI
│ ├── validators.py # Kiểm tra đường dẫn & định dạng ảnh
│ └── cli_app.py # Giao diện dòng lệnh (CLI)
├── notebooks/
│ └── run_model_image_vqa.ipynb # Notebook chạy server trên Colab
├── cli.py # Entry point chạy CLI
├── test_model.py # Script test nhanh các endpoint
├── .env.example # Template biến môi trường
├── requirements.txt # Danh sách thư viện Python
└── README.md
git clone https://github.com/NguyenDuyHao-cheems/Image_Caption_API.git
cd Image_Caption_APIpython -m venv .venvKích hoạt:
- Windows:
.venv\Scripts\activate
- macOS/Linux:
source .venv/bin/activate
pip install -r requirements.txtcp .env.example .envMở file .env và thay URL bằng địa chỉ server đang chạy:
API_URL=https://your-server-url.trycloudflare.comLưu ý: URL này lấy từ output khi chạy notebook trên Google Colab (xem bước tiếp theo).
- Mở file
notebooks/run_model_image_vqa.ipynbtrên Google Colab - Chạy tất cả các cell
- Copy URL Cloudflare Tunnel được tạo ra (dạng
https://xxx.trycloudflare.com) - Dán URL vào file
.env
Cách 1 — CLI tương tác:
python cli.pyChương trình sẽ hỏi bạn nhập đường dẫn ảnh và câu hỏi, sau đó trả kết quả từ AI.
Cách 2 — Test nhanh:
python test_model.pyTự động test 3 endpoint: /, /health, /predict.
Server cung cấp 3 endpoint:
curl https://your-server-url.trycloudflare.com/Response:
{"message": "API có khả năng đọc và trả lời các câu hỏi liên quan đến ảnh đầu vào"}curl https://your-server-url.trycloudflare.com/healthResponse:
{"status": "Hệ thống hoạt động bình thường"}| Field | Type | Required | Description |
|---|---|---|---|
file |
File |
Yes | Hỗ trợ định dạng: JPG, PNG, WebP). |
query |
String |
Yes | Câu hỏi muốn hỏi về bức ảnh. |
| Gửi ảnh kèm câu hỏi: |
curl -X POST https://your-server-url.trycloudflare.com/predict \
-F "file=@/path/to/your/test_image.jpg" \
-F "query=What color is the shirt?"Response:
{"answer": "blue"}import requests
url = "https://your-server-url.trycloudflare.com/predict"
with open("test_image.jpg", "rb") as f:
response = requests.post(
url,
files={"file": ("test_image.jpg", f, "image/jpeg")},
data={"query": "What is in this image?"}
)
print(response.json())
# {"answer": "a cat sitting on a couch"}demo_api.mp4
- 0-17: giới thiệu mô hình
- 0:17 - 1:18 => mô tả source code
- 1:18 - 1:53 -> chạy server trên gg colab
- 1:53 - 3:40 -> chạy test_model
- 3:40 - hết -> chạy command line