File size: 6,501 Bytes
b96156b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import os
import sys
import warnings

# Suppress benign warnings from dependencies (like Starlette)
warnings.filterwarnings("ignore")

# Add current directory to path so imports work correctly
sys.path.append(os.path.abspath(os.path.dirname(__file__)))

from preprocessing.process_pipeline import generate_mcq_pipeline

def process_text(passage):
    if not passage.strip():
        return "⚠️ Please enter some Tamil text first!", []
    
    # Split the text into sentences
    sentences = [s.strip() for s in passage.replace("!", ".").replace("?", ".").split(".") if s.strip()]
    mcq_list = []
    
    for sentence in sentences:
        try:
            # Run the pipeline
            res = generate_mcq_pipeline(sentence)
            if res and res.get("question") and len(res.get("options", [])) >= 4:
                mcq_list.append(res)
        except Exception as e:
            print(f"Error processing sentence '{sentence}': {e}")
            pass
            
    if not mcq_list:
        return "ℹ️ No suitable sentences found for MCQ generation. Try a longer, more descriptive paragraph.", []
        
    return f"✅ Successfully generated {len(mcq_list)} MCQs!", mcq_list


default_text = """சாதி அடிப்படையிலான அரசியல் இயக்கங்கள் மற்றும் அவற்றின் தாக்கம் குறித்து விரிவாக பார்க்கும்போது, தமிழ்நாட்டின் அரசியல் பரிணாமம் பல்வேறு சமூக, மொழி மற்றும் பொருளாதார காரணிகளால் வடிவமைக்கப்பட்டுள்ளது. இந்த அமைப்பில் தேர்தல் ஆணையத்தின் கட்டுப்பாடுகள், தொகுதி மறுசீரமைப்புகள், மற்றும் வாக்காளர் பதிவுகள் ஆகியவை குறிப்பிடத்தக்கவை. சட்டமன்றத்தில் நிறைவேற்றப்படும் மசோதாக்கள் பொதுவாக குழு ஆய்வுகள் மற்றும் விவாதங்களின் மூலம் மாற்றங்களுடன் நிறைவேற்றப்படுகின்றன. நிர்வாகத் துறைகள் அமைச்சரவை முடிவுகளின் அடிப்படையில் செயல்படுகின்றன மற்றும் ஒவ்வொரு துறைக்கும் தனிப்பட்ட செயல்முறை விதிகள் உள்ளன. அரசியல் கட்சிகள் தங்களது தேர்தல் அறிக்கைகளில் குறிப்பிட்ட நலத்திட்டங்களை முன்வைத்து வாக்காளர்களை அணுகுகின்றன."""

with gr.Blocks(title="Tamil Question Answering App") as demo:
    gr.HTML("""
    <div style="text-align: center; padding: 20px;">
        <h1 style="color: #f97316; font-size: 2.5rem; margin-bottom: 0;">தமிழ் Question Answering App</h1>
        <p style="color: #6b7280; font-size: 1.2rem;">Generate interactive questions from text and answer them!</p>
    </div>
    """)
    
    mcq_state = gr.JSON(visible=False, value=[])
    
    with gr.Row():
        with gr.Column(scale=1):
            gr.Markdown("### ⚙️ Settings / About")
            gr.Markdown("""
            This application uses advanced Natural Language Processing to generate Multiple-Choice Questions (MCQs) in **Tamil**.
            
            **Features:**
            1. **NER & POS Tagging**
            2. **Lemmatizer Engine**
            3. **FastText Distractors**
            """)
            
        with gr.Column(scale=2):
            passage_input = gr.Textbox(
                label="📝 Enter Tamil Passage", 
                lines=8, 
                value=default_text,
                placeholder="இங்கு தமிழ் உரையினை உள்ளிடவும்..."
            )
            generate_btn = gr.Button("✨ Generate MCQs", variant="primary")
            status_output = gr.Markdown()
            
    quiz_container = gr.Column()
    
    @gr.render(inputs=mcq_state)
    def render_quiz(mcqs):
        if not mcqs:
            return
            
        gr.Markdown("---")
        gr.Markdown("## ✍️ Interactive Quiz")
        
        for i, mcq in enumerate(mcqs):
            with gr.Group():
                gr.Markdown(f"### Q{i+1}: {mcq['question']}")
                
                # Function to check answer
                def make_checker(correct_ans):
                    def check(user_choice):
                        if not user_choice:
                            return ""
                        if user_choice == correct_ans:
                            return f"<div style='padding:10px; background-color:#d1fae5; color:#065f46; border-radius:5px;'><b>✅ Correct!</b> The answer is {correct_ans}.</div>"
                        else:
                            return f"<div style='padding:10px; background-color:#fee2e2; color:#991b1b; border-radius:5px;'><b>❌ Incorrect.</b> You chose {user_choice}. Try again!</div>"
                    return check
                
                radio = gr.Radio(choices=mcq['options'], label="Select your answer:")
                feedback = gr.HTML()
                
                # When user clicks a radio option, check it
                radio.change(
                    fn=make_checker(mcq['answer']),
                    inputs=[radio],
                    outputs=[feedback]
                )
                
                with gr.Accordion("💡 Show Correct Answer", open=False):
                    gr.Markdown(f"**Correct Answer:** {mcq['answer']}")
            gr.Markdown("<br>")
            
    def on_generate(passage):
        status, mcqs = process_text(passage)
        return status, mcqs

    generate_btn.click(
        fn=on_generate,
        inputs=[passage_input],
        outputs=[status_output, mcq_state]
    )

if __name__ == "__main__":
    demo.launch(theme=gr.themes.Soft())