Commit
·
e40dd74
1
Parent(s):
7bc170e
Update app.py, dockerfile, & requirements.txt
Browse files- Dockerfile +2 -2
- app.py +100 -24
- requirements.txt +2 -1
Dockerfile
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
# Gunakan image Python dasar
|
| 2 |
FROM python:3.10
|
| 3 |
|
| 4 |
# Atur direktori kerja di dalam container
|
|
@@ -16,7 +16,7 @@ COPY requirements.txt .
|
|
| 16 |
RUN pip install --no-cache-dir -r requirements.txt
|
| 17 |
|
| 18 |
# Salin skrip aplikasi
|
| 19 |
-
COPY . .
|
| 20 |
|
| 21 |
# Perintah untuk menjalankan aplikasi Gradio saat container diluncurkan
|
| 22 |
CMD ["python", "app.py"]
|
|
|
|
| 1 |
+
# Gunakan image Python 3.10 dasar
|
| 2 |
FROM python:3.10
|
| 3 |
|
| 4 |
# Atur direktori kerja di dalam container
|
|
|
|
| 16 |
RUN pip install --no-cache-dir -r requirements.txt
|
| 17 |
|
| 18 |
# Salin skrip aplikasi
|
| 19 |
+
COPY app.py .
|
| 20 |
|
| 21 |
# Perintah untuk menjalankan aplikasi Gradio saat container diluncurkan
|
| 22 |
CMD ["python", "app.py"]
|
app.py
CHANGED
|
@@ -1,13 +1,15 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import cv2
|
| 3 |
import torch
|
| 4 |
-
import numpy as np
|
| 5 |
import os
|
|
|
|
|
|
|
| 6 |
from transformers import AutoProcessor, AutoModelForCausalLM
|
| 7 |
from PIL import Image
|
|
|
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
# Model akan
|
| 11 |
print("Memuat model GIT...")
|
| 12 |
processor = AutoProcessor.from_pretrained("microsoft/git-base-coco")
|
| 13 |
model = AutoModelForCausalLM.from_pretrained("microsoft/git-base-coco")
|
|
@@ -15,31 +17,96 @@ device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
| 15 |
model.to(device)
|
| 16 |
print("Model GIT berhasil dimuat!")
|
| 17 |
|
| 18 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
def validate_video_with_git(video_path, mission_type):
|
| 20 |
"""
|
| 21 |
-
Validasi video menggunakan model GIT.
|
| 22 |
-
Menerima:
|
| 23 |
-
- video_path: path ke file video yang diunggah.
|
| 24 |
-
- mission_type: 'kertas' atau 'daun'.
|
| 25 |
-
Mengembalikan:
|
| 26 |
-
- string hasil validasi.
|
| 27 |
"""
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
required_keywords = ['person', 'paper', 'yellow trash can']
|
| 30 |
-
elif mission_type == '
|
| 31 |
-
required_keywords = ['person', 'leaves', 'trash can']
|
| 32 |
else:
|
|
|
|
|
|
|
| 33 |
return "Gagal. Tipe misi tidak valid."
|
| 34 |
|
| 35 |
-
cap = cv2.VideoCapture(
|
| 36 |
if not cap.isOpened():
|
| 37 |
-
|
|
|
|
| 38 |
|
| 39 |
valid_frames_count = 0
|
| 40 |
-
frame_interval =
|
| 41 |
-
|
| 42 |
frame_count = 0
|
|
|
|
|
|
|
|
|
|
| 43 |
while cap.isOpened():
|
| 44 |
ret, frame_bgr = cap.read()
|
| 45 |
if not ret:
|
|
@@ -64,25 +131,34 @@ def validate_video_with_git(video_path, mission_type):
|
|
| 64 |
valid_frames_count = 0
|
| 65 |
|
| 66 |
if valid_frames_count >= 3:
|
| 67 |
-
|
| 68 |
-
|
| 69 |
|
| 70 |
frame_count += 1
|
| 71 |
|
| 72 |
cap.release()
|
| 73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
-
#
|
| 76 |
-
# PENTING: Gunakan gr.Interface untuk endpoint API yang lebih mudah
|
| 77 |
interface = gr.Interface(
|
| 78 |
fn=validate_video_with_git,
|
| 79 |
inputs=[
|
| 80 |
gr.Video(label="Video Sampah"),
|
| 81 |
-
gr.Radio(choices=["
|
| 82 |
],
|
| 83 |
outputs=gr.Textbox(label="Hasil Validasi"),
|
| 84 |
title="Validasi Misi Sampah dengan AI",
|
| 85 |
-
description="Unggah video dan pilih tipe misi
|
| 86 |
)
|
| 87 |
|
| 88 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import cv2
|
| 3 |
import torch
|
|
|
|
| 4 |
import os
|
| 5 |
+
import datetime
|
| 6 |
+
import subprocess
|
| 7 |
from transformers import AutoProcessor, AutoModelForCausalLM
|
| 8 |
from PIL import Image
|
| 9 |
+
from huggingface_hub import HfApi
|
| 10 |
|
| 11 |
+
# Muat model GIT
|
| 12 |
+
# Model akan diunduh ke direktori yang dapat ditulis berkat Dockerfile
|
| 13 |
print("Memuat model GIT...")
|
| 14 |
processor = AutoProcessor.from_pretrained("microsoft/git-base-coco")
|
| 15 |
model = AutoModelForCausalLM.from_pretrained("microsoft/git-base-coco")
|
|
|
|
| 17 |
model.to(device)
|
| 18 |
print("Model GIT berhasil dimuat!")
|
| 19 |
|
| 20 |
+
# Konfigurasi Hugging Face Hub
|
| 21 |
+
DATASET_REPO_ID = "monikahung/videos_throw_garbage"
|
| 22 |
+
HF_TOKEN = os.getenv("HF_TOKEN_VIDEOS")
|
| 23 |
+
api = HfApi(token=HF_TOKEN)
|
| 24 |
+
|
| 25 |
+
def upload_video_to_dataset(video_path, folder_name):
|
| 26 |
+
"""
|
| 27 |
+
Mengunggah file video ke Hugging Face Dataset di folder yang ditentukan.
|
| 28 |
+
"""
|
| 29 |
+
if not HF_TOKEN:
|
| 30 |
+
print("Peringatan: Token Hugging Face tidak ditemukan. Tidak dapat mengunggah ke dataset.")
|
| 31 |
+
return
|
| 32 |
+
|
| 33 |
+
try:
|
| 34 |
+
# Gunakan nama file yang unik berdasarkan timestamp
|
| 35 |
+
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 36 |
+
file_name = f"{timestamp}_{os.path.basename(video_path)}"
|
| 37 |
+
path_in_repo = f"{folder_name}/{file_name}"
|
| 38 |
+
|
| 39 |
+
# Unggah file ke folder yang benar di dataset
|
| 40 |
+
api.upload_file(
|
| 41 |
+
path_or_fileobj=video_path,
|
| 42 |
+
path_in_repo=path_in_repo,
|
| 43 |
+
repo_id=DATASET_REPO_ID,
|
| 44 |
+
repo_type="dataset",
|
| 45 |
+
)
|
| 46 |
+
print(f"File {file_name} berhasil diunggah ke folder '{folder_name}'.")
|
| 47 |
+
except Exception as e:
|
| 48 |
+
print(f"Gagal mengunggah file ke dataset: {e}")
|
| 49 |
+
|
| 50 |
+
def process_and_slow_video(video_path, slow_factor=2):
|
| 51 |
+
"""
|
| 52 |
+
Memperlambat video menggunakan FFmpeg.
|
| 53 |
+
"""
|
| 54 |
+
# Buat path untuk video yang diperlambat
|
| 55 |
+
slowed_video_path = f"{os.path.splitext(video_path)[0]}_slowed.mp4"
|
| 56 |
+
|
| 57 |
+
# Perintah FFmpeg untuk memperlambat video dan mempertahankan audio
|
| 58 |
+
# `-filter:v` mengubah kecepatan video, `-filter:a` mengubah kecepatan audio
|
| 59 |
+
# `-c:a aac` mengkodekan ulang audio ke format AAC
|
| 60 |
+
command = [
|
| 61 |
+
'ffmpeg',
|
| 62 |
+
'-i', video_path,
|
| 63 |
+
'-filter:v', f'setpts={slow_factor}*PTS',
|
| 64 |
+
'-c:a', 'aac',
|
| 65 |
+
'-y', slowed_video_path
|
| 66 |
+
]
|
| 67 |
+
|
| 68 |
+
try:
|
| 69 |
+
subprocess.run(command, check=True, capture_output=True, text=True)
|
| 70 |
+
print(f"Video berhasil diperlambat. Disimpan di: {slowed_video_path}")
|
| 71 |
+
return slowed_video_path
|
| 72 |
+
except subprocess.CalledProcessError as e:
|
| 73 |
+
print(f"FFmpeg gagal. Error: {e.stderr}")
|
| 74 |
+
return None
|
| 75 |
+
except FileNotFoundError:
|
| 76 |
+
print("FFmpeg tidak ditemukan. Pastikan sudah terinstal dan ada di PATH.")
|
| 77 |
+
return None
|
| 78 |
+
|
| 79 |
def validate_video_with_git(video_path, mission_type):
|
| 80 |
"""
|
| 81 |
+
Validasi video menggunakan model GIT dan mengunggahnya ke dataset.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
"""
|
| 83 |
+
# Memperlambat video terlebih dahulu
|
| 84 |
+
slowed_video_path = process_and_slow_video(video_path)
|
| 85 |
+
if not slowed_video_path:
|
| 86 |
+
upload_video_to_dataset(video_path, "neither")
|
| 87 |
+
return "Gagal. Gagal memproses video."
|
| 88 |
+
|
| 89 |
+
# Tentukan kata kunci berdasarkan tipe misi
|
| 90 |
+
if mission_type == 'paper':
|
| 91 |
required_keywords = ['person', 'paper', 'yellow trash can']
|
| 92 |
+
elif mission_type == 'leaf':
|
| 93 |
+
required_keywords = ['person', 'leaves', 'green trash can']
|
| 94 |
else:
|
| 95 |
+
# Jika tipe misi tidak valid, simpan ke folder "neither"
|
| 96 |
+
upload_video_to_dataset(video_path, "neither")
|
| 97 |
return "Gagal. Tipe misi tidak valid."
|
| 98 |
|
| 99 |
+
cap = cv2.VideoCapture(slowed_video_path)
|
| 100 |
if not cap.isOpened():
|
| 101 |
+
upload_video_to_dataset(video_path, "neither")
|
| 102 |
+
return "Gagal. Gagal membuka file video yang diperlambat."
|
| 103 |
|
| 104 |
valid_frames_count = 0
|
| 105 |
+
frame_interval = 1
|
|
|
|
| 106 |
frame_count = 0
|
| 107 |
+
|
| 108 |
+
validation_status = "gagal"
|
| 109 |
+
|
| 110 |
while cap.isOpened():
|
| 111 |
ret, frame_bgr = cap.read()
|
| 112 |
if not ret:
|
|
|
|
| 131 |
valid_frames_count = 0
|
| 132 |
|
| 133 |
if valid_frames_count >= 3:
|
| 134 |
+
validation_status = "valid"
|
| 135 |
+
break
|
| 136 |
|
| 137 |
frame_count += 1
|
| 138 |
|
| 139 |
cap.release()
|
| 140 |
+
|
| 141 |
+
# Hapus file video yang diperlambat setelah selesai
|
| 142 |
+
os.remove(slowed_video_path)
|
| 143 |
+
|
| 144 |
+
# Tentukan folder berdasarkan hasil validasi dan tipe misi
|
| 145 |
+
if validation_status == "valid":
|
| 146 |
+
upload_video_to_dataset(video_path, mission_type)
|
| 147 |
+
return "Video dianggap valid. Misi berhasil!"
|
| 148 |
+
else:
|
| 149 |
+
upload_video_to_dataset(video_path, "neither")
|
| 150 |
+
return "Video tidak memenuhi kriteria. Misi gagal."
|
| 151 |
|
| 152 |
+
# Buat antarmuka Gradio
|
|
|
|
| 153 |
interface = gr.Interface(
|
| 154 |
fn=validate_video_with_git,
|
| 155 |
inputs=[
|
| 156 |
gr.Video(label="Video Sampah"),
|
| 157 |
+
gr.Radio(choices=["paper", "leaf"], label="Jenis Misi")
|
| 158 |
],
|
| 159 |
outputs=gr.Textbox(label="Hasil Validasi"),
|
| 160 |
title="Validasi Misi Sampah dengan AI",
|
| 161 |
+
description="Unggah video dan pilih tipe misi untuk memvalidasi aksi membuang sampah."
|
| 162 |
)
|
| 163 |
|
| 164 |
if __name__ == "__main__":
|
requirements.txt
CHANGED
|
@@ -3,4 +3,5 @@ torch
|
|
| 3 |
transformers==4.42.0
|
| 4 |
opencv-python
|
| 5 |
Pillow
|
| 6 |
-
accelerate
|
|
|
|
|
|
| 3 |
transformers==4.42.0
|
| 4 |
opencv-python
|
| 5 |
Pillow
|
| 6 |
+
accelerate
|
| 7 |
+
huggingface_hub
|