python -m venv .venv
source .venv/bin/activate
.venv\Scripts\activate
pip install streamlit cloudinary python-dotenv
requirements.txt
:Thư viện | Phiên bản |
---|---|
cloudinary | 1.41.0 |
python-dotenv | 1.0.1 |
streamlit | 1.39.0 |
.env
để lưu trữ an toàn các thông tin nhạy cảm:CLOUDINARY_CLOUD_NAME=your_cloud_nameCLOUDINARY_API_KEY=your_api_keyCLOUDINARY_API_SECRET=your_api_secret
Vào Cloudinary Dashboard để lấy thông tin chi tiết và thay thế vào các biến trên.
.gitignore
và thêm .env
vào để tránh đẩy lên kho code công khai:.env
import streamlit as stimport cloudinaryfrom cloudinary import CloudinaryImage, CloudinaryVideofrom dotenv import load_dotenvimport os
# Load biến môi trườngload_dotenv()
# Cấu hình Cloudinarycloudinary.config( cloud_name=os.getenv('CLOUDINARY_CLOUD_NAME'), api_key=os.getenv('CLOUDINARY_API_KEY'), api_secret=os.getenv('CLOUDINARY_API_SECRET'), secure=True)
def generate_image_url(public_id: str, aspect_ratio: float, width: int) -> str: image = CloudinaryImage(public_id) transformation = [ {'aspect_ratio': aspect_ratio, 'gravity': "auto", 'width': width, 'crop': "fill"}, {'quality': "auto"}, {'fetch_format': "auto"} ] return image.build_url(transformation=transformation)
def generate_video_url(public_id: str, aspect_ratio: float, width: int) -> str: video = CloudinaryVideo(public_id) transformation = [ {'aspect_ratio': aspect_ratio, 'gravity': "auto", 'width': width, 'crop': "fill"}, {'quality': "auto"}, {'fetch_format': "auto"} ] return video.build_url(transformation=transformation)
def main(): st.title("AI-Powered Content-Aware Cropping for Images and Videos with Cloudinary") st.write("Ứng dụng trình diễn khả năng cắt ghép thông minh bằng AI từ Cloudinary.")
# Nhập public ID ảnh public_id = st.text_input("Nhập Cloudinary image public ID:", "profile_uzviqu")
# Tùy chọn tỷ lệ khung hình (aspect ratio) và chiều rộng tương ứng aspect_ratio_options = { "1:2": (0.5, 433), "5:2": (2.5, 1300), "1:1": (1.0, 867) }
st.subheader("Tỷ lệ khung hình:") selected_option = st.radio("Chọn tỷ lệ khung hình", list(aspect_ratio_options.keys()), index=1, key="image_radio")
# Lấy giá trị tương ứng theo lựa chọn aspect_ratio, width = aspect_ratio_options[selected_option]
# Hiển thị ảnh với URL đã được crop if public_id: image_url = generate_image_url(public_id, aspect_ratio, width) st.image(image_url, caption="Ảnh được cắt ghép tự động")
def video(): public_id = st.text_input("Nhập Cloudinary Video public ID:", "video-player/gushing-waterfall")
video_aspect_ratio_options = { "1:2": (0.5, 364), "5:2": (2.5, 1300), "1:1": (1.0, 728) }
st.subheader("Tỷ lệ khung hình:") selected_option = st.radio("Chọn tỷ lệ khung hình", list(video_aspect_ratio_options.keys()), index=1, key="video_radio")
aspect_ratio, width = video_aspect_ratio_options[selected_option]
if public_id: video_url = generate_video_url(public_id, aspect_ratio, width) st.video(video_url)
if __name__ == "__main__": main() video()
streamlit run app.py
public ID
của ảnh và video trên Cloudinary, sau đó tự động hiển thị nội dung đã được cắt ghép với tỷ lệ bạn chọn.Muốn sở hữu app riêng? Hãy clone repo từ GitHub và cập nhật thông tin Cloudinary trong .env
.
.env
(Cloudinary credentials) trong phần Settings của dự án trên Community Cloud.