TahaRasouli commited on
Commit
77e240b
·
verified ·
1 Parent(s): 8f38bd2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +67 -53
app.py CHANGED
@@ -43,15 +43,19 @@ def analyze_task1_graph(image, question):
43
 
44
  return analysis, "✅ Image analyzed successfully"
45
 
 
 
 
 
46
  def evaluate_task1(image, question, response, graph_analysis):
47
  if image is None:
48
- return "Please upload an image", "", "", "", ""
49
  if not question.strip():
50
- return "Please enter the question", "", "", "", ""
51
  if not response.strip():
52
- return "Please write your response", "", "", "", ""
53
  if graph_analysis is None:
54
- return "Graph not analyzed yet", "", "", "", ""
55
 
56
  evaluator, _ = create_evaluators("Task 1")
57
 
@@ -61,13 +65,21 @@ def evaluate_task1(image, question, response, graph_analysis):
61
  task_achievement = evaluator.analyze_task_achievement(question, graph_analysis, response)
62
  cohesion = evaluator.analyze_cohesion_coherence(response, graph_analysis)
63
 
64
- return evaluation, grammar, vocabulary, task_achievement, cohesion
 
 
 
 
65
 
 
 
 
 
66
  def evaluate_task2(question, response):
67
  if not question.strip():
68
- return "Please enter the question", "", "", "", ""
69
  if not response.strip():
70
- return "Please write your response", "", "", "", ""
71
 
72
  evaluator, _ = create_evaluators("Task 2")
73
 
@@ -77,49 +89,47 @@ def evaluate_task2(question, response):
77
  task_achievement = evaluator.analyze_task_achievement(question, response)
78
  cohesion = evaluator.analyze_cohesion_coherence(response)
79
 
80
- return evaluation, grammar, vocabulary, task_achievement, cohesion
 
 
 
 
81
 
 
 
 
82
  def generate_exercises(
83
  task,
84
  grammar_t1, vocab_t1, resp_t1,
85
- grammar_t2, vocab_t2, resp_t2,
86
  ):
87
- # Decide which task's results to use
88
  if task == "Task 1":
89
- grammar_text = grammar_t1 or ""
90
- vocab_text = vocab_t1 or ""
91
- response = resp_t1 or ""
92
  else:
93
- grammar_text = grammar_t2 or ""
94
- vocab_text = vocab_t2 or ""
95
- response = resp_t2 or ""
96
-
97
- # Require evaluation first
98
- if not grammar_text.strip() or not vocab_text.strip() or not response.strip():
99
- return (
100
- "⚠️ Please run the evaluation for the selected task first, then click this button.",
101
- ""
102
- )
103
-
104
- # Get the right generator (Task1 or Task2)
105
  _, generator = create_evaluators(task)
106
 
107
  try:
108
- grammar_ex = generator.generate_grammar_exercises(grammar_text, response)
109
  vocab_ex = generator.generate_vocabulary_exercises(vocab_text)
110
  return grammar_ex, vocab_ex
111
  except Exception as e:
112
- return f"❌ Error while generating exercises:\n\n{e}", ""
113
 
114
 
115
  # -------------------------
116
- # BUILD INTERFACE
117
  # -------------------------
118
  with gr.Blocks() as demo:
119
 
120
- # -------------------------
121
- # LOGIN SCREEN
122
- # -------------------------
123
  login_box = gr.Group(visible=True)
124
  with login_box:
125
  gr.Markdown("## 🔐 Login")
@@ -128,21 +138,18 @@ with gr.Blocks() as demo:
128
  login_btn = gr.Button("Login")
129
  login_msg = gr.Markdown("")
130
 
131
- # -------------------------
132
- # MAIN APP
133
- # -------------------------
134
  main = gr.Group(visible=False)
135
  with main:
136
  gr.Markdown("# 📝 IELTS Writing Evaluator")
137
 
138
- task_selector = gr.Radio(["Task 1", "Task 2"], label="Choose Task Type", value="Task 1")
139
 
140
  # -------------------------
141
  # TASK 1 UI
142
  # -------------------------
143
  with gr.Tab("Task 1"):
144
  image = gr.Image(type="pil", label="Upload Graph/Chart")
145
- t1_question = gr.Textbox(label="Task 1 Question", lines=3)
146
  analyze_btn = gr.Button("Analyze Graph")
147
  graph_output = gr.Markdown("")
148
  graph_state = gr.State()
@@ -156,11 +163,15 @@ with gr.Blocks() as demo:
156
  task_ach1 = gr.Markdown()
157
  cohesion1 = gr.Markdown()
158
 
 
 
 
 
159
  # -------------------------
160
  # TASK 2 UI
161
  # -------------------------
162
  with gr.Tab("Task 2"):
163
- t2_question = gr.Textbox(label="Task 2 Question", lines=3)
164
  response2 = gr.Textbox(label="Your Response", lines=10)
165
  eval_btn2 = gr.Button("Evaluate Essay")
166
 
@@ -170,22 +181,23 @@ with gr.Blocks() as demo:
170
  task_ach2 = gr.Markdown()
171
  cohesion2 = gr.Markdown()
172
 
 
 
 
 
173
  # -------------------------
174
- # EXERCISE GENERATION
175
  # -------------------------
176
- gr.Markdown("---\n## 📚 Practice Exercises")
177
  exercise_btn = gr.Button("Generate Exercises")
178
  exercises_grammar = gr.Markdown()
179
  exercises_vocab = gr.Markdown()
180
 
 
181
  # -------------------------
182
- # BIND FUNCTIONS
183
  # -------------------------
184
- login_btn.click(
185
- login,
186
- [user, pwd],
187
- [login_box, main, login_msg]
188
- )
189
 
190
  analyze_btn.click(
191
  analyze_task1_graph,
@@ -196,27 +208,29 @@ with gr.Blocks() as demo:
196
  eval_btn1.click(
197
  evaluate_task1,
198
  [image, t1_question, response1, graph_state],
199
- [evaluation1, grammar1, vocabulary1, task_ach1, cohesion1]
 
 
 
200
  )
201
 
202
  eval_btn2.click(
203
  evaluate_task2,
204
  [t2_question, response2],
205
- [evaluation2, grammar2, vocabulary2, task_ach2, cohesion2]
 
 
 
206
  )
207
 
208
  exercise_btn.click(
209
  generate_exercises,
210
  [
211
  task_selector,
212
- grammar1, vocabulary1, response1, # Task 1 outputs
213
- grammar2, vocabulary2, response2 # Task 2 outputs
214
  ],
215
  [exercises_grammar, exercises_vocab]
216
  )
217
 
218
-
219
- # -------------------------
220
- # RUN
221
- # -------------------------
222
  demo.launch()
 
43
 
44
  return analysis, "✅ Image analyzed successfully"
45
 
46
+
47
+ # -------------------------
48
+ # TASK 1 EVALUATION
49
+ # -------------------------
50
  def evaluate_task1(image, question, response, graph_analysis):
51
  if image is None:
52
+ return "Please upload an image", "", "", "", "", "", ""
53
  if not question.strip():
54
+ return "Please enter the question", "", "", "", "", "", ""
55
  if not response.strip():
56
+ return "Please write your response", "", "", "", "", "", ""
57
  if graph_analysis is None:
58
+ return "Graph not analyzed yet", "", "", "", "", "", ""
59
 
60
  evaluator, _ = create_evaluators("Task 1")
61
 
 
65
  task_achievement = evaluator.analyze_task_achievement(question, graph_analysis, response)
66
  cohesion = evaluator.analyze_cohesion_coherence(response, graph_analysis)
67
 
68
+ # Return display + states
69
+ return (
70
+ evaluation, grammar, vocabulary, task_achievement, cohesion,
71
+ grammar, vocabulary, response
72
+ )
73
 
74
+
75
+ # -------------------------
76
+ # TASK 2 EVALUATION
77
+ # -------------------------
78
  def evaluate_task2(question, response):
79
  if not question.strip():
80
+ return "Please enter the question", "", "", "", "", "", ""
81
  if not response.strip():
82
+ return "Please write your response", "", "", "", "", "", ""
83
 
84
  evaluator, _ = create_evaluators("Task 2")
85
 
 
89
  task_achievement = evaluator.analyze_task_achievement(question, response)
90
  cohesion = evaluator.analyze_cohesion_coherence(response)
91
 
92
+ return (
93
+ evaluation, grammar, vocabulary, task_achievement, cohesion,
94
+ grammar, vocabulary, response
95
+ )
96
+
97
 
98
+ # -------------------------
99
+ # GENERATE EXERCISES (working version)
100
+ # -------------------------
101
  def generate_exercises(
102
  task,
103
  grammar_t1, vocab_t1, resp_t1,
104
+ grammar_t2, vocab_t2, resp_t2
105
  ):
 
106
  if task == "Task 1":
107
+ grammar_text = grammar_t1
108
+ vocab_text = vocab_t1
109
+ response_text = resp_t1
110
  else:
111
+ grammar_text = grammar_t2
112
+ vocab_text = vocab_t2
113
+ response_text = resp_t2
114
+
115
+ if not grammar_text or not vocab_text or not response_text:
116
+ return "⚠️ Please run evaluation first.", ""
117
+
 
 
 
 
 
118
  _, generator = create_evaluators(task)
119
 
120
  try:
121
+ grammar_ex = generator.generate_grammar_exercises(grammar_text, response_text)
122
  vocab_ex = generator.generate_vocabulary_exercises(vocab_text)
123
  return grammar_ex, vocab_ex
124
  except Exception as e:
125
+ return f"❌ Error generating exercises:\n{e}", ""
126
 
127
 
128
  # -------------------------
129
+ # BUILD UI
130
  # -------------------------
131
  with gr.Blocks() as demo:
132
 
 
 
 
133
  login_box = gr.Group(visible=True)
134
  with login_box:
135
  gr.Markdown("## 🔐 Login")
 
138
  login_btn = gr.Button("Login")
139
  login_msg = gr.Markdown("")
140
 
 
 
 
141
  main = gr.Group(visible=False)
142
  with main:
143
  gr.Markdown("# 📝 IELTS Writing Evaluator")
144
 
145
+ task_selector = gr.Radio(["Task 1", "Task 2"], value="Task 1")
146
 
147
  # -------------------------
148
  # TASK 1 UI
149
  # -------------------------
150
  with gr.Tab("Task 1"):
151
  image = gr.Image(type="pil", label="Upload Graph/Chart")
152
+ t1_question = gr.Textbox(label="Task 1 Question")
153
  analyze_btn = gr.Button("Analyze Graph")
154
  graph_output = gr.Markdown("")
155
  graph_state = gr.State()
 
163
  task_ach1 = gr.Markdown()
164
  cohesion1 = gr.Markdown()
165
 
166
+ grammar1_state = gr.State("")
167
+ vocabulary1_state = gr.State("")
168
+ response1_state = gr.State("")
169
+
170
  # -------------------------
171
  # TASK 2 UI
172
  # -------------------------
173
  with gr.Tab("Task 2"):
174
+ t2_question = gr.Textbox(label="Task 2 Question")
175
  response2 = gr.Textbox(label="Your Response", lines=10)
176
  eval_btn2 = gr.Button("Evaluate Essay")
177
 
 
181
  task_ach2 = gr.Markdown()
182
  cohesion2 = gr.Markdown()
183
 
184
+ grammar2_state = gr.State("")
185
+ vocabulary2_state = gr.State("")
186
+ response2_state = gr.State("")
187
+
188
  # -------------------------
189
+ # EXERCISE SECTION
190
  # -------------------------
191
+ gr.Markdown("## 📚 Practice Exercises")
192
  exercise_btn = gr.Button("Generate Exercises")
193
  exercises_grammar = gr.Markdown()
194
  exercises_vocab = gr.Markdown()
195
 
196
+
197
  # -------------------------
198
+ # BINDINGS
199
  # -------------------------
200
+ login_btn.click(login, [user, pwd], [login_box, main, login_msg])
 
 
 
 
201
 
202
  analyze_btn.click(
203
  analyze_task1_graph,
 
208
  eval_btn1.click(
209
  evaluate_task1,
210
  [image, t1_question, response1, graph_state],
211
+ [
212
+ evaluation1, grammar1, vocabulary1, task_ach1, cohesion1,
213
+ grammar1_state, vocabulary1_state, response1_state
214
+ ]
215
  )
216
 
217
  eval_btn2.click(
218
  evaluate_task2,
219
  [t2_question, response2],
220
+ [
221
+ evaluation2, grammar2, vocabulary2, task_ach2, cohesion2,
222
+ grammar2_state, vocabulary2_state, response2_state
223
+ ]
224
  )
225
 
226
  exercise_btn.click(
227
  generate_exercises,
228
  [
229
  task_selector,
230
+ grammar1_state, vocabulary1_state, response1_state,
231
+ grammar2_state, vocabulary2_state, response2_state
232
  ],
233
  [exercises_grammar, exercises_vocab]
234
  )
235
 
 
 
 
 
236
  demo.launch()