metek7 commited on
Commit
ef5efca
·
verified ·
1 Parent(s): 3b421e3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -17
app.py CHANGED
@@ -1,13 +1,16 @@
1
  import sys
2
  import subprocess
3
  import pkg_resources
 
 
4
 
5
  required_packages = {
6
  'torch': 'torch',
7
  'gradio': 'gradio',
8
  'transformers': 'transformers',
9
  'decord': 'decord',
10
- 'numpy': 'numpy'
 
11
  }
12
 
13
  def install_packages(packages):
@@ -34,8 +37,33 @@ import torch
34
  from transformers import AutoTokenizer, AutoModelForCausalLM
35
  from decord import VideoReader, cpu
36
  import numpy as np
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
 
38
- # Define a simple video processing function (placeholder for LLaVA-Video)
39
  def process_video(video_path, max_frames=64):
40
  vr = VideoReader(video_path, ctx=cpu(0))
41
  total_frames = len(vr)
@@ -43,34 +71,38 @@ def process_video(video_path, max_frames=64):
43
  frames = vr.get_batch(frame_indices).asnumpy()
44
  return frames
45
 
46
- # Define a simple text generation function (placeholder for actual model)
47
- def generate_response(video_frames, question):
48
- # This is a placeholder. In reality, you'd use the LLaVA-Video model here.
49
  return f"Analyzed {len(video_frames)} frames. Your question was: {question}"
50
 
51
- def analyze_instagram_short(video_file, question):
52
- if video_file is None:
53
- return "Please upload an Instagram short video."
 
 
 
 
54
 
55
- video_frames = process_video(video_file)
56
- response = generate_response(video_frames, question)
57
  return response
58
 
59
  # Create Gradio interface
60
  with gr.Blocks() as demo:
61
- gr.Markdown("# 🎥 Instagram Short Video Analyzer")
62
- gr.Markdown("Upload your Instagram short video and ask questions about its content!")
63
 
64
  with gr.Row():
65
  with gr.Column():
66
- video_input = gr.Video(label="Upload Instagram Short Video")
67
- question_input = gr.Textbox(label="Ask a question about the video", placeholder="What's happening in this Instagram short?")
68
- submit_button = gr.Button("Analyze Short Video")
69
  output = gr.Textbox(label="Analysis Result")
70
 
71
  submit_button.click(
72
- fn=analyze_instagram_short,
73
- inputs=[video_input, question_input],
74
  outputs=output
75
  )
76
 
 
1
  import sys
2
  import subprocess
3
  import pkg_resources
4
+ import os
5
+ import tempfile
6
 
7
  required_packages = {
8
  'torch': 'torch',
9
  'gradio': 'gradio',
10
  'transformers': 'transformers',
11
  'decord': 'decord',
12
+ 'numpy': 'numpy',
13
+ 'instaloader': 'instaloader'
14
  }
15
 
16
  def install_packages(packages):
 
37
  from transformers import AutoTokenizer, AutoModelForCausalLM
38
  from decord import VideoReader, cpu
39
  import numpy as np
40
+ import instaloader
41
+
42
+ # Initialize Instaloader
43
+ L = instaloader.Instaloader()
44
+
45
+ def download_instagram_video(url):
46
+ try:
47
+ # Extract post shortcode from URL
48
+ shortcode = url.split("/")[-2]
49
+
50
+ # Download the post
51
+ post = instaloader.Post.from_shortcode(L.context, shortcode)
52
+
53
+ # Create a temporary directory
54
+ with tempfile.TemporaryDirectory() as tmpdirname:
55
+ # Download the video
56
+ L.download_post(post, target=tmpdirname)
57
+
58
+ # Find the downloaded video file
59
+ video_file = next(file for file in os.listdir(tmpdirname) if file.endswith('.mp4'))
60
+ video_path = os.path.join(tmpdirname, video_file)
61
+
62
+ return video_path
63
+ except Exception as e:
64
+ print(f"Error downloading video: {e}")
65
+ return None
66
 
 
67
  def process_video(video_path, max_frames=64):
68
  vr = VideoReader(video_path, ctx=cpu(0))
69
  total_frames = len(vr)
 
71
  frames = vr.get_batch(frame_indices).asnumpy()
72
  return frames
73
 
74
+ # This is a placeholder for the actual LLaVA-Video model
75
+ def analyze_video(video_frames, question):
76
+ # In a real implementation, you would use the LLaVA-Video model here
77
  return f"Analyzed {len(video_frames)} frames. Your question was: {question}"
78
 
79
+ def analyze_instagram_video(video_url, question):
80
+ if not video_url:
81
+ return "Please enter an Instagram video URL."
82
+
83
+ video_path = download_instagram_video(video_url)
84
+ if not video_path:
85
+ return "Failed to download the video. Please check the URL and try again."
86
 
87
+ video_frames = process_video(video_path)
88
+ response = analyze_video(video_frames, question)
89
  return response
90
 
91
  # Create Gradio interface
92
  with gr.Blocks() as demo:
93
+ gr.Markdown("# 🎥 Instagram Video Analyzer")
94
+ gr.Markdown("Enter the URL of an Instagram video and ask questions about its content!")
95
 
96
  with gr.Row():
97
  with gr.Column():
98
+ video_url_input = gr.Textbox(label="Instagram Video URL", placeholder="https://www.instagram.com/p/...")
99
+ question_input = gr.Textbox(label="Ask a question about the video", placeholder="What's happening in this Instagram video?")
100
+ submit_button = gr.Button("Analyze Video")
101
  output = gr.Textbox(label="Analysis Result")
102
 
103
  submit_button.click(
104
+ fn=analyze_instagram_video,
105
+ inputs=[video_url_input, question_input],
106
  outputs=output
107
  )
108