Rehman1603 commited on
Commit
3d571e7
·
verified ·
1 Parent(s): c945221

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +102 -0
app.py CHANGED
@@ -0,0 +1,102 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import re
2
+ from docx import Document
3
+ import os
4
+ import gradio as gr
5
+ from PyPDF2 import PdfReader
6
+ from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
7
+ from langchain import PromptTemplate
8
+ from langchain import LLMChain
9
+ from langchain_together import Together
10
+
11
+ # Initialize Together API key
12
+ os.environ['TOGETHER_API_KEY'] = "c2f52626b97118b71c0c36f66eda4f5957c8fc475e760c3d72f98ba07d3ed3b5"
13
+ checkpoint = "sshleifer/distilbart-cnn-12-6"
14
+ llama3 = Together(model="meta-llama/Llama-3-70b-chat-hf", max_tokens=2500)
15
+ tokenizer = AutoTokenizer.from_pretrained(checkpoint)
16
+ model = AutoModelForSeq2SeqLM.from_pretrained(checkpoint)
17
+
18
+ def Summary_BART(text):
19
+ inputs = tokenizer(text, max_length=1024, truncation=True, return_tensors="pt")
20
+ summary_ids = model.generate(inputs["input_ids"])
21
+ summary = tokenizer.batch_decode(summary_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)
22
+ return summary[0]
23
+
24
+ def DocToQuizz(file, difficulty_level):
25
+ # Read the PDF content
26
+ reader = PdfReader(file.name) # Use `file.name` to access the uploaded file path
27
+ text = ""
28
+ for page in reader.pages:
29
+ text += page.extract_text()
30
+ summary = Summary_BART(text)
31
+
32
+ # Define the prompt template for generating questions
33
+ mcq_template = """
34
+ Generate 20 different multiple-choice questions (MCQs) based on the following summary: {summary}
35
+ The difficulty level of the questions should be: {difficulty_level}
36
+ For each MCQ, please provide the following:
37
+ 1. Question
38
+ - Use varied question formats such as:
39
+ - "How does...", "Why is...", "In what way...", "Which of the following...", "When does...", etc.
40
+ - Ensure questions are logically phrased and relevant to the content.
41
+ 2. Correct answer
42
+ 3. Three plausible incorrect answer options
43
+ 4. Format: "Question: <question text>\nCorrect answer: <correct answer>\nIncorrect answers: <option1>, <option2>, <option3>"
44
+ """
45
+ prompt = PromptTemplate(
46
+ input_variables=['summary', 'difficulty_level'],
47
+ template=mcq_template
48
+ )
49
+
50
+ Generated_mcqs = LLMChain(llm=llama3, prompt=prompt)
51
+
52
+ response = Generated_mcqs.invoke({
53
+ "summary": summary,
54
+ "difficulty_level": difficulty_level
55
+ })
56
+
57
+ response_text = response['text']
58
+ print(response_text)
59
+
60
+ # Updated MCQ pattern to match the format in the response
61
+ mcq_pattern = r'Question:\s*(.*?)\nCorrect answer:\s*(.*?)\nIncorrect answers:\s*(.*?)\n'
62
+ mcqs = re.findall(mcq_pattern, response_text, re.DOTALL)
63
+ print(f"the mcqs from pattern{mcqs}")
64
+
65
+ # Initialize a Word document
66
+ doc = Document()
67
+ doc.add_heading("Physics Questions", level=1)
68
+
69
+ # Add a section for MCQs with options
70
+ doc.add_heading("Multiple Choice Questions (MCQs)", level=2)
71
+ for idx, (question, correct_answer, incorrect_answers) in enumerate(mcqs, start=1):
72
+ # Split incorrect answers
73
+ incorrect_answers = incorrect_answers.split(', ')
74
+
75
+ # Add question and options to the document
76
+ doc.add_paragraph(f"Q{idx}: {question.strip()}", style="List Number")
77
+ doc.add_paragraph(f"A) {correct_answer.strip()}", style="List Bullet")
78
+ for i, incorrect in enumerate(incorrect_answers, start=2):
79
+ doc.add_paragraph(f"{chr(64 + i)}) {incorrect.strip()}", style="List Bullet")
80
+
81
+ # Save the document
82
+ doc.save("mcqs_Questions.docx")
83
+ return "mcqs_Questions.docx"
84
+
85
+ # Gradio Interface
86
+ def generate_quiz(file, difficulty_level):
87
+ output_file = DocToQuizz(file, difficulty_level)
88
+ return output_file
89
+
90
+ interface = gr.Interface(
91
+ fn=generate_quiz,
92
+ inputs=[
93
+ gr.File(file_types=[".pdf"], label="Upload PDF File"), # Allow file upload
94
+ gr.Dropdown(["Easy", "Medium", "Hard"], label="Select Difficulty Level")
95
+ ],
96
+ outputs=gr.File(label="Download Quiz Document"),
97
+ title="Quiz Generator",
98
+ description="Upload a PDF file and select a difficulty level to generate quiz questions."
99
+ )
100
+
101
+ # Launch the interface
102
+ interface.launch(debug=True)