ayushsaun commited on
Commit
4c2a3dd
·
1 Parent(s): 2793dd0

added file upload feature

Browse files
Files changed (1) hide show
  1. app.py +87 -27
app.py CHANGED
@@ -1,33 +1,95 @@
1
  import gradio as gr
2
- from main import grade_submission, MODEL_MAP
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
 
4
  with gr.Blocks() as demo:
5
- gr.Markdown("## AutoGrader (CPU-only, Rubric-Aware, Flexible)")
6
 
7
  model = gr.Dropdown(
8
- choices=list(MODEL_MAP.keys()),
9
  value="Phi-3-mini",
10
- label="Select Model"
11
  )
12
 
13
- question_paper = gr.Textbox(
14
- label="Question Paper",
15
- lines=5
16
- )
 
 
 
 
 
17
 
18
- rubric = gr.Textbox(
19
- label="Rubric",
20
- lines=5
21
- )
 
 
 
 
 
22
 
23
- grading_instruction = gr.Textbox(
24
- label="Grading Instruction (e.g. 'Grade only Q2 out of 20')",
25
- lines=2
26
- )
 
 
 
 
 
27
 
28
- student_answer = gr.Textbox(
29
- label="Student Submission",
30
- lines=6
31
  )
32
 
33
  output = gr.Textbox(
@@ -35,16 +97,14 @@ with gr.Blocks() as demo:
35
  lines=12
36
  )
37
 
38
- grade = gr.Button("Grade")
39
-
40
- grade.click(
41
- fn=grade_submission,
42
  inputs=[
43
  model,
44
- question_paper,
45
- rubric,
46
- student_answer,
47
- grading_instruction
48
  ],
49
  outputs=output
50
  )
 
1
  import gradio as gr
2
+ from main import grade_submission
3
+ import pdfplumber
4
+ import docx
5
+
6
+
7
+ def extract_text(file):
8
+ if file is None:
9
+ return ""
10
+
11
+ name = file.name.lower()
12
+
13
+ if name.endswith(".pdf"):
14
+ text = ""
15
+ with pdfplumber.open(file) as pdf:
16
+ for page in pdf.pages:
17
+ text += page.extract_text() + "\n"
18
+ return text
19
+
20
+ if name.endswith(".docx"):
21
+ doc = docx.Document(file)
22
+ return "\n".join(p.text for p in doc.paragraphs)
23
+
24
+ return file.read().decode("utf-8", errors="ignore")
25
+
26
+
27
+ def resolve_input(file, text):
28
+ return extract_text(file) if file else text
29
+
30
+
31
+ def grade_ui(
32
+ model,
33
+ q_file, q_text,
34
+ r_file, r_text,
35
+ s_file, s_text,
36
+ instruction
37
+ ):
38
+ question_paper = resolve_input(q_file, q_text)
39
+ rubric = resolve_input(r_file, r_text)
40
+ student_answer = resolve_input(s_file, s_text)
41
+
42
+ return grade_submission(
43
+ model,
44
+ question_paper,
45
+ rubric,
46
+ student_answer,
47
+ instruction
48
+ )
49
+
50
 
51
  with gr.Blocks() as demo:
52
+ gr.Markdown("## AutoGrader")
53
 
54
  model = gr.Dropdown(
55
+ ["Phi-3-mini", "Mistral-7B-Instruct"],
56
  value="Phi-3-mini",
57
+ label="Model"
58
  )
59
 
60
+ with gr.Tab("Question Paper"):
61
+ q_file = gr.File(
62
+ label="Upload Question Paper (PDF/DOCX/TXT)",
63
+ file_types=[".pdf", ".docx", ".txt"]
64
+ )
65
+ q_text = gr.Textbox(
66
+ label="Or paste Question Paper",
67
+ lines=5
68
+ )
69
 
70
+ with gr.Tab("Rubric"):
71
+ r_file = gr.File(
72
+ label="Upload Rubric (PDF/DOCX/TXT)",
73
+ file_types=[".pdf", ".docx", ".txt"]
74
+ )
75
+ r_text = gr.Textbox(
76
+ label="Or paste Rubric",
77
+ lines=5
78
+ )
79
 
80
+ with gr.Tab("Student Submission"):
81
+ s_file = gr.File(
82
+ label="Upload Student Submission (PDF/DOCX/TXT)",
83
+ file_types=[".pdf", ".docx", ".txt"]
84
+ )
85
+ s_text = gr.Textbox(
86
+ label="Or paste Student Submission",
87
+ lines=6
88
+ )
89
 
90
+ instruction = gr.Textbox(
91
+ label="Grading Instruction",
92
+ placeholder="e.g. Grade only Q2 out of 20"
93
  )
94
 
95
  output = gr.Textbox(
 
97
  lines=12
98
  )
99
 
100
+ gr.Button("Grade").click(
101
+ grade_ui,
 
 
102
  inputs=[
103
  model,
104
+ q_file, q_text,
105
+ r_file, r_text,
106
+ s_file, s_text,
107
+ instruction
108
  ],
109
  outputs=output
110
  )