Turbiling commited on
Commit
1ff7dbe
·
verified ·
1 Parent(s): 2ebffae

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -57
app.py CHANGED
@@ -5,7 +5,6 @@ from datetime import datetime
5
  from docx import Document
6
  from docx.shared import Pt, Inches
7
  from docx.enum.text import WD_ALIGN_PARAGRAPH
8
- from docx.oxml.ns import qn
9
 
10
  # -------------------------
11
  # Initialize GROQ client
@@ -21,88 +20,59 @@ min_mcq, max_mcq = 5, 50
21
  # Chapters by grade & subject (use your full dictionary here)
22
  # -------------------------
23
  chapters_by_subject_and_grade = {
24
- # Paste full dictionary from your previous code (Grades 1-12)
25
  }
26
 
27
  # -------------------------
28
- # Generate MCQs from Groq LLaMA-3
29
  # -------------------------
30
  def generate_mcqs(grade, subject, chapter, difficulty, num_questions):
31
- prompt = f"""
32
- Generate {num_questions} multiple-choice questions for {subject}, Grade {grade}, Chapter '{chapter}'.
33
- Level: {difficulty}.
34
- Each question should have 4 options (A, B, C, D) and indicate the correct answer.
35
- Return the output in JSON format like:
36
- [
37
- {{"question": "Question text", "options": ["A text", "B text", "C text", "D text"], "answer": "A"}}
38
- ]
39
- """
40
- response = client.generate(
41
- model="llama3",
42
- prompt=prompt,
43
- max_tokens=1500
44
- )
45
-
46
- try:
47
- import json
48
- mcq_list = json.loads(response.text)
49
- except:
50
- mcq_list = [{"question": f"Sample question {i} for {subject} chapter {chapter}",
51
- "options": ["Option A","Option B","Option C","Option D"],
52
- "answer": "A"} for i in range(1, num_questions+1)]
53
-
54
- questions = [q["question"] for q in mcq_list]
55
- options_list = [q["options"] for q in mcq_list]
56
- answers = [q["answer"] for q in mcq_list]
57
-
58
- return questions, options_list, answers
59
 
60
  # -------------------------
61
  # Export to Word
62
  # -------------------------
63
  def export_to_word(mcqs, options_list, answers, grade, subject, chapter, difficulty):
64
  doc = Document()
65
-
66
- # Set document default font (Times New Roman)
67
  style = doc.styles['Normal']
68
  font = style.font
69
  font.name = 'Times New Roman'
70
  font.size = Pt(12)
71
 
72
- # Heading
73
- heading = doc.add_paragraph("MONTHLY/ WEEKLY TEST")
74
  heading.alignment = WD_ALIGN_PARAGRAPH.CENTER
75
  heading.runs[0].bold = True
76
  heading.runs[0].font.size = Pt(16)
77
 
78
- doc.add_paragraph() # empty line
79
 
80
- # Info Table
81
  table = doc.add_table(rows=6, cols=2)
82
  table.style = 'Table Grid'
83
  info = [
84
- ("Grade", grade),("Subject", subject),("Chapter", chapter),("Difficulty", difficulty),
85
- ("Number of Questions", str(len(mcqs))),("Date", datetime.now().strftime("%d-%m-%Y"))
 
 
 
 
86
  ]
87
  for row, (label, value) in zip(table.rows, info):
88
  row.cells[0].text = label
89
  row.cells[1].text = value
90
  row.cells[0].paragraphs[0].runs[0].bold = True
91
 
92
- doc.add_paragraph() # empty line
93
 
94
- # MCQs
95
  for i, (q, opts) in enumerate(zip(mcqs, options_list), start=1):
96
- q_para = doc.add_paragraph(f"{i}. {q}")
97
- q_para.paragraph_format.space_after = Pt(2)
98
  for letter, text in zip(["A","B","C","D"], opts):
99
- opt_para = doc.add_paragraph(f"{letter}. {text}")
100
- opt_para.paragraph_format.left_indent = Inches(0.25)
101
- opt_para.paragraph_format.space_after = Pt(1)
102
-
103
- doc.add_paragraph() # empty line
104
 
105
- # Answer Key Table
106
  doc.add_paragraph("ANSWER KEY")
107
  ans_table = doc.add_table(rows=len(answers)+1, cols=5)
108
  ans_table.style = 'Table Grid'
@@ -146,16 +116,22 @@ with gr.Blocks() as demo:
146
  output_text = gr.Textbox(label="Status")
147
  output_file = gr.File(label="Download Test")
148
 
149
- # Dropdown updates
150
- def update_subjects(grade):
151
- return gr.update(choices=list(chapters_by_subject_and_grade.get(grade, {}).keys()), value=None)
 
 
 
152
 
153
- def update_chapters(subject, grade):
154
- return gr.update(choices=chapters_by_subject_and_grade.get(grade, {}).get(subject, []), value=None)
 
155
 
156
- grade_dropdown.change(update_subjects, grade_dropdown, subject_dropdown)
157
- subject_dropdown.change(update_chapters, [subject_dropdown, grade_dropdown], chapter_dropdown)
158
 
159
- generate_btn.click(on_generate, inputs=[grade_dropdown, subject_dropdown, chapter_dropdown, difficulty_dropdown, num_mcq_slider], outputs=[output_text, output_file])
 
 
160
 
161
  demo.launch()
 
5
  from docx import Document
6
  from docx.shared import Pt, Inches
7
  from docx.enum.text import WD_ALIGN_PARAGRAPH
 
8
 
9
  # -------------------------
10
  # Initialize GROQ client
 
20
  # Chapters by grade & subject (use your full dictionary here)
21
  # -------------------------
22
  chapters_by_subject_and_grade = {
23
+ # Paste the full dictionary from your previous code (Grades 1-12 with all subjects & chapters)
24
  }
25
 
26
  # -------------------------
27
+ # Generate MCQs (dummy placeholder or Groq call)
28
  # -------------------------
29
  def generate_mcqs(grade, subject, chapter, difficulty, num_questions):
30
+ mcqs = [f"Sample question {i} for {subject} chapter {chapter}" for i in range(1, num_questions+1)]
31
+ options_list = [["Option A", "Option B", "Option C", "Option D"] for _ in range(num_questions)]
32
+ answers = ["A" for _ in range(num_questions)]
33
+ return mcqs, options_list, answers
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
 
35
  # -------------------------
36
  # Export to Word
37
  # -------------------------
38
  def export_to_word(mcqs, options_list, answers, grade, subject, chapter, difficulty):
39
  doc = Document()
 
 
40
  style = doc.styles['Normal']
41
  font = style.font
42
  font.name = 'Times New Roman'
43
  font.size = Pt(12)
44
 
45
+ heading = doc.add_paragraph("MONTHLY/WEEKLY TEST")
 
46
  heading.alignment = WD_ALIGN_PARAGRAPH.CENTER
47
  heading.runs[0].bold = True
48
  heading.runs[0].font.size = Pt(16)
49
 
50
+ doc.add_paragraph()
51
 
 
52
  table = doc.add_table(rows=6, cols=2)
53
  table.style = 'Table Grid'
54
  info = [
55
+ ("Grade", grade),
56
+ ("Subject", subject),
57
+ ("Chapter", chapter),
58
+ ("Difficulty", difficulty),
59
+ ("Number of Questions", str(len(mcqs))),
60
+ ("Date", datetime.now().strftime("%d-%m-%Y"))
61
  ]
62
  for row, (label, value) in zip(table.rows, info):
63
  row.cells[0].text = label
64
  row.cells[1].text = value
65
  row.cells[0].paragraphs[0].runs[0].bold = True
66
 
67
+ doc.add_paragraph()
68
 
 
69
  for i, (q, opts) in enumerate(zip(mcqs, options_list), start=1):
70
+ doc.add_paragraph(f"{i}. {q}")
 
71
  for letter, text in zip(["A","B","C","D"], opts):
72
+ p = doc.add_paragraph(f"{letter}. {text}")
73
+ p.paragraph_format.left_indent = Inches(0.25)
 
 
 
74
 
75
+ doc.add_paragraph()
76
  doc.add_paragraph("ANSWER KEY")
77
  ans_table = doc.add_table(rows=len(answers)+1, cols=5)
78
  ans_table.style = 'Table Grid'
 
116
  output_text = gr.Textbox(label="Status")
117
  output_file = gr.File(label="Download Test")
118
 
119
+ # -------------------------
120
+ # Dynamic dropdown updates
121
+ # -------------------------
122
+ def update_subjects(selected_grade):
123
+ subjects = list(chapters_by_subject_and_grade.get(selected_grade, {}).keys())
124
+ return gr.update(choices=subjects, value=subjects[0] if subjects else None)
125
 
126
+ def update_chapters(selected_subject, selected_grade):
127
+ chapters = chapters_by_subject_and_grade.get(selected_grade, {}).get(selected_subject, [])
128
+ return gr.update(choices=chapters, value=chapters[0] if chapters else None)
129
 
130
+ grade_dropdown.change(update_subjects, inputs=[grade_dropdown], outputs=[subject_dropdown])
131
+ subject_dropdown.change(update_chapters, inputs=[subject_dropdown, grade_dropdown], outputs=[chapter_dropdown])
132
 
133
+ generate_btn.click(on_generate,
134
+ inputs=[grade_dropdown, subject_dropdown, chapter_dropdown, difficulty_dropdown, num_mcq_slider],
135
+ outputs=[output_text, output_file])
136
 
137
  demo.launch()