File size: 18,399 Bytes
2fa8377
 
 
 
 
 
 
 
 
 
8fa7be2
2fa8377
 
 
8fa7be2
2fa8377
 
8fa7be2
2fa8377
 
 
 
 
 
 
 
 
 
 
 
 
 
8fa7be2
2fa8377
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8fa7be2
2fa8377
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8fa7be2
2fa8377
 
8fa7be2
2fa8377
 
 
 
 
 
 
8fa7be2
2fa8377
 
 
 
 
 
8fa7be2
2fa8377
 
 
 
 
 
 
8fa7be2
2fa8377
 
 
 
 
 
 
 
 
 
 
 
 
8fa7be2
2fa8377
 
 
 
 
 
 
8fa7be2
2fa8377
 
 
 
 
 
 
 
 
 
8fa7be2
2fa8377
 
 
 
 
 
 
 
 
 
 
 
8fa7be2
2fa8377
 
 
8fa7be2
2fa8377
 
 
 
 
 
 
8fa7be2
2fa8377
 
 
 
 
 
8fa7be2
2fa8377
 
 
 
 
 
 
8fa7be2
2fa8377
8fa7be2
2fa8377
 
 
 
8fa7be2
2fa8377
8fa7be2
2fa8377
 
 
 
 
 
 
8fa7be2
2fa8377
 
 
8fa7be2
2fa8377
 
 
8fa7be2
2fa8377
 
 
8fa7be2
2fa8377
 
 
8fa7be2
2fa8377
 
 
 
 
 
 
 
 
 
 
 
8fa7be2
2fa8377
 
8fa7be2
2fa8377
 
 
 
 
 
 
 
 
8fa7be2
2fa8377
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8fa7be2
2fa8377
 
 
 
 
 
 
 
8fa7be2
2fa8377
 
 
8fa7be2
2fa8377
 
 
8fa7be2
2fa8377
 
8fa7be2
2fa8377
 
 
 
 
 
 
 
8fa7be2
2fa8377
 
 
 
 
 
 
 
 
 
8fa7be2
2fa8377
 
 
 
 
8fa7be2
2fa8377
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2d540cf
2fa8377
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8fa7be2
2fa8377
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
# import os
# import base64
# from PIL import Image
# import streamlit as st
# from evaluator import (
#     IELTSTask1Evaluator,
#     IELTSTask1ExerciseGenerator,
#     IELTSTask2Evaluator,
#     IELTSTask2ExerciseGenerator
# )

# # Initialize session state for login
# if 'logged_in' not in st.session_state:
#     st.session_state.logged_in = False

# def check_password():
#     """Returns `True` if the user had the correct password."""
    
#     def password_entered():
#         """Checks whether a password entered by the user is correct."""
#         if st.session_state["password"] == "hamid1377" and st.session_state["username"] == "afarinesh":
#             st.session_state.logged_in = True
#             del st.session_state["password"]  # Don't store password
#             del st.session_state["username"]  # Don't store username
#         else:
#             st.session_state.logged_in = False

#     if not st.session_state.logged_in:
#         st.text_input("Username", key="username")
#         st.text_input("Password", type="password", key="password")
#         st.button("Login", on_click=password_entered)
#         return False
    
#     return True

# # Configure Streamlit page
# st.set_page_config(
#     page_title="IELTS Writing Evaluator",
#     page_icon="📝",
#     layout="wide",
#     initial_sidebar_state="expanded"
# )

# # Custom CSS
# st.markdown("""
# <style>
#     .stTabs [data-baseweb="tab-list"] {
#         gap: 2px;
#     }
#     .stTabs [data-baseweb="tab"] {
#         padding: 10px 20px;
#     }
#     .main .block-container {
#         padding-top: 2rem;
#     }
#     .stButton button {
#         width: 100%;
#     }
#     .assessment-box {
#         padding: 1rem;
#         border-radius: 0.5rem;
#         border: 1px solid #e0e0e0;
#         margin: 1rem 0;
#     }
#     .info-box {
#         background-color: #f8f9fa;
#         padding: 1rem;
#         border-radius: 0.5rem;
#         margin: 1rem 0;
#     }
# </style>
# """, unsafe_allow_html=True)

# # Initialize session state
# if 'current_task' not in st.session_state:
#     st.session_state.current_task = 'Task 1'
# if 'evaluation_results' not in st.session_state:
#     st.session_state.evaluation_results = None
# if 'graph_analysis' not in st.session_state:
#     st.session_state.graph_analysis = None

# def create_evaluator_instance(task_type):
#     """Creates and returns appropriate evaluator instance based on task type"""
#     try:
#         if task_type == "Task 1":
#             return IELTSTask1Evaluator(), IELTSTask1ExerciseGenerator()
#         else:
#             return IELTSTask2Evaluator(), IELTSTask2ExerciseGenerator()
#     except Exception as e:
#         st.error(f"Error initializing evaluator: {str(e)}")
#         return None, None

# def display_task1_interface():
#     """Displays Task 1 interface and handles evaluation"""
#     st.header("Academic Writing Task 1 Evaluation")
    
#     with st.container():
#         col1, col2 = st.columns([1, 1])
        
#         with col1:
#             st.subheader("Upload Graph/Chart")
#             uploaded_file = st.file_uploader(
#                 "Upload image (JPG, PNG)",
#                 type=['jpg', 'jpeg', 'png'],
#                 help="Upload the graph or chart you're describing"
#             )
            
#             # Add question input field
#             question = st.text_area(
#                 "Enter the Task 1 question",
#                 height=100,
#                 help="Paste the task question here"
#             )
            
#             if uploaded_file and question:
#                 try:
#                     image = Image.open(uploaded_file)
#                     st.image(image, use_column_width=True)
#                     # Save image temporarily
#                     temp_path = "temp_graph.jpg"
#                     image.save(temp_path)
                    
#                     # Analyze graph but don't display analysis
#                     evaluator, generator = create_evaluator_instance("Task 1")
#                     if evaluator:
#                         with st.spinner("Analyzing image..."):
#                             graph_analysis = evaluator.analyze_graph(temp_path, question)
#                             # Store graph analysis in session state without displaying
#                             st.session_state.graph_analysis = graph_analysis
#                             st.success("Image analysis complete")
#                 except Exception as e:
#                     st.error(f"Error processing image: {str(e)}")
#                 finally:
#                     if os.path.exists("temp_graph.jpg"):
#                         os.remove("temp_graph.jpg")
        
#         with col2:
#             st.subheader("Your Response")
#             written_response = st.text_area(
#                 "Enter your response here",
#                 height=300,
#                 help="Write your Task 1 response here"
#             )
            
#             if st.button("Evaluate Response", type="primary"):
#                 if not uploaded_file:
#                     st.warning("Please upload a graph/chart first.")
#                     return
#                 if not question:
#                     st.warning("Please enter the task question.")
#                     return
#                 if not written_response:
#                     st.warning("Please enter your written response.")
#                     return
                
#                 # Pass both question and graph analysis to evaluation
#                 if st.session_state.graph_analysis is not None:
#                     process_evaluation(
#                         written_response,
#                         "Task 1",
#                         {
#                             'question': question,
#                             'graph_analysis': st.session_state.graph_analysis
#                         }
#                     )
#                 else:
#                     st.warning("Please wait for image analysis to complete.")
                    
# def display_task2_interface():
#     """Displays Task 2 interface and handles evaluation"""
#     st.header("Academic Writing Task 2 Evaluation")
    
#     with st.container():
#         st.subheader("Essay Question")
#         essay_question = st.text_area(
#             "Enter the Task 2 question",
#             height=100,
#             help="Paste the essay question here"
#         )
        
#         st.subheader("Your Response")
#         written_response = st.text_area(
#             "Enter your response here",
#             height=300,
#             help="Write your Task 2 response here"
#         )
        
#         if st.button("Evaluate Essay", type="primary"):
#             if not essay_question:
#                 st.warning("Please enter the essay question.")
#                 return
#             if not written_response:
#                 st.warning("Please enter your written response.")
#                 return
                
#             process_evaluation(written_response, "Task 2", essay_question)

# def display_evaluation_results(task_type):
#     """Displays evaluation results in organized tabs"""
#     if not st.session_state.evaluation_results:
#         return
        
#     results = st.session_state.evaluation_results
    
#     tabs = st.tabs([
#         "📊 Overall Score",
#         "🔤 Grammar Analysis",
#         "📚 Vocabulary Analysis",
#         "🎯 Task Achievement",
#         "📝 Practice Exercises"
#     ])
    
#     with tabs[0]:
#         st.markdown("### Overall Assessment")
#         st.markdown(results['evaluation'])
        
#     with tabs[1]:
#         st.markdown("### Grammar Analysis")
#         st.markdown(results['grammar'])
        
#     with tabs[2]:
#         st.markdown("### Vocabulary Analysis")
#         st.markdown(results['vocabulary'])
        
#     with tabs[3]:
#         st.markdown("### Task Achievement")
#         st.markdown(results['task_achievement'])
        
#     with tabs[4]:
#         if st.button("Generate Practice Exercises", type="secondary"):
#             with st.spinner("Generating exercises..."):
#                 evaluator, generator = create_evaluator_instance(task_type)
#                 if generator:
#                     grammar_exercises = generator.generate_grammar_exercises(
#                         results['grammar'],
#                         results['written_response']
#                     )
#                     vocab_exercises = generator.generate_vocabulary_exercises(
#                         results['vocabulary']
#                     )
                    
#                     st.markdown("### Grammar Exercises")
#                     st.markdown(grammar_exercises)
                    
#                     st.markdown("### Vocabulary Exercises")
#                     st.markdown(vocab_exercises)

# def process_evaluation(written_response, task_type, additional_info=None):
#     """Processes the evaluation request and stores results"""
#     try:
#         evaluator, _ = create_evaluator_instance(task_type)
#         if not evaluator:
#             return
            
#         with st.spinner("Evaluating your response..."):
#             if task_type == "Task 1":
#                 question = additional_info['question']
#                 graph_analysis = additional_info['graph_analysis']
#                 evaluation = evaluator.evaluate_task1_response(question, graph_analysis, written_response)
#                 grammar = evaluator.analyze_grammar(written_response)
#                 vocabulary = evaluator.analyze_vocabulary(written_response)
#                 task_achievement = evaluator.analyze_task_achievement(question, graph_analysis, written_response)
#                 cohesion = evaluator.analyze_cohesion_coherence(written_response, graph_analysis)
#             else:
#                 evaluation = evaluator.evaluate_essay(additional_info, written_response)
#                 grammar = evaluator.analyze_grammar(written_response)
#                 vocabulary = evaluator.analyze_vocabulary(written_response)
#                 task_achievement = evaluator.analyze_task_achievement(additional_info, written_response)
#                 cohesion = evaluator.analyze_cohesion_coherence(written_response)
            
#             st.session_state.evaluation_results = {
#                 'evaluation': evaluation,
#                 'grammar': grammar,
#                 'vocabulary': vocabulary,
#                 'task_achievement': task_achievement,
#                 'cohesion': cohesion,
#                 'written_response': written_response
#             }
            
#             st.success("Evaluation complete! View results in the tabs below.")
#     except Exception as e:
#         st.error(f"Error during evaluation: {str(e)}")

# def main():
#     """Main application function"""
#     st.title("IELTS Writing Evaluation System")

#     if not check_password():
#         st.stop()  # Do not continue if check_password is not True
        
#     # Task selection in sidebar
#     with st.sidebar:
#         st.title("Navigation")
#         st.session_state.current_task = st.radio(
#             "Select Task Type",
#             ["Task 1", "Task 2"],
#             key="task_selection"
#         )
        
#         st.markdown("---")
#         st.markdown("### About")
#         st.markdown("""
#         This tool helps evaluate IELTS Writing responses and provides:
#         - Detailed scoring
#         - Grammar analysis
#         - Vocabulary feedback
#         - Task achievement assessment
#         - Practice exercises
#         """)
    
#     # Display appropriate interface based on task selection
#     if st.session_state.current_task == "Task 1":
#         display_task1_interface()
#     else:
#         display_task2_interface()
    
#     # Display evaluation results if available
#     if st.session_state.evaluation_results:
#         display_evaluation_results(st.session_state.current_task)

# if __name__ == "__main__":
#     main()


import os
import gradio as gr
from PIL import Image
from evaluator import (
    IELTSTask1Evaluator,
    IELTSTask1ExerciseGenerator,
    IELTSTask2Evaluator,
    IELTSTask2ExerciseGenerator
)

# -------------------------
# AUTHENTICATION
# -------------------------
USERNAME = "afarinesh"
PASSWORD = "hamid1377"

def login(username, password):
    if username == USERNAME and password == PASSWORD:
        return gr.update(visible=False), gr.update(visible=True), ""
    return gr.update(visible=True), gr.update(visible=False), "❌ Incorrect username or password"


# -------------------------
# EVALUATION LOGIC
# -------------------------
def create_evaluators(task):
    if task == "Task 1":
        return IELTSTask1Evaluator(), IELTSTask1ExerciseGenerator()
    return IELTSTask2Evaluator(), IELTSTask2ExerciseGenerator()

def analyze_task1_graph(image, question):
    if image is None:
        return None, "❌ Please upload an image."
    if not question.strip():
        return None, "❌ Please enter a Task 1 question."

    temp_path = "temp_graph.jpg"
    image.save(temp_path)

    evaluator, _ = create_evaluators("Task 1")
    analysis = evaluator.analyze_graph(temp_path, question)
    os.remove(temp_path)

    return analysis, "✅ Image analyzed successfully"

def evaluate_task1(image, question, response, graph_analysis):
    if image is None:
        return "Please upload an image", "", "", "", ""
    if not question.strip():
        return "Please enter the question", "", "", "", ""
    if not response.strip():
        return "Please write your response", "", "", "", ""
    if graph_analysis is None:
        return "Graph not analyzed yet", "", "", "", ""

    evaluator, _ = create_evaluators("Task 1")

    evaluation = evaluator.evaluate_task1_response(question, graph_analysis, response)
    grammar = evaluator.analyze_grammar(response)
    vocabulary = evaluator.analyze_vocabulary(response)
    task_achievement = evaluator.analyze_task_achievement(question, graph_analysis, response)
    cohesion = evaluator.analyze_cohesion_coherence(response, graph_analysis)

    return evaluation, grammar, vocabulary, task_achievement, cohesion

def evaluate_task2(question, response):
    if not question.strip():
        return "Please enter the question", "", "", "", ""
    if not response.strip():
        return "Please write your response", "", "", "", ""

    evaluator, _ = create_evaluators("Task 2")

    evaluation = evaluator.evaluate_essay(question, response)
    grammar = evaluator.analyze_grammar(response)
    vocabulary = evaluator.analyze_vocabulary(response)
    task_achievement = evaluator.analyze_task_achievement(question, response)
    cohesion = evaluator.analyze_cohesion_coherence(response)

    return evaluation, grammar, vocabulary, task_achievement, cohesion

def generate_exercises(task, grammar_text, vocab_text, response):
    _, generator = create_evaluators(task)
    g = generator.generate_grammar_exercises(grammar_text, response)
    v = generator.generate_vocabulary_exercises(vocab_text)
    return g, v


# -------------------------
# BUILD INTERFACE
# -------------------------
with gr.Blocks() as demo:

    # -------------------------
    # LOGIN SCREEN
    # -------------------------
    login_box = gr.Group(visible=True)
    with login_box:
        gr.Markdown("## 🔐 Login")
        user = gr.Textbox(label="Username")
        pwd = gr.Textbox(label="Password", type="password")
        login_btn = gr.Button("Login")
        login_msg = gr.Markdown("")

    # -------------------------
    # MAIN APP
    # -------------------------
    main = gr.Group(visible=False)
    with main:
        gr.Markdown("# 📝 IELTS Writing Evaluator")

        task_selector = gr.Radio(["Task 1", "Task 2"], label="Choose Task Type", value="Task 1")

        # -------------------------
        # TASK 1 UI
        # -------------------------
        with gr.Tab("Task 1"):
            image = gr.Image(type="pil", label="Upload Graph/Chart")
            t1_question = gr.Textbox(label="Task 1 Question", lines=3)
            analyze_btn = gr.Button("Analyze Graph")
            graph_output = gr.Markdown("")
            graph_state = gr.State()

            response1 = gr.Textbox(label="Your Response", lines=10)
            eval_btn1 = gr.Button("Evaluate Response")

            evaluation1 = gr.Markdown()
            grammar1 = gr.Markdown()
            vocabulary1 = gr.Markdown()
            task_ach1 = gr.Markdown()
            cohesion1 = gr.Markdown()

        # -------------------------
        # TASK 2 UI
        # -------------------------
        with gr.Tab("Task 2"):
            t2_question = gr.Textbox(label="Task 2 Question", lines=3)
            response2 = gr.Textbox(label="Your Response", lines=10)
            eval_btn2 = gr.Button("Evaluate Essay")

            evaluation2 = gr.Markdown()
            grammar2 = gr.Markdown()
            vocabulary2 = gr.Markdown()
            task_ach2 = gr.Markdown()
            cohesion2 = gr.Markdown()

        # -------------------------
        # EXERCISE GENERATION
        # -------------------------
        gr.Markdown("---\n## 📚 Practice Exercises")
        exercise_btn = gr.Button("Generate Exercises")
        exercises_grammar = gr.Markdown()
        exercises_vocab = gr.Markdown()

    # -------------------------
    # BIND FUNCTIONS
    # -------------------------
    login_btn.click(
        login,
        [user, pwd],
        [login_box, main, login_msg]
    )

    analyze_btn.click(
        analyze_task1_graph,
        [image, t1_question],
        [graph_state, graph_output]
    )

    eval_btn1.click(
        evaluate_task1,
        [image, t1_question, response1, graph_state],
        [evaluation1, grammar1, vocabulary1, task_ach1, cohesion1]
    )

    eval_btn2.click(
        evaluate_task2,
        [t2_question, response2],
        [evaluation2, grammar2, vocabulary2, task_ach2, cohesion2]
    )

    exercise_btn.click(
        generate_exercises,
        [task_selector, grammar1, vocabulary1, response1],
        [exercises_grammar, exercises_vocab]
    )


# -------------------------
# RUN
# -------------------------
demo.launch()