Rehman1603 commited on
Commit
61e7eb4
·
verified ·
1 Parent(s): 3961677

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -18
app.py CHANGED
@@ -7,6 +7,7 @@ from langchain import PromptTemplate
7
  from langchain import LLMChain
8
  from langchain_together import Together
9
  import re
 
10
  # Set the API key with double quotes
11
  os.environ['TOGETHER_API_KEY'] = "d88cb7414e4039a84d2ed63f1b47daaaa4230c4c53a422045d8a30a9a3bc87d8"
12
 
@@ -24,17 +25,17 @@ def YtToQuizz(link, difficulty_level):
24
  transcript = yt.get_transcript(video_id)
25
  data = ""
26
  for text in transcript:
27
- data += text.get('text')
28
  summary = Summary_BART(data)
29
 
30
  mcq_template = """
31
- Give a 10 different multiple-choice question MCQ related to the summary: {summary}
32
- The difficulty level of the question should be: {difficulty_level}
33
- Please provide the following in:
34
  1. Question
35
  2. Correct answer
36
  3. Three plausible incorrect answer options
37
- 4. Proper MCQ format
38
  """
39
  prompt = PromptTemplate(
40
  input_variables=['summary', 'difficulty_level'],
@@ -50,22 +51,24 @@ def YtToQuizz(link, difficulty_level):
50
 
51
  response_text = response['text']
52
 
53
- # Extract MCQs, correct answers, and options
54
- #questions = re.findall(r'Question: (.*?)\n', response_text)
55
- #correct_answers = re.findall(r'Correct answer: (.*?)\n', response_text)
56
- #options = re.findall(r'Options: (.*?)\n', response_text)
57
- #all_options = [option.split(', ') for option in options]
 
58
 
59
- return response_text
 
 
 
 
 
 
60
 
61
  def main(link, difficulty_level):
62
- #questions, options, correct_answers = YtToQuizz(link, difficulty_level)
63
- # return {
64
- # "Questions": questions,
65
- # "Options": options,
66
- # "Correct Answers": correct_answers
67
- # }
68
  return YtToQuizz(link, difficulty_level)
 
69
  iface = gr.Interface(
70
  fn=main,
71
  inputs=[
@@ -73,7 +76,7 @@ iface = gr.Interface(
73
  gr.components.Dropdown(["Easy", "Medium", "Hard"], label="Select difficulty level:")
74
  ],
75
  outputs=[
76
- gr.components.Textbox(label="MCQs Output")
77
  ],
78
  title="YouTube Video Subtitle to MCQs Quiz",
79
  description="Generate MCQs from YouTube video subtitles"
 
7
  from langchain import LLMChain
8
  from langchain_together import Together
9
  import re
10
+
11
  # Set the API key with double quotes
12
  os.environ['TOGETHER_API_KEY'] = "d88cb7414e4039a84d2ed63f1b47daaaa4230c4c53a422045d8a30a9a3bc87d8"
13
 
 
25
  transcript = yt.get_transcript(video_id)
26
  data = ""
27
  for text in transcript:
28
+ data += text.get('text') + " "
29
  summary = Summary_BART(data)
30
 
31
  mcq_template = """
32
+ Generate 10 different multiple-choice questions (MCQs) related to the following summary: {summary}
33
+ The difficulty level of the questions should be: {difficulty_level}
34
+ Please provide the following for each question:
35
  1. Question
36
  2. Correct answer
37
  3. Three plausible incorrect answer options
38
+ 4. Format: "Question: <question text>\\nCorrect answer: <correct answer>\\nIncorrect answers: <option1>, <option2>, <option3>"
39
  """
40
  prompt = PromptTemplate(
41
  input_variables=['summary', 'difficulty_level'],
 
51
 
52
  response_text = response['text']
53
 
54
+ # Extract MCQs
55
+ mcq_pattern = r'Question: (.*?)\nCorrect answer: (.*?)\nIncorrect answers: (.*?)(?:\n|$)'
56
+ mcqs = re.findall(mcq_pattern, response_text, re.DOTALL)
57
+
58
+ if len(mcqs) < 10:
59
+ return "Failed to generate 10 complete MCQs. Please try again."
60
 
61
+ formatted_mcqs = []
62
+ for idx, mcq in enumerate(mcqs[:10]):
63
+ question, correct_answer, incorrect_answers = mcq
64
+ incorrect_answers = incorrect_answers.split(', ')
65
+ formatted_mcqs.append(f"Q{idx+1}: {question}\nA) {correct_answer}\nB) {incorrect_answers[0]}\nC) {incorrect_answers[1]}\nD) {incorrect_answers[2]}\n")
66
+
67
+ return "\n\n".join(formatted_mcqs)
68
 
69
  def main(link, difficulty_level):
 
 
 
 
 
 
70
  return YtToQuizz(link, difficulty_level)
71
+
72
  iface = gr.Interface(
73
  fn=main,
74
  inputs=[
 
76
  gr.components.Dropdown(["Easy", "Medium", "Hard"], label="Select difficulty level:")
77
  ],
78
  outputs=[
79
+ gr.components.Textbox(label="MCQs Output", lines=20)
80
  ],
81
  title="YouTube Video Subtitle to MCQs Quiz",
82
  description="Generate MCQs from YouTube video subtitles"