File size: 11,798 Bytes
a30b564
 
 
d6b9612
 
 
a30b564
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d6b9612
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6a1b068
d6b9612
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a30b564
 
b8caddb
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
import gradio as gr
import google.generativeai as genai
import random
from datasets import load_dataset
import difflib

# Danh sách các chủ đề hoàn toàn bằng tiếng Anh
topics = [
    "Information Technology & Artificial Intelligence",
    "Travel & Cultural Heritage",
    "Economics & Finance",
    "Smart Agriculture",
    "Daily Communication",
    "Scientific Research",
    "Environment & Climate Change"
] + [
  "My New School",
  "My House",
  "My Friends",
  "My Neighbourhood",
  "Natural Wonders of the Viet Nam",
  "Our Tet Holiday",
  "Television",
  "Sports and Games",
  "Cities around the World",
  "Our Houses in the Future",
  "Our Greener World",
  "Robots"
] + [
  "Hobbies",
  "Healthy Living",
  "Community Service",
  "Music and Arts",
  "Food and Drink",
  "A Visit to a School",
  "Traffic",
  "Films",
  "Festivals around the World",
  "Energy Sources",
  "Travelling in the Future",
  "English-speaking Countries"
] + [
  "Leisure time",
  "Life in the countryside",
  "Teenagers",
  "Ethnic groups of Viet Nam",
  "Our customs and traditions",
  "Lifestyles",
  "Environmental protection",
  "Shopping",
  "Natural disasters",
  "Communication in the future",
  "Science and technology",
  "Life on other planets"
] + [
  "Local community",
  "City life",
  "Teen stress and pressure",
  "Life in the past",
  "Our experiences",
  "Vietnamese lifestyle: then and now",
  "Natural wonders of the world",
  "English speaking countries",
  "World Englishes",
  "Planet Earth",
  "Electronic devices",
  "Career choices"
] + [
  "Family Life",
  "Humans and the Environment",
  "Music",
  "For a Better Community",
  "Inventions",
  "Gender Equality",
  "Viet Nam and International Organisations",
  "New Ways to Learn",
  "Protecting the Environment",
  "Ecotorism"
] + [
  "A long and healthy life",
  "The generation gap",
  "Cities of the future",
  "ASEAN and Viet Nam",
  "Global warming",
  "Preserving our heritage",
  "Education options for school-leavers",
  "Becoming independent",
  "Social issues",
  "The ecosystem"
] + [
  "Life stories",
  "A multicultural world",
  "Green living",
  "Urbanisation",
  "The world of work",
  "Artificial intelligence",
  "The world of advertising",
  "Cultural identity",
  "Choosing a career",
  "Lifelong learning"
]

tenses = [
    "Present Simple",
    "Present Continuous",
    "Present Perfect",
    "Present Perfect Continuous",
    "Past Simple",
    "Past Continuous",
    "Past Perfect",
    "Past Perfect Continuous",
    "Future Simple",
    "Future Continuous",
    "Future Perfect",
    "Future Perfect Continuous"
]

grammar_focus_areas = [
    "Sentence Structure & Clauses",  # Relative clauses, Conditionals, Reported speech
    "Modal Verbs",                   # Ability, Probability, Obligation
    "Passive Voice & Causatives",    # Focus on the object or service
    "Articles & Prepositions",       # The 'glue' of the language
    "Gerunds vs Infinitives",        # Using V-ing vs to-V
    "Determiners & Quantifiers",     # much, many, few, little, each, every
    "Conjunctions",                  # Coordinating (and, but) vs Subordinating (although, because)
    "Adverbial Phrases",             # Adding detail to actions
    "Subject-Verb Agreement"         # Ensuring logic between actor and action
]

# Danh sách ngôn ngữ hỗ trợ
languages = [
    "English",
    "Vietnamese",
    "Japanese",
    "Korean",
    "Chinese",
    "French",
    "Spanish",
    "German"
]
conjunction_types = [
    "Concession (Although, Despite)", 
    "Reason (Because, Since, Due to)",
    "Purpose (So that, In order to)",
    "Result (So...that, Such...that)",
    "Contrast (While, Whereas)",
    "Condition (If, Unless, Provided that)"
]

def generate_source_text(api_key, source_lang, target_lang):
    topic = random.choice(topics)
    tense = random.choice(tenses)
    grammar = random.choice(grammar_focus_areas)
    conjunction_type = random.choice(conjunction_types)
    if not api_key.strip():
        return "⚠️ Please enter your Gemini API Key."
    
    try:
        genai.configure(api_key=api_key)
        model = genai.GenerativeModel('gemini-3.1-flash-lite-preview')
        
        # System prompt hoàn toàn bằng tiếng Anh, yêu cầu output bằng source_lang
        prompt = f"""
        Act as a professional language teacher.
        Generate a long sentence in {source_lang} about '{topic}', incorporating {grammar}, {tense} and {conjunction_type} naturally.
        This text is intended for a student to practice translating from {source_lang} into {target_lang}.
        Return ONLY the {source_lang} text. Do NOT include any translations, introductions, or explanations.
        """
        
        response = model.generate_content(prompt)
        return response.text.strip()
    except Exception as e:
        return f"API Error: {str(e)}"

def check_translation(api_key, source_text, user_translation, source_lang, target_lang):
    if not api_key.strip():
        return "⚠️ Please enter your Gemini API Key."
    if not source_text.strip():
        return "⚠️ Please generate source text first."
    if not user_translation.strip():
        return "⚠️ Please enter your translation."

    try:
        genai.configure(api_key=api_key)
        model = genai.GenerativeModel('gemini-3.1-flash-lite-preview')

        # System prompt bằng tiếng Anh, ép buộc output lời giải thích bằng target_lang và theo format mới
        prompt = f"""
        Act as an expert linguist and strict language evaluator. Evaluate the following translation:
        
        - Source text ({source_lang}): "{source_text}"
        - User's translation ({target_lang}): "{user_translation}"

        Provide detailed feedback formatted in Markdown. 
        IMPORTANT: You MUST write the entire feedback and explanation in {source_lang}. And 
        
        Strictly follow this structure:
        1. **CEFR Level:** Evaluate the vocabulary and sentence structure used in the user's translation and assign an estimated CEFR level (A1, A2, B1, B2, C1, or C2).
        2. **Scores:**
           - Grammar Score: [Score]/10
           - Vocabulary Usage Score: [Score]/10
        3. **Detailed Feedback & Corrections:** - Point out specific errors in grammar, word choice, or unnatural phrasing.
           - Explain clearly why they are incorrect.
           - Provide step-by-step guidance on how to fix them.
        4. **Reference Translations:** Provide 1-2 highly accurate, natural, and native-like alternative translations in {target_lang}.
        MANDATORY: NO explanation, NO introductory sentences. ONLY return the correctly translated content and formatting unrelated to these points.
        """
        response = model.generate_content(prompt)
        return response.text.strip()
    except Exception as e:
        return f"API Error: {str(e)}"


# ==========================================
# 1. CÁC HÀM XỬ LÝ CHO TAB "LISTEN & WRITE"
# ==========================================
print("Đang kết nối với dataset libriheavy...")
dataset = load_dataset("mythicinfinity/libriheavy", "large", split="train", streaming=True)

def get_new_sample():
    shuffled_dataset = dataset.shuffle(buffer_size=1000, seed=random.randint(0, 10000))
    sample = next(iter(shuffled_dataset))
    audio_data = sample['audio']['array']
    sr = sample['audio']['sampling_rate']
    truth_text = sample['text_transcription']
    return (sr, audio_data), truth_text, "", ""

def evaluate_listen(user_text, truth_text):
    if not truth_text:
        return "<span style='color:red'>Vui lòng nhấn 'Tải Audio Mới' trước khi chấm điểm!</span>"
    
    user_words = user_text.strip().lower().split()
    truth_words = truth_text.strip().lower().split()
    
    matcher = difflib.SequenceMatcher(None, truth_words, user_words)
    correct_words = sum([match.size for match in matcher.get_matching_blocks()])
    total_words = len(truth_words)
    
    score = (correct_words / total_words) * 100 if total_words > 0 else 0
    result_html = f"<h3>Kết quả: {correct_words}/{total_words} từ đúng ({score:.1f}%)</h3>"
    result_html += f"<p><b>Văn bản gốc:</b> {truth_text}</p>"
    result_html += f"<p><b>Văn bản của bạn:</b> {user_text}</p>"
    return result_html

# ==========================================
# 2. CÁC HÀM XỬ LÝ CHO TAB "DỊCH THUẬT"
# ==========================================
# Khởi tạo danh sách ngôn ngữ (thêm bớt tùy ý bạn)
languages = ["English", "Vietnamese", "French", "Japanese", "Korean", "Chinese"]

# TODO: Thay thế nội dung 2 hàm này bằng logic gọi Gemini API thực tế của bạn
def generate_source_text(api_key, source_lang, target_lang):
    if not api_key:
        return "Vui lòng nhập API Key!"
    return f"Đây là một câu mẫu tiếng {source_lang} để bạn dịch sang {target_lang}."

def check_translation(api_key, source_text, user_translation, source_lang, target_lang):
    if not api_key:
        return "*Vui lòng nhập API Key!*"
    return f"**Nhận xét từ AI:** Bản dịch '{user_translation}' của bạn khá tốt. (Đang sử dụng Placeholder)"

# ==========================================
# 3. XÂY DỰNG GIAO DIỆN VỚI GRADIO TABS
# ==========================================
with gr.Blocks(theme=gr.themes.Soft(), title="ReWrite") as demo:

    with gr.Tab("🎧 Listen & Write"):
        truth_state = gr.State("")
        with gr.Row():
            with gr.Column():
                audio_player = gr.Audio(label="Listen the audio", interactive=False)
                btn_new = gr.Button("🔄 Load new audio", variant="secondary")
                
                user_input = gr.Textbox(label="Type your text you listen here...", lines=5, placeholder="Type what you hear...")
                btn_submit_listen = gr.Button("✅ Evaluate", variant="primary")
            
            with gr.Column():
                result_output_listen = gr.HTML(label="Kết quả Đánh giá")
                
        btn_new.click(
            fn=get_new_sample, 
            inputs=[], 
            outputs=[audio_player, truth_state, user_input, result_output_listen]
        )
        
        btn_submit_listen.click(
            fn=evaluate_listen, 
            inputs=[user_input, truth_state], 
            outputs=[result_output_listen]
        )

    # --- TAB 2: DỊCH THUẬT ---
    with gr.Tab("🌍 Translate"):
        with gr.Row():
            api_key_input = gr.Textbox(label="Gemini API Key", type="password", placeholder="Enter your API key here...")
            source_lang_dropdown = gr.Dropdown(choices=languages, value="English", label="Source Language")
            target_lang_dropdown = gr.Dropdown(choices=languages, value="Vietnamese", label="Target Language")
            
        generate_btn = gr.Button("Generate Source Text", variant="secondary")
        source_textbox = gr.Textbox(label="Source Text", interactive=False)
        user_translation_box = gr.Textbox(label="Your Translation", placeholder="Type your translation here...")
        
        check_btn = gr.Button("✅ Evaluate", variant="primary")
        feedback_output = gr.Markdown(value="*AI feedback will appear here.*")

        generate_btn.click(
            fn=generate_source_text,
            inputs=[api_key_input, source_lang_dropdown, target_lang_dropdown],
            outputs=source_textbox
        )
        
        check_btn.click(
            fn=check_translation,
            inputs=[api_key_input, source_textbox, user_translation_box, source_lang_dropdown, target_lang_dropdown],
            outputs=feedback_output
        )

if __name__ == "__main__":
    demo.launch()