import gradio as gr from youtube_transcript_api import YouTubeTranscriptApi from transformers import AutoTokenizer from transformers import pipeline from transformers import AutoModelForQuestionAnswering import pandas as pd from sentence_transformers import SentenceTransformer, util import torch model_ckpt = "deepset/minilm-uncased-squad2" tokenizer = AutoTokenizer.from_pretrained(model_ckpt) model = AutoModelForQuestionAnswering.from_pretrained(model_ckpt) modelST = SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2') #input - video link, output - full transcript def get_transcript(link): print("******** Inside get_transcript ********") print(f"link to be extracted is : {link}") video_id = link.split("=")[1] print(f"video id extracted is : {video_id}") transcript = YouTubeTranscriptApi.get_transcript(video_id) FinalTranscript = ' '.join([i['text'] for i in transcript]) return FinalTranscript,transcript, video_id #input - question and transcript, output - answer timestamp def get_answers_timestamp(question, final_transcript, transcript): print("******** Inside get_answers_timestamp ********") context = final_transcript print(f"Input Question is : {question}") print(f"Type of trancript is : {type(context)}, Length of transcript is : {len(context)}") inputs = tokenizer(question, context, return_overflowing_tokens=True, max_length=512, stride = 25) #getting a list of contexts available after striding contx=[] for window in inputs["input_ids"]: #print(f"{tokenizer.decode(window)} \n") contx.append(tokenizer.decode(window).split('[SEP]')[1].strip()) #print(ques) #print(contx) lst=[] pipe = pipeline("question-answering", model=model, tokenizer=tokenizer) for contexts in contx: lst.append(pipe(question=question, context=contexts)) print(f"contx list is : {contx}") lst_scores = [dicts['score'] for dicts in lst] print(f"lst_scores is : {lst_scores}") #getting highest and second highest scores idxmax = lst_scores.index(max(lst_scores)) lst_scores.remove(max(lst_scores)) idxmax2 = lst_scores.index(max(lst_scores)) sentence_for_timestamp = lst[idxmax]['answer'] sentence_for_timestamp_secondbest = lst[idxmax2]['answer'] dftranscript = pd.DataFrame(transcript) embedding_1= modelST.encode(dftranscript.text, convert_to_tensor=True) embedding_2 = modelST.encode(sentence_for_timestamp, convert_to_tensor=True) embedding_3 = modelST.encode(sentence_for_timestamp_secondbest, convert_to_tensor=True) similarity_tensor = util.pytorch_cos_sim(embedding_1, embedding_2) idx = torch.argmax(similarity_tensor) start_timestamp = dftranscript.iloc[[int(idx)-3]].start.values[0] start_timestamp = round(start_timestamp) similarity_tensor_secondbest = util.pytorch_cos_sim(embedding_1, embedding_3) idx_secondbest = torch.argmax(similarity_tensor_secondbest) start_timestamp_secondbest = dftranscript.iloc[[int(idx_secondbest)-3]].start.values[0] start_timestamp_secondbest = round(start_timestamp_secondbest) return start_timestamp, start_timestamp_secondbest def display_vid(url, question, sample_question=None, example_video=None): print("******** display_vid ********") if question == '': question = sample_question #get embedding and youtube link for initial video html_in = "" #print(html) if len(example_video) !=0 : #is not None: print(f"example_video is : {example_video}") url = example_video[0] #get transcript final_transcript, transcript, video_id = get_transcript(url) #get answer timestamp #input - question and transcript, output - answer timestamp ans_timestamp, ans_timestamp_secondbest = get_answers_timestamp(question, final_transcript, transcript) #created embedding width='560' height='315' html_out = "" print(f"html output is : {html_out}") html_out_secondbest = "" if question == '': print(f"Inside display_vid(), Sample_Question coming from Radio box is BEFORE : {sample_question}") sample_ques = set_example_question(sample_question) print(f"Inside display_vid(), Sample Question coming from Radio box is AFTER : {sample_ques}") else: sample_ques = question return html_out, html_out_secondbest, sample_ques, url def set_example_question(sample_question): print(f"******* Inside Sample Questions ********") print(f"Sample Question coming from Radio box is : {sample_question}") print("What is the Return value : {gr.Radio.update(value=sample_question)}") return gr.Radio.update(value=sample_question) #input_ques.update(example) demo = gr.Blocks() with demo: gr.Markdown("