File size: 3,673 Bytes
3b421e3
 
 
ef5efca
 
940df70
3b421e3
 
 
 
 
 
ef5efca
 
3b421e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7efde76
f0272e1
3b421e3
f0272e1
 
ef5efca
 
 
 
 
 
 
940df70
 
 
 
 
 
 
 
 
 
 
 
 
 
fa02584
940df70
fa02584
ef5efca
 
 
7efde76
3b421e3
f0272e1
3b421e3
 
 
 
7efde76
ef5efca
 
 
3b421e3
7efde76
ef5efca
 
 
 
 
 
 
3b421e3
ef5efca
 
f0272e1
7efde76
3b421e3
f0272e1
ef5efca
 
f0272e1
 
 
ef5efca
 
 
f0272e1
 
 
ef5efca
 
f0272e1
 
7efde76
 
3b421e3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import sys
import subprocess
import pkg_resources
import os
import tempfile
import requests

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:
        # Use a third-party API service like savefrom.net
        api_url = f"https://api.savefrom.net/1/info?url={url}"
        response = requests.get(api_url)
        data = response.json()

        if 'url' in data:
            video_url = data['url']
            video_data = requests.get(video_url)

            # Save the video file locally
            with open('downloaded_reel.mp4', 'wb') as f:
                f.write(video_data.content)

            return 'downloaded_reel.mp4'
        else:
            print(f"Error: {data.get('error', 'Unknown error')}")
            return None
    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()