Spaces:
Build error
Build error
app.py
CHANGED
|
@@ -1,13 +1,97 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
| 3 |
-
from
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
#https://www.youtube.com/watch?v=smUHQndcmOY&t=425s
|
| 7 |
#html = HTML("<iframe width='560' height='315' src='https://www.youtube.com/watch?v=smUHQndcmOY&t=425s' frameborder='0' allowfullscreen></iframe>")
|
| 8 |
#html = "<iframe width='560' height='315' src='https://www.youtube.com/embed/smUHQndcmOY' frameborder='0' allowfullscreen></iframe>"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
#vid = YouTubeVideo('smUHQndcmOY&t=425s')
|
| 10 |
return html
|
|
|
|
| 11 |
#https://youtu.be/smUHQndcmOY
|
| 12 |
def fun(url):
|
| 13 |
return gr.Video(value=url)
|
|
@@ -24,13 +108,13 @@ with demo:
|
|
| 24 |
)
|
| 25 |
with gr.Row():
|
| 26 |
input_url = gr.Textbox() #gr.HTML(placeholder="Enter a video link here..")
|
| 27 |
-
|
| 28 |
output_vid = gr.HTML()
|
| 29 |
|
| 30 |
b1 = gr.Button("Publish Video")
|
| 31 |
#b2 = gr.Button("Generate Image")
|
| 32 |
|
| 33 |
-
b1.click(display_vid, input_url, output_vid)
|
| 34 |
#b2.click(poem_to_image, poem_txt, output_image)
|
| 35 |
#examples=examples
|
| 36 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from youtube_transcript_api import YouTubeTranscriptApi
|
| 3 |
+
from transformers import AutoTokenizer
|
| 4 |
+
from transformers import pipeline
|
| 5 |
+
from transformers import AutoModelForQuestionAnswering
|
| 6 |
+
import pandas as pd
|
| 7 |
+
from sentence_transformers import SentenceTransformer, util
|
| 8 |
+
import torch
|
| 9 |
|
| 10 |
+
#from IPython.display import HTML, IFrame
|
| 11 |
+
#from IPython.display import YouTubeVideo
|
| 12 |
+
|
| 13 |
+
#input - video link, output - full transcript
|
| 14 |
+
def get_transcript(link):
|
| 15 |
+
video_id = link.split("=")[1]
|
| 16 |
+
print(f"video id extracted is : {video_id}")
|
| 17 |
+
transcript = YouTubeTranscriptApi.get_transcript(video_id)
|
| 18 |
+
FinalTranscript = ' '.join([i['text'] for i in transcript])
|
| 19 |
+
return transcript, video_id
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
#input - question and transcript, output - answer timestamp
|
| 23 |
+
def get_answers_timestamp(question, transcript):
|
| 24 |
+
model_ckpt = "deepset/minilm-uncased-squad2"
|
| 25 |
+
tokenizer = AutoTokenizer.from_pretrained(model_ckpt)
|
| 26 |
+
#question = "any funny examples in video??"
|
| 27 |
+
context = transcript
|
| 28 |
+
inputs = tokenizer(question, context, return_overflowing_tokens=True, max_length=512, stride = 25)
|
| 29 |
+
|
| 30 |
+
#overlaps
|
| 31 |
+
#getting a list of contexts available after striding
|
| 32 |
+
contx=[]
|
| 33 |
+
for window in inputs["input_ids"]:
|
| 34 |
+
#print(f"{tokenizer.decode(window)} \n")
|
| 35 |
+
contx.append(tokenizer.decode(window).split('[SEP]')[1].strip())
|
| 36 |
+
#print(ques)
|
| 37 |
+
#print(contx)
|
| 38 |
+
|
| 39 |
+
model = AutoModelForQuestionAnswering.from_pretrained(model_ckpt)
|
| 40 |
+
lst=[]
|
| 41 |
+
pipe = pipeline("question-answering", model=model, tokenizer=tokenizer)
|
| 42 |
+
for contexts in contx:
|
| 43 |
+
#print(pipe(question=question, context=contexts))
|
| 44 |
+
lst.append(pipe(question=question, context=contexts))
|
| 45 |
+
|
| 46 |
+
lst_scores = [dicts['score'] for dicts in lst]
|
| 47 |
+
#print(lst_scores)
|
| 48 |
+
#getting highest and second highest scores
|
| 49 |
+
idxmax = lst_scores.index(max(lst_scores))
|
| 50 |
+
lst_scores.remove(max(lst_scores))
|
| 51 |
+
idxmax2 = lst_scores.index(max(lst_scores))
|
| 52 |
+
#idxmax, idxmax2
|
| 53 |
+
|
| 54 |
+
idxcont = lst[idxmax2]['start']
|
| 55 |
+
answer = FinalTranscript[len(contx[0])-135 + idxcont:]
|
| 56 |
+
sentence_keyword = answer[:50]
|
| 57 |
+
|
| 58 |
+
dftranscript = pd.DataFrame(transcript)
|
| 59 |
+
#dftranscript.head()
|
| 60 |
+
|
| 61 |
+
modelST = SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2')
|
| 62 |
+
embedding_1= modelST.encode(dftranscript.text, convert_to_tensor=True)
|
| 63 |
+
embedding_2 = modelST.encode(sentence_keyword, convert_to_tensor=True)
|
| 64 |
+
|
| 65 |
+
similarity_tensor = util.pytorch_cos_sim(embedding_1, embedding_2)
|
| 66 |
+
idx = torch.argmax(similarity_tensor)
|
| 67 |
+
start_timestamp = dftranscript.iloc[[int(idx)+1]].start.values[0]
|
| 68 |
+
start_timestamp = round(start_timestamp)
|
| 69 |
+
|
| 70 |
+
return start_timestamp
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
def display_vid(url, question):
|
| 74 |
#https://www.youtube.com/watch?v=smUHQndcmOY&t=425s
|
| 75 |
#html = HTML("<iframe width='560' height='315' src='https://www.youtube.com/watch?v=smUHQndcmOY&t=425s' frameborder='0' allowfullscreen></iframe>")
|
| 76 |
#html = "<iframe width='560' height='315' src='https://www.youtube.com/embed/smUHQndcmOY' frameborder='0' allowfullscreen></iframe>"
|
| 77 |
+
#get embedding and youtube link
|
| 78 |
+
html = "<iframe width='560' height='315' src=" + url + " frameborder='0' allowfullscreen></iframe>"
|
| 79 |
+
print(html)
|
| 80 |
+
|
| 81 |
+
#get transcript
|
| 82 |
+
transcript, video_id = get_transcript(html)
|
| 83 |
+
|
| 84 |
+
#get answer timestamp
|
| 85 |
+
#input - question and transcript, output - answer timestamp
|
| 86 |
+
ans_timestamp = get_answers_timestamp(question, transcript):
|
| 87 |
+
|
| 88 |
+
#created embedding
|
| 89 |
+
#sample - smUHQndcmOY?start=234
|
| 90 |
+
html_out = "<iframe width='560' height='315' src='https://www.youtube.com/embed/" + video_id + "?start=" + ans_timestamp + " title='YouTube video player' frameborder='0' allow='accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture' allowfullscreen></iframe>"
|
| 91 |
+
print(f"html output is : {html_out}")
|
| 92 |
#vid = YouTubeVideo('smUHQndcmOY&t=425s')
|
| 93 |
return html
|
| 94 |
+
|
| 95 |
#https://youtu.be/smUHQndcmOY
|
| 96 |
def fun(url):
|
| 97 |
return gr.Video(value=url)
|
|
|
|
| 108 |
)
|
| 109 |
with gr.Row():
|
| 110 |
input_url = gr.Textbox() #gr.HTML(placeholder="Enter a video link here..")
|
| 111 |
+
input_ques = gr.Textbox()
|
| 112 |
output_vid = gr.HTML()
|
| 113 |
|
| 114 |
b1 = gr.Button("Publish Video")
|
| 115 |
#b2 = gr.Button("Generate Image")
|
| 116 |
|
| 117 |
+
b1.click(display_vid, inputs=[input_url,input_ques], outputs=output_vid)
|
| 118 |
#b2.click(poem_to_image, poem_txt, output_image)
|
| 119 |
#examples=examples
|
| 120 |
|