Spaces:
Runtime error
Runtime error
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() |