Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pytube
|
3 |
+
from youtube_transcript_api import YouTubeTranscriptApi as yt
|
4 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
5 |
+
import os
|
6 |
+
from langchain import PromptTemplate
|
7 |
+
from langchain import LLMChain
|
8 |
+
from langchain_together import Together
|
9 |
+
import re
|
10 |
+
import json
|
11 |
+
|
12 |
+
def Summary_BART(text):
|
13 |
+
checkpoint = "sshleifer/distilbart-cnn-12-6"
|
14 |
+
tokenizer = AutoTokenizer.from_pretrained(checkpoint)
|
15 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(checkpoint)
|
16 |
+
inputs = tokenizer(text,
|
17 |
+
max_length=1024,
|
18 |
+
truncation=True,
|
19 |
+
return_tensors="pt")
|
20 |
+
summary_ids = model.generate(inputs["input_ids"])
|
21 |
+
summary = tokenizer.batch_decode(summary_ids,
|
22 |
+
skip_special_tokens=True,
|
23 |
+
clean_up_tokenization_spaces=False)
|
24 |
+
return summary[0]
|
25 |
+
|
26 |
+
def YtToQuizz(link,difficulty_level):
|
27 |
+
video_id=pytube.extract.video_id(link)
|
28 |
+
transcript=yt.get_transcript(video_id)
|
29 |
+
data=""
|
30 |
+
for text in transcript:
|
31 |
+
data+=text.get('text')
|
32 |
+
summary=Summary_BART(data)
|
33 |
+
print(summary)
|
34 |
+
mcq_template = """
|
35 |
+
Give a 10 different multiple-choice question MCQ related to the summary: {summary}
|
36 |
+
The difficulty level of the question should be: {difficulty_level}
|
37 |
+
|
38 |
+
Please provide the following in:
|
39 |
+
1. Question
|
40 |
+
2. Correct answer
|
41 |
+
3. Three plausible incorrect answer options
|
42 |
+
4. Proper Mcqs format
|
43 |
+
"""
|
44 |
+
prompt=PromptTemplate(
|
45 |
+
input_variables=['summary','difficulty_level'],
|
46 |
+
template=mcq_template
|
47 |
+
)
|
48 |
+
llama3 = Together(model = "meta-llama/Llama-3-70b-chat-hf",
|
49 |
+
max_tokens = 2500
|
50 |
+
)
|
51 |
+
Generated_mcqs=LLMChain(llm=llama3,prompt=prompt)
|
52 |
+
|
53 |
+
response = Generated_mcqs.invoke({
|
54 |
+
"summary": summary,
|
55 |
+
"difficulty_level": difficulty_level
|
56 |
+
})
|
57 |
+
st.write("MCQ's are following")
|
58 |
+
st.write(response['text'])
|
59 |
+
|
60 |
+
|
61 |
+
def main():
|
62 |
+
st.title("YouTube video Subtitle to MCQ's Quizz")
|
63 |
+
url_link=st.text_area("Enter YouTube video link")
|
64 |
+
diffculity_level=st.selectbox("Select diffculity level:",["Eassy","Medium","Hard"])
|
65 |
+
if st.button("Generate MCQS Quizz"):
|
66 |
+
YtToQuizz(url_link,diffculity_level)
|
67 |
+
if __name__ == '__main__':
|
68 |
+
main()
|