KubraBashir commited on
Commit
74ced92
·
verified ·
1 Parent(s): 8d382ae

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +159 -0
app.py ADDED
@@ -0,0 +1,159 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import os
3
+ import fitz # PyMuPDF for PDF extraction
4
+ from pptx import Presentation
5
+ from docx import Document # For Word files
6
+ from groq import Groq # Assuming Groq is available as a Python library
7
+
8
+ # Initialize Groq Client
9
+
10
+ api_key=os.environ['GROQ_API_KEY']
11
+ client = Groq(api_key=os.environ['GROQ_API_KEY'])
12
+
13
+ # File Extraction Functions
14
+ def extract_text_from_pdf(file_path):
15
+ pdf_text = ""
16
+ try:
17
+ pdf_file = fitz.open(file_path)
18
+ for page_num in range(pdf_file.page_count):
19
+ page = pdf_file.load_page(page_num)
20
+ pdf_text += page.get_text()
21
+ except Exception as e:
22
+ return f"Error reading PDF: {e}"
23
+ return pdf_text
24
+
25
+ def extract_text_from_ppt(file_path):
26
+ ppt_text = ""
27
+ try:
28
+ presentation = Presentation(file_path)
29
+ for slide in presentation.slides:
30
+ for shape in slide.shapes:
31
+ if hasattr(shape, 'text'):
32
+ ppt_text += shape.text + "\n"
33
+ except Exception as e:
34
+ return f"Error reading PPT: {e}"
35
+ return ppt_text
36
+
37
+ def extract_text_from_word(file_path):
38
+ doc_text = ""
39
+ try:
40
+ document = Document(file_path)
41
+ for paragraph in document.paragraphs:
42
+ doc_text += paragraph.text + "\n"
43
+ except Exception as e:
44
+ return f"Error reading Word file: {e}"
45
+ return doc_text
46
+
47
+ def process_files(file_paths):
48
+ text_data = ""
49
+ for file_path in file_paths:
50
+ if file_path.endswith(".pdf"):
51
+ text_data += extract_text_from_pdf(file_path)
52
+ elif file_path.endswith(".pptx"):
53
+ text_data += extract_text_from_ppt(file_path)
54
+ elif file_path.endswith(".docx"):
55
+ text_data += extract_text_from_word(file_path)
56
+ else:
57
+ text_data += f"Unsupported file format: {file_path}\n"
58
+ return text_data
59
+
60
+ # Generate MCQs and Subjective Questions Using Groq
61
+ def generate_questions(text, num_mcqs=5, num_subjective=2, difficulty_mcqs="medium", difficulty_subjective="medium", question_type="mix"):
62
+ try:
63
+ num_mcqs = min(num_mcqs, 60) # Limit MCQs to 40
64
+ num_subjective = min(num_subjective, 20) # Limit Subjective Questions to 20
65
+
66
+ difficulty_levels = {
67
+ "easy": "simple questions with direct answers.",
68
+ "medium": "moderate complexity questions requiring reasoning.",
69
+ "hard": "challenging questions requiring deep understanding."
70
+ }
71
+
72
+ question_type_map = {
73
+ "reason": "Generate reasoning-based questions.",
74
+ "short": "Generate short-answer questions.",
75
+ "long": "Generate long-answer questions.",
76
+ "case study": "Generate case study-based questions.",
77
+ "mix": "Generate a mix of question types."
78
+ }
79
+
80
+ prompt = f"Generate {num_mcqs} multiple choice questions and {num_subjective} subjective questions from the following text: {text}. Include the correct answers for each question. The questions should be {difficulty_levels.get(difficulty_mcqs, 'medium')} for MCQs and {difficulty_levels.get(difficulty_subjective, 'medium')} for Subjective questions. {question_type_map.get(question_type, 'mix')}"
81
+
82
+ chat_completion = client.chat.completions.create(
83
+ messages=[{"role": "user", "content": prompt}],
84
+ model="llama3-8b-8192",
85
+ )
86
+
87
+ response = chat_completion.choices[0].message.content.strip()
88
+
89
+ # Split response into MCQs and Subjective questions
90
+ mcqs, subjective = "", ""
91
+ is_subjective_section = False
92
+
93
+ for line in response.split("\n"):
94
+ if "**Subjective Questions**" in line:
95
+ is_subjective_section = True
96
+ if is_subjective_section:
97
+ subjective += line + "\n"
98
+ else:
99
+ mcqs += line + "\n"
100
+
101
+ return mcqs, subjective
102
+
103
+ except Exception as e:
104
+ return f"Error generating questions: {e}", ""
105
+
106
+ # Gradio Interface Function
107
+ def process_and_generate(file_paths, raw_text, num_mcqs, num_subjective, difficulty_mcqs, difficulty_subjective, question_type):
108
+ combined_text = "" # Initialize the combined text variable
109
+
110
+ # Extract text from uploaded files
111
+ if file_paths:
112
+ extracted_text = process_files(file_paths)
113
+ if extracted_text.strip(): # Ensure extracted text is non-empty
114
+ combined_text += extracted_text
115
+
116
+ # Add raw text if provided
117
+ if raw_text.strip(): # Check if raw text is non-empty
118
+ if combined_text: # If text from files exists, concatenate with raw text
119
+ combined_text += "\n" + raw_text
120
+ else: # If no text from files, use raw text as the only input
121
+ combined_text = raw_text
122
+
123
+ # Check if there is any text to process
124
+ if not combined_text.strip():
125
+ return "No text provided to generate questions.", "No text provided to generate questions."
126
+
127
+ # Generate questions from the combined text
128
+ try:
129
+ mcqs, subjective = generate_questions(
130
+ combined_text, num_mcqs, num_subjective, difficulty_mcqs, difficulty_subjective, question_type
131
+ )
132
+ return mcqs, subjective
133
+ except Exception as e:
134
+ return f"Error generating questions: {e}", f"Error generating questions: {e}"
135
+
136
+ # Gradio Inputs and Outputs
137
+ inputs = [
138
+ gr.File(file_count="multiple", type="filepath", label="Upload Files (.pdf, .pptx, .docx)"),
139
+ gr.Textbox(lines=3, placeholder="Enter raw text here (Optional)...", label="Raw Text"),
140
+ gr.Slider(minimum=2, maximum=60, value=5, step=1, label="Number of MCQs (Max 60)"),
141
+ gr.Slider(minimum=2, maximum=20, value=2, step=1, label="Number of Subjective Questions (Max 20)"),
142
+ gr.Radio(["easy", "medium", "hard"], label="Select Difficulty Level for MCQs", value="medium"),
143
+ gr.Radio(["easy", "medium", "hard"], label="Select Difficulty Level for Subjective Questions", value="medium"),
144
+ gr.Radio(["reason", "short", "long", "case study", "mix"], label="Select Type of Question", value="mix")
145
+ ]
146
+
147
+ outputs = [
148
+ gr.Textbox(label="Generated MCQs", lines=10),
149
+ gr.Textbox(label="Generated Subjective Questions", lines=10)
150
+ ]
151
+
152
+ # Gradio Interface
153
+ gr.Interface(
154
+ fn=process_and_generate,
155
+ inputs=inputs,
156
+ outputs=outputs,
157
+ title="MCQ & Subjective Question Generator",
158
+ live=False
159
+ ).launch(share=True)