Spaces:
Sleeping
Sleeping
Initial commmit
Browse files- .DS_Store +0 -0
- .gitattributes +2 -0
- README.md +5 -5
- RNN_model.keras +3 -0
- __init__.py +0 -0
- app.py +47 -0
- functions/.DS_Store +0 -0
- functions/RNN_model.keras +3 -0
- functions/__init__.py +0 -0
- functions/convert_time.py +52 -0
- functions/model_infer.py +49 -0
- functions/punctuation.py +58 -0
- requirements.txt +6 -0
.DS_Store
ADDED
Binary file (6.15 kB). View file
|
|
.gitattributes
CHANGED
@@ -22,6 +22,8 @@
|
|
22 |
*.pt filter=lfs diff=lfs merge=lfs -text
|
23 |
*.pth filter=lfs diff=lfs merge=lfs -text
|
24 |
*.rar filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
25 |
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
26 |
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
27 |
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
|
|
22 |
*.pt filter=lfs diff=lfs merge=lfs -text
|
23 |
*.pth filter=lfs diff=lfs merge=lfs -text
|
24 |
*.rar filter=lfs diff=lfs merge=lfs -text
|
25 |
+
*.keras filter=lfs diff=lfs merge=lfs -text
|
26 |
+
functions/*.keras filter=lfs diff=lfs merge=lfs -text
|
27 |
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
28 |
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
29 |
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
README.md
CHANGED
@@ -1,12 +1,12 @@
|
|
1 |
---
|
2 |
title: Sponsoredbye
|
3 |
-
emoji:
|
4 |
-
colorFrom:
|
5 |
-
colorTo:
|
6 |
sdk: gradio
|
7 |
-
sdk_version: 4.31.
|
8 |
app_file: app.py
|
9 |
-
pinned:
|
10 |
---
|
11 |
|
12 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
1 |
---
|
2 |
title: Sponsoredbye
|
3 |
+
emoji: π
|
4 |
+
colorFrom: blue
|
5 |
+
colorTo: purple
|
6 |
sdk: gradio
|
7 |
+
sdk_version: 4.31.4
|
8 |
app_file: app.py
|
9 |
+
pinned: true
|
10 |
---
|
11 |
|
12 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
RNN_model.keras
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:642ec9499996ca6dcd3c8f2874ae3c5d9ca0095064d2f8faae1f12b2fea1e020
|
3 |
+
size 3974964
|
__init__.py
ADDED
File without changes
|
app.py
ADDED
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from os import pipe
|
2 |
+
import re
|
3 |
+
import gradio as gr
|
4 |
+
from functions.punctuation import punctuate
|
5 |
+
from functions.model_infer import predict_from_document
|
6 |
+
from functions.convert_time import match_mask_and_transcript
|
7 |
+
|
8 |
+
|
9 |
+
title = "sponsoredBye - never listen to sponsors again"
|
10 |
+
description = "Sponsored sections in videos are annoying and take up a lot of time. Improve your YouTube watching experience, by filling in the youtube url and figure out what segments to skip."
|
11 |
+
article = "Check out [the original Rick and Morty Bot](https://huggingface.co/spaces/kingabzpro/Rick_and_Morty_Bot) that this demo is based off of."
|
12 |
+
|
13 |
+
|
14 |
+
def pipeline(video_url):
|
15 |
+
video_id = video_url.split("?v=")[-1]
|
16 |
+
punctuated_text, transcript = punctuate(video_id)
|
17 |
+
sentences = re.split(r"[\.\!\?]\s", punctuated_text)
|
18 |
+
classification, probs = predict_from_document(sentences)
|
19 |
+
# return punctuated_text
|
20 |
+
times, timestamps = match_mask_and_transcript(sentences, transcript, classification)
|
21 |
+
return [{"begin": time[0], "end": time[1]} for time in times]
|
22 |
+
# return [
|
23 |
+
# {
|
24 |
+
# "start": "12:05",
|
25 |
+
# "end": "12:52",
|
26 |
+
# "classification": str(classification),
|
27 |
+
# "probabilities": probs,
|
28 |
+
# "times": times,
|
29 |
+
# "timestamps": timestamps,
|
30 |
+
# }
|
31 |
+
# ]
|
32 |
+
|
33 |
+
|
34 |
+
# print(pipeline("VL5M5ZihJK4"))
|
35 |
+
demo = gr.Interface(
|
36 |
+
fn=pipeline,
|
37 |
+
title=title,
|
38 |
+
description=description,
|
39 |
+
inputs="text",
|
40 |
+
# outputs=gr.Label(num_top_classes=3),
|
41 |
+
outputs="json",
|
42 |
+
examples=[
|
43 |
+
"https://www.youtube.com/watch?v=UjtOGPJ0URM",
|
44 |
+
"https://www.youtube.com/watch?v=TrZyuCh9df0",
|
45 |
+
],
|
46 |
+
)
|
47 |
+
demo.launch(share=True)
|
functions/.DS_Store
ADDED
Binary file (6.15 kB). View file
|
|
functions/RNN_model.keras
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:642ec9499996ca6dcd3c8f2874ae3c5d9ca0095064d2f8faae1f12b2fea1e020
|
3 |
+
size 3974964
|
functions/__init__.py
ADDED
File without changes
|
functions/convert_time.py
ADDED
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import re
|
2 |
+
from thefuzz import fuzz
|
3 |
+
import numpy as np
|
4 |
+
|
5 |
+
|
6 |
+
def match_mask_and_transcript(split_punct, transcript, classification):
|
7 |
+
"""
|
8 |
+
Input:
|
9 |
+
split_punct: the punctuated text, split on ?/!/.\s,
|
10 |
+
transcript: original transcript with timestamps
|
11 |
+
classification: classification object (list of numbers 0,1)
|
12 |
+
Output: times
|
13 |
+
"""
|
14 |
+
|
15 |
+
# Get the sponsored part
|
16 |
+
sponsored_segment = []
|
17 |
+
for i, val in enumerate(classification):
|
18 |
+
if val == 1:
|
19 |
+
sponsored_segment.append(split_punct[i])
|
20 |
+
|
21 |
+
segment = " ".join(sponsored_segment)
|
22 |
+
sim_scores = list()
|
23 |
+
|
24 |
+
# Check the similarity scores between the sponsored part and the transcript parts
|
25 |
+
for elem in transcript:
|
26 |
+
sim_scores.append(fuzz.partial_ratio(segment, elem["text"]))
|
27 |
+
|
28 |
+
# Get the scores and check if they are above mean + 2*stdev
|
29 |
+
scores = np.array(sim_scores)
|
30 |
+
timestamp_mask = (scores > np.mean(scores) + np.std(scores) * 2).astype(int)
|
31 |
+
timestamps = [
|
32 |
+
(transcript[i]["start"], transcript[i]["duration"])
|
33 |
+
for i, elem in enumerate(timestamp_mask)
|
34 |
+
if elem == 1
|
35 |
+
]
|
36 |
+
|
37 |
+
# Get the timestamp segments
|
38 |
+
times = []
|
39 |
+
current = -1
|
40 |
+
current_time = 0
|
41 |
+
for elem in timestamps:
|
42 |
+
# Threshold of 5 to see if it is a jump to another segment (also to make sure smaller segments are added together
|
43 |
+
if elem[0] > (current_time + 15):
|
44 |
+
current += 1
|
45 |
+
times.append((elem[0], elem[0] + elem[1]))
|
46 |
+
current_time = elem[0] + elem[1]
|
47 |
+
else:
|
48 |
+
times[current] = (times[current][0], elem[0] + elem[1])
|
49 |
+
current_time = elem[0] + elem[1]
|
50 |
+
|
51 |
+
return_times = [x for x in times if (x[1] - x[0]) > 10]
|
52 |
+
return return_times, timestamps
|
functions/model_infer.py
ADDED
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from keras.preprocessing.sequence import pad_sequences
|
2 |
+
import numpy as np
|
3 |
+
import re
|
4 |
+
|
5 |
+
# import tensorflow as tf
|
6 |
+
import os
|
7 |
+
import requests
|
8 |
+
from keras.models import load_model
|
9 |
+
|
10 |
+
headers = {"Authorization": f"Bearer {os.environ['HF_Token']}"}
|
11 |
+
|
12 |
+
model = load_model("./RNN_model.keras")
|
13 |
+
|
14 |
+
|
15 |
+
def query_embeddings(texts):
|
16 |
+
payload = {"inputs": texts, "options": {"wait_for_model": True}}
|
17 |
+
|
18 |
+
model_id = "sentence-transformers/sentence-t5-base"
|
19 |
+
API_URL = (
|
20 |
+
f"https://api-inference.huggingface.co/pipeline/feature-extraction/{model_id}"
|
21 |
+
)
|
22 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
23 |
+
return response.json()
|
24 |
+
|
25 |
+
|
26 |
+
def preprocess(sentences):
|
27 |
+
max_len = 1682
|
28 |
+
embeddings = query_embeddings(sentences)
|
29 |
+
|
30 |
+
if len(sentences) > max_len:
|
31 |
+
X = embeddings[:max_len]
|
32 |
+
else:
|
33 |
+
X = embeddings
|
34 |
+
X_padded = pad_sequences([X], maxlen=max_len, dtype="float32", padding="post")
|
35 |
+
return X_padded
|
36 |
+
|
37 |
+
|
38 |
+
def predict_from_document(sentences):
|
39 |
+
preprop = preprocess(sentences)
|
40 |
+
prediction = model.predict(preprop)
|
41 |
+
# Set the prediction threshold to 0.8 instead of 0.5, now use mean
|
42 |
+
if np.mean(prediction) < 0.5:
|
43 |
+
output = (prediction.flatten()[: len(sentences)] >= 0.5).astype(int)
|
44 |
+
else:
|
45 |
+
output = (
|
46 |
+
prediction.flatten()[: len(sentences)]
|
47 |
+
>= np.mean(prediction) * 1.20 # + np.std(prediction)
|
48 |
+
).astype(int)
|
49 |
+
return output, prediction.flatten()[: len(sentences)]
|
functions/punctuation.py
ADDED
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
2 |
+
from youtube_transcript_api import YouTubeTranscriptApi
|
3 |
+
import json
|
4 |
+
import os
|
5 |
+
|
6 |
+
headers = {
|
7 |
+
"Authorization": f"Bearer {os.environ['HF_Token']}"
|
8 |
+
} # NOTE: put this somewhere else
|
9 |
+
|
10 |
+
|
11 |
+
def retrieve_transcript(vid_id):
|
12 |
+
try:
|
13 |
+
transcript = YouTubeTranscriptApi.get_transcript(vid_id)
|
14 |
+
return transcript
|
15 |
+
except Exception as e:
|
16 |
+
return None
|
17 |
+
|
18 |
+
|
19 |
+
def split_transcript(transcript, chunk_size=40):
|
20 |
+
sentences = []
|
21 |
+
for i in range(0, len(transcript), chunk_size):
|
22 |
+
to_add = [x["text"] for x in transcript[i : i + chunk_size]]
|
23 |
+
sentences.append(" ".join(to_add))
|
24 |
+
return sentences
|
25 |
+
|
26 |
+
|
27 |
+
def query_punctuation(splits):
|
28 |
+
payload = {"inputs": splits}
|
29 |
+
API_URL = "https://api-inference.huggingface.co/models/oliverguhr/fullstop-punctuation-multilang-large"
|
30 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
31 |
+
return response.json()
|
32 |
+
|
33 |
+
|
34 |
+
def parse_output(output, comb):
|
35 |
+
total = []
|
36 |
+
|
37 |
+
# loop over the response from the huggingface api
|
38 |
+
for i, o in enumerate(output):
|
39 |
+
added = 0
|
40 |
+
tt = comb[i]
|
41 |
+
for elem in o:
|
42 |
+
# Loop over the output chunks and add the . and ?
|
43 |
+
if elem["entity_group"] not in ["0", ",", ""]:
|
44 |
+
split = elem["end"] + added
|
45 |
+
tt = tt[:split] + elem["entity_group"] + tt[split:]
|
46 |
+
added += 1
|
47 |
+
total.append(tt)
|
48 |
+
return " ".join(total)
|
49 |
+
|
50 |
+
|
51 |
+
def punctuate(video_id):
|
52 |
+
transcript = retrieve_transcript(video_id)
|
53 |
+
splits = split_transcript(
|
54 |
+
transcript
|
55 |
+
) # Get the transcript from the YoutubeTranscriptApi
|
56 |
+
resp = query_punctuation(splits) # Get the response from the Inference API
|
57 |
+
punctuated_transcript = parse_output(resp, splits)
|
58 |
+
return punctuated_transcript, transcript
|
requirements.txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
youtube_transcript_api
|
2 |
+
thefuzz
|
3 |
+
numpy
|
4 |
+
tensorflow==2.15
|
5 |
+
keras
|
6 |
+
keras-nlp
|