Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,112 +1,79 @@
|
|
1 |
-
import sys
|
2 |
-
import subprocess
|
3 |
-
import pkg_resources
|
4 |
-
import os
|
5 |
-
import tempfile
|
6 |
-
import requests
|
7 |
-
|
8 |
-
required_packages = {
|
9 |
-
'torch': 'torch',
|
10 |
-
'gradio': 'gradio',
|
11 |
-
'transformers': 'transformers',
|
12 |
-
'decord': 'decord',
|
13 |
-
'numpy': 'numpy',
|
14 |
-
'instaloader': 'instaloader'
|
15 |
-
}
|
16 |
-
|
17 |
-
def install_packages(packages):
|
18 |
-
for package in packages:
|
19 |
-
subprocess.check_call([sys.executable, "-m", "pip", "install", package])
|
20 |
-
|
21 |
-
def check_and_install_packages():
|
22 |
-
installed_packages = {pkg.key for pkg in pkg_resources.working_set}
|
23 |
-
missing_packages = [required_packages[pkg] for pkg in required_packages if pkg not in installed_packages]
|
24 |
-
|
25 |
-
if missing_packages:
|
26 |
-
print("Installing missing packages...")
|
27 |
-
install_packages(missing_packages)
|
28 |
-
print("Packages installed successfully.")
|
29 |
-
else:
|
30 |
-
print("All required packages are already installed.")
|
31 |
-
|
32 |
-
# Check and install required packages
|
33 |
-
check_and_install_packages()
|
34 |
-
|
35 |
-
# Now import the required modules
|
36 |
import gradio as gr
|
37 |
import torch
|
38 |
-
from
|
|
|
|
|
|
|
|
|
39 |
from decord import VideoReader, cpu
|
40 |
import numpy as np
|
41 |
-
import instaloader
|
42 |
-
|
43 |
-
# Initialize Instaloader
|
44 |
-
L = instaloader.Instaloader()
|
45 |
-
|
46 |
-
def download_instagram_video(url):
|
47 |
-
try:
|
48 |
-
# Use a third-party API service like savefrom.net
|
49 |
-
api_url = f"https://api.savefrom.net/1/info?url={url}"
|
50 |
-
response = requests.get(api_url)
|
51 |
-
data = response.json()
|
52 |
-
|
53 |
-
if 'url' in data:
|
54 |
-
video_url = data['url']
|
55 |
-
video_data = requests.get(video_url)
|
56 |
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
|
61 |
-
|
62 |
-
else:
|
63 |
-
print(f"Error: {data.get('error', 'Unknown error')}")
|
64 |
-
return None
|
65 |
-
except Exception as e:
|
66 |
-
print(f"Error downloading video: {e}")
|
67 |
-
return None
|
68 |
-
|
69 |
-
def process_video(video_path, max_frames=64):
|
70 |
vr = VideoReader(video_path, ctx=cpu(0))
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
75 |
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
|
81 |
-
def
|
82 |
-
|
83 |
-
|
84 |
|
85 |
-
|
86 |
-
if not video_path:
|
87 |
-
return "Failed to download the video. Please check the URL and try again."
|
88 |
|
89 |
-
|
90 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
91 |
return response
|
92 |
|
93 |
-
|
|
|
|
|
|
|
|
|
94 |
with gr.Blocks() as demo:
|
95 |
-
gr.Markdown(
|
96 |
-
gr.Markdown(
|
97 |
|
98 |
with gr.Row():
|
99 |
-
|
100 |
-
|
101 |
-
question_input = gr.Textbox(label="Ask a question about the video", placeholder="What's happening in this Instagram video?")
|
102 |
-
submit_button = gr.Button("Analyze Video")
|
103 |
-
output = gr.Textbox(label="Analysis Result")
|
104 |
|
105 |
-
|
106 |
-
|
107 |
-
inputs=[video_url_input, question_input],
|
108 |
-
outputs=output
|
109 |
-
)
|
110 |
|
111 |
if __name__ == "__main__":
|
112 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
import torch
|
3 |
+
from llava.model.builder import load_pretrained_model
|
4 |
+
from llava.mm_utils import get_model_name_from_path, process_images, tokenizer_image_token
|
5 |
+
from llava.constants import IMAGE_TOKEN_INDEX, DEFAULT_IMAGE_TOKEN
|
6 |
+
from llava.conversation import conv_templates
|
7 |
+
import copy
|
8 |
from decord import VideoReader, cpu
|
9 |
import numpy as np
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
+
title = "# 📸 Instagram Reels Analiz Aracı"
|
12 |
+
description = """Bu araç, yüklenen Instagram Reels videolarını analiz eder ve içeriği özetler.
|
13 |
+
Video hakkında genel bir açıklama yapar ve klipte neler olup bittiğini adım adım anlatır."""
|
14 |
|
15 |
+
def load_video(video_path, max_frames_num=64, fps=1):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
vr = VideoReader(video_path, ctx=cpu(0))
|
17 |
+
total_frame_num = len(vr)
|
18 |
+
frame_idx = list(range(0, total_frame_num, int(vr.get_avg_fps() / fps)))
|
19 |
+
|
20 |
+
if len(frame_idx) > max_frames_num:
|
21 |
+
frame_idx = np.linspace(0, total_frame_num - 1, max_frames_num, dtype=int).tolist()
|
22 |
+
|
23 |
+
video_frames = vr.get_batch(frame_idx).asnumpy()
|
24 |
+
return video_frames, len(frame_idx)
|
25 |
+
|
26 |
+
# Model yükleme
|
27 |
+
pretrained = "lmms-lab/LLaVA-Video-7B-Qwen2"
|
28 |
+
model_name = "llava_qwen"
|
29 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
30 |
|
31 |
+
print("Model yükleniyor...")
|
32 |
+
tokenizer, model, image_processor, max_length = load_pretrained_model(pretrained, None, model_name, torch_dtype="bfloat16", device_map="auto")
|
33 |
+
model.eval()
|
34 |
+
print("Model başarıyla yüklendi!")
|
35 |
|
36 |
+
def analyze_reel(video_path):
|
37 |
+
video_frames, frame_count = load_video(video_path)
|
38 |
+
video = image_processor.preprocess(video_frames, return_tensors="pt")["pixel_values"].to(device).bfloat16()
|
39 |
|
40 |
+
prompt = f"{DEFAULT_IMAGE_TOKEN}Bu Instagram Reels videosunu analiz et. Önce videonun genel içeriğini özetle, ardından klipte neler olup bittiğini adım adım açıkla. Video {frame_count} kareye bölünmüştür."
|
|
|
|
|
41 |
|
42 |
+
conv = copy.deepcopy(conv_templates["qwen_1_5"])
|
43 |
+
conv.append_message(conv.roles[0], prompt)
|
44 |
+
conv.append_message(conv.roles[1], None)
|
45 |
+
prompt = conv.get_prompt()
|
46 |
+
|
47 |
+
input_ids = tokenizer_image_token(prompt, tokenizer, IMAGE_TOKEN_INDEX, return_tensors="pt").unsqueeze(0).to(device)
|
48 |
+
|
49 |
+
with torch.no_grad():
|
50 |
+
output = model.generate(
|
51 |
+
input_ids,
|
52 |
+
images=[video],
|
53 |
+
modalities=["video"],
|
54 |
+
do_sample=False,
|
55 |
+
temperature=0,
|
56 |
+
max_new_tokens=1024,
|
57 |
+
)
|
58 |
+
|
59 |
+
response = tokenizer.batch_decode(output, skip_special_tokens=True)[0].strip()
|
60 |
return response
|
61 |
|
62 |
+
def gradio_interface(video_file):
|
63 |
+
if video_file is None:
|
64 |
+
return "Lütfen bir video dosyası yükleyin."
|
65 |
+
return analyze_reel(video_file)
|
66 |
+
|
67 |
with gr.Blocks() as demo:
|
68 |
+
gr.Markdown(title)
|
69 |
+
gr.Markdown(description)
|
70 |
|
71 |
with gr.Row():
|
72 |
+
video_input = gr.Video(label="Instagram Reels Videosu")
|
73 |
+
output = gr.Textbox(label="Analiz Sonucu", lines=10)
|
|
|
|
|
|
|
74 |
|
75 |
+
analyze_button = gr.Button("Reels'i Analiz Et")
|
76 |
+
analyze_button.click(fn=gradio_interface, inputs=video_input, outputs=output)
|
|
|
|
|
|
|
77 |
|
78 |
if __name__ == "__main__":
|
79 |
+
demo.launch(share=True)
|