File size: 4,668 Bytes
f8b370e
 
affcffd
f8b370e
 
 
 
 
 
 
 
 
948bf14
f8b370e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a3737ea
 
f8b370e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8a36d25
f8b370e
 
 
 
 
 
 
a3737ea
f8b370e
148dbc3
f8b370e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
88eb8ee
d1ba93a
f8b370e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import tensorflow as tf
import gradio as gr
## importing necessary libraries
from transformers import AutoTokenizer, TFAutoModelForQuestionAnswering

tokenizer = AutoTokenizer.from_pretrained("bert-large-uncased-whole-word-masking-finetuned-squad")
model = TFAutoModelForQuestionAnswering.from_pretrained("bert-large-uncased-whole-word-masking-finetuned-squad",return_dict=False)
from transformers import pipeline

nlp = pipeline("question-answering", model=model, tokenizer=tokenizer)

context = "My name is Hema Raikhola, i am a data scientist and machine learning engineer."
question = "what is my profession ?"

result = nlp(question = question, context=context)

print(f"QUESTION: {question}")
print(f"ANSWER: {result['answer']}")

# creating the function
def func(context, question):
  result = nlp(question = question, context=context)
  return result['answer']

example_1 = "(1) Kanisha,Preeti,Hema and Shaksham are the team members.They are working on a science project"
qst_1 =  "who are the team members?"

example_2 = "(2) Natural Language Processing (NLP) allows machines to break down and interpret human language. It's at the core of tools we use every day – from translation software, chatbots, spam filters, and search engines, to grammar correction software, voice assistants, and social media monitoring tools."
qst_2 =  "What is NLP used for?"



# for visual QA
from transformers import ViltProcessor, ViltForQuestionAnswering


def getResult(query, image):
    # prepare image + question
    #image = Image.open(BytesIO(base64.b64decode(base64_encoded_image)))
    text = query

    processor = ViltProcessor.from_pretrained(
        "dandelin/vilt-b32-finetuned-vqa")
    model = ViltForQuestionAnswering.from_pretrained(
        "dandelin/vilt-b32-finetuned-vqa")

    # prepare inputs
    encoding = processor(image, text, return_tensors="pt")

    ## forward pass
    outputs = model(**encoding)
    logits = outputs.logits
    idx = logits.argmax(-1).item()
    print("Predicted answer:", model.config.id2label[idx])
    return model.config.id2label[idx]


#for youtube video summarization
    
from transformers import pipeline
from youtube_transcript_api import YouTubeTranscriptApi
import gradio as gr


def summarize(Youtube_Video_Link):
  video_id = Youtube_Video_Link.split("=")[1]
  try:
    transcript = YouTubeTranscriptApi.get_transcript(video_id)
    summarizer = pipeline('summarization',model='facebook/bart-large-cnn')
    input_text = ""
    for i in transcript:
        input_text += ' ' + i['text']
    num_iters = int(len(input_text)/1000)
    summarized_text = []
    for i in range(0, num_iters + 1):
      start = 0
      start = i * 1000
      end = (i + 1) * 1000
      print("input text \n" + input_text[start:end])
      out = summarizer(input_text[start:end])
      out = out[0]
      out = out['summary_text']
      print("Summarized text\n"+out)
      summarized_text.append(out)
    output_text=' '.join(summarized_text)
    return output_text
  except:
    return "Some Error has occurred either with Video link passed is invalid or No Captions present for this video"

title = "YouTube Live Video Summarization"
examples = [("https://www.youtube.com/watch?v=zKvd1JwJ4Po"),("https://www.youtube.com/watch?v=9izcbNYmP8M"),]
description = "Get YouTube Video Summarization. Just Enter the YouTube Video link below. Make sure Video has Captions and it is not very long as Model Computation time will Increase."
Youtube_Video_Link = gr.Textbox("Input YouTube Link here (Note: This will take time if passed a long video)", show_label=False)
App= gr.Interface(fn=summarize, inputs=Youtube_Video_Link, outputs="text", examples=examples,description=description, title=title,)

## finished  youtube video summarization


# creating the interface
iface = gr.Interface(fn=getResult, inputs=[
                     "text", gr.Image(type="pil")], outputs="text")

# creating the interface
app = gr.Interface(fn=func,
                   inputs = ['textbox', 'text'],
                   outputs = gr.Textbox( lines=10), 
                   title = 'Question Answering bot',
                   description = 'Input context and question, then get answers!',
                   examples = [[example_1, qst_1],
                               [example_2, qst_2]],
                    theme = "darkhuggingface",
                   Timeout =120,
                   allow_flagging="manual",
                   flagging_options=["incorrect", "ambiguous", "offensive", "other"],
              
                   ).queue()
# launching the apps
gr.TabbedInterface([iface,app,App],["Visual QA","Text QA","Video Summarization"]).launch()