metek7's picture
Update app.py
ef5efca verified
raw
history blame
3.7 kB
import sys
import subprocess
import pkg_resources
import os
import tempfile
required_packages = {
'torch': 'torch',
'gradio': 'gradio',
'transformers': 'transformers',
'decord': 'decord',
'numpy': 'numpy',
'instaloader': 'instaloader'
}
def install_packages(packages):
for package in packages:
subprocess.check_call([sys.executable, "-m", "pip", "install", package])
def check_and_install_packages():
installed_packages = {pkg.key for pkg in pkg_resources.working_set}
missing_packages = [required_packages[pkg] for pkg in required_packages if pkg not in installed_packages]
if missing_packages:
print("Installing missing packages...")
install_packages(missing_packages)
print("Packages installed successfully.")
else:
print("All required packages are already installed.")
# Check and install required packages
check_and_install_packages()
# Now import the required modules
import gradio as gr
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM
from decord import VideoReader, cpu
import numpy as np
import instaloader
# Initialize Instaloader
L = instaloader.Instaloader()
def download_instagram_video(url):
try:
# Extract post shortcode from URL
shortcode = url.split("/")[-2]
# Download the post
post = instaloader.Post.from_shortcode(L.context, shortcode)
# Create a temporary directory
with tempfile.TemporaryDirectory() as tmpdirname:
# Download the video
L.download_post(post, target=tmpdirname)
# Find the downloaded video file
video_file = next(file for file in os.listdir(tmpdirname) if file.endswith('.mp4'))
video_path = os.path.join(tmpdirname, video_file)
return video_path
except Exception as e:
print(f"Error downloading video: {e}")
return None
def process_video(video_path, max_frames=64):
vr = VideoReader(video_path, ctx=cpu(0))
total_frames = len(vr)
frame_indices = np.linspace(0, total_frames - 1, max_frames, dtype=int)
frames = vr.get_batch(frame_indices).asnumpy()
return frames
# This is a placeholder for the actual LLaVA-Video model
def analyze_video(video_frames, question):
# In a real implementation, you would use the LLaVA-Video model here
return f"Analyzed {len(video_frames)} frames. Your question was: {question}"
def analyze_instagram_video(video_url, question):
if not video_url:
return "Please enter an Instagram video URL."
video_path = download_instagram_video(video_url)
if not video_path:
return "Failed to download the video. Please check the URL and try again."
video_frames = process_video(video_path)
response = analyze_video(video_frames, question)
return response
# Create Gradio interface
with gr.Blocks() as demo:
gr.Markdown("# 🎥 Instagram Video Analyzer")
gr.Markdown("Enter the URL of an Instagram video and ask questions about its content!")
with gr.Row():
with gr.Column():
video_url_input = gr.Textbox(label="Instagram Video URL", placeholder="https://www.instagram.com/p/...")
question_input = gr.Textbox(label="Ask a question about the video", placeholder="What's happening in this Instagram video?")
submit_button = gr.Button("Analyze Video")
output = gr.Textbox(label="Analysis Result")
submit_button.click(
fn=analyze_instagram_video,
inputs=[video_url_input, question_input],
outputs=output
)
if __name__ == "__main__":
demo.launch()