Spaces:
Runtime error
Runtime error
Commit
Β·
e95a3a8
0
Parent(s):
init
Browse files- .gitignore +1 -0
- app.py +78 -0
- requirements.txt +7 -0
- tiktok.py +26 -0
.gitignore
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
venv/
|
app.py
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoModel, AutoTokenizer
|
| 3 |
+
import torch
|
| 4 |
+
from decord import VideoReader, cpu
|
| 5 |
+
import os
|
| 6 |
+
import spaces
|
| 7 |
+
|
| 8 |
+
# Load the model and tokenizer
|
| 9 |
+
model_name = "openbmb/MiniCPM-V-2_6-int4"
|
| 10 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
|
| 11 |
+
model = AutoModel.from_pretrained(model_name, trust_remote_code=True, device_map="auto")
|
| 12 |
+
model.eval()
|
| 13 |
+
|
| 14 |
+
MAX_NUM_FRAMES = 64
|
| 15 |
+
VIDEO_EXTENSIONS = {'.mp4', '.mkv', '.mov', '.avi', '.flv', '.wmv', '.webm', '.m4v'}
|
| 16 |
+
|
| 17 |
+
def get_file_extension(filename):
|
| 18 |
+
return os.path.splitext(filename)[1].lower()
|
| 19 |
+
|
| 20 |
+
def is_video(filename):
|
| 21 |
+
return get_file_extension(filename) in VIDEO_EXTENSIONS
|
| 22 |
+
|
| 23 |
+
def encode_video(video):
|
| 24 |
+
def uniform_sample(l, n):
|
| 25 |
+
gap = len(l) / n
|
| 26 |
+
idxs = [int(i * gap + gap / 2) for i in range(n)]
|
| 27 |
+
return [l[i] for i in idxs]
|
| 28 |
+
|
| 29 |
+
if hasattr(video, 'path'):
|
| 30 |
+
video_path = video.path
|
| 31 |
+
else:
|
| 32 |
+
video_path = video.file.path
|
| 33 |
+
|
| 34 |
+
vr = VideoReader(video_path, ctx=cpu(0))
|
| 35 |
+
total_frames = len(vr)
|
| 36 |
+
if total_frames <= MAX_NUM_FRAMES:
|
| 37 |
+
frame_idxs = list(range(total_frames))
|
| 38 |
+
else:
|
| 39 |
+
frame_idxs = uniform_sample(range(total_frames), MAX_NUM_FRAMES)
|
| 40 |
+
|
| 41 |
+
frames = vr.get_batch(frame_idxs).asnumpy()
|
| 42 |
+
return frames
|
| 43 |
+
|
| 44 |
+
@spaces.GPU
|
| 45 |
+
def analyze_video(video, prompt):
|
| 46 |
+
if not is_video(video.name):
|
| 47 |
+
return "Please upload a valid video file."
|
| 48 |
+
|
| 49 |
+
frames = encode_video(video)
|
| 50 |
+
|
| 51 |
+
# Prepare the frames for the model
|
| 52 |
+
inputs = model.vpm(frames)
|
| 53 |
+
|
| 54 |
+
# Generate the caption with the user's prompt
|
| 55 |
+
with torch.no_grad():
|
| 56 |
+
outputs = model.generate(inputs=inputs, tokenizer=tokenizer, max_new_tokens=50, prompt=prompt)
|
| 57 |
+
|
| 58 |
+
# Decode the output
|
| 59 |
+
caption = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 60 |
+
|
| 61 |
+
return caption
|
| 62 |
+
|
| 63 |
+
# Create the Gradio interface using Blocks
|
| 64 |
+
with gr.Blocks(title="Video Analyzer using MiniCPM-V-2.6-int4") as iface:
|
| 65 |
+
gr.Markdown("# Video Analyzer using MiniCPM-V-2.6-int4")
|
| 66 |
+
gr.Markdown("Upload a video to get an analysis using the MiniCPM-V-2.6-int4 model.")
|
| 67 |
+
gr.Markdown("This model uses 4-bit quantization for improved efficiency. [Learn more](https://huggingface.co/openbmb/MiniCPM-V-2_6-int4)")
|
| 68 |
+
|
| 69 |
+
with gr.Row():
|
| 70 |
+
video_input = gr.Video()
|
| 71 |
+
prompt_input = gr.Textbox(label="Prompt (optional)", placeholder="Enter a prompt to guide the analysis...")
|
| 72 |
+
analysis_output = gr.Textbox(label="Video Analysis")
|
| 73 |
+
|
| 74 |
+
analyze_button = gr.Button("Analyze Video")
|
| 75 |
+
analyze_button.click(fn=analyze_video, inputs=[video_input, prompt_input], outputs=analysis_output)
|
| 76 |
+
|
| 77 |
+
# Launch the interface
|
| 78 |
+
iface.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Pillow==10.1.0
|
| 2 |
+
torch==2.1.2
|
| 3 |
+
torchvision==0.16.2
|
| 4 |
+
transformers==4.40.0
|
| 5 |
+
sentencepiece==0.1.99
|
| 6 |
+
accelerate==0.30.1
|
| 7 |
+
bitsandbytes==0.43.1
|
tiktok.py
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import asyncio
|
| 2 |
+
from douyin_tiktok_scraper.scraper import Scraper
|
| 3 |
+
import traceback
|
| 4 |
+
|
| 5 |
+
api = Scraper()
|
| 6 |
+
|
| 7 |
+
async def hybrid_parsing(url: str) -> dict:
|
| 8 |
+
try:
|
| 9 |
+
result = await api.hybrid_parsing(url)
|
| 10 |
+
print(f"The hybrid parsing result:\n {result}")
|
| 11 |
+
return result
|
| 12 |
+
except Exception as e:
|
| 13 |
+
print(f"An error occurred: {str(e)}")
|
| 14 |
+
print("Traceback:")
|
| 15 |
+
traceback.print_exc()
|
| 16 |
+
return None
|
| 17 |
+
|
| 18 |
+
async def main():
|
| 19 |
+
url = input("Paste Douyin/TikTok/Bilibili share URL here: ")
|
| 20 |
+
result = await hybrid_parsing(url)
|
| 21 |
+
if result:
|
| 22 |
+
print("Parsing successful!")
|
| 23 |
+
else:
|
| 24 |
+
print("Parsing failed.")
|
| 25 |
+
|
| 26 |
+
asyncio.run(main())
|