Spaces:
Sleeping
Sleeping
import gradio as gr | |
import re | |
# Define images and corresponding titles, original sentences, and examples | |
images_and_titles = [ | |
{ | |
"title": "1. Echo Ridge Village", | |
"image": "https://github.com/englissi/englissi/blob/00d4ac2ab7d8b88169af3afe332897db02056a28/Sample/wordcloud_Echo%20Ridge%20Village.png?raw=true", | |
"sentence": "In the small mountain village of Echo Ridge, adventure was a part of everyday life.", | |
"example": "small mountain village / Echo Ridge / adventure / part of everyday life (λμ¬ νν λ³ν κ°λ₯)" | |
}, | |
{ | |
"title": "2. Alex finding the map", | |
"image": "https://github.com/englissi/englissi/blob/00d4ac2ab7d8b88169af3afe332897db02056a28/Sample/wordcloud_Alex%20Finding%20the%20map.png?raw=true", | |
"sentence": "One day, while exploring the local library, Alex stumbled upon an ancient map tucked inside a forgotten book on village lore.", | |
"example": "explore the local library / One day / tuck inside a forgotten book / Alex stumble upon an ancient map (λμ¬ νν λ³ν κ°λ₯)" | |
}, | |
{ | |
"title": "3. Alex, Mia, and Sam preparing for the expedition", | |
"image": "https://github.com/englissi/englissi/blob/00d4ac2ab7d8b88169af3afe332897db02056a28/Sample/wordcloud_Preparing%20for%20the%20expedition.png?raw=true", | |
"sentence": "Knowing the journey would be risky, he enlisted the help of his best friends, Mia and Sam.", | |
"example": "the help / Know the journey would be risky / enlist / Mia and Sam / of his best friends (λμ¬ νν λ³ν κ°λ₯)" | |
}, | |
{ | |
"title": "4. The journey begins at dawn", | |
"image": "https://github.com/englissi/englissi/blob/00d4ac2ab7d8b88169af3afe332897db02056a28/Sample/wordcloud_Journey%20begins%20at%20dawn.png?raw=true", | |
"sentence": "Their journey began at dawn. They trekked through dense forests, crossed rushing streams, and climbed steep cliffs.", | |
"example": "trek through dense forests / Their journey begin at dawn / cross rushing streams / climb steep cliffs (λμ¬ νν λ³ν κ°λ₯)" | |
}, | |
{ | |
"title": "5. Reaching Whispering Hollow", | |
"image": "https://github.com/englissi/englissi/blob/00d4ac2ab7d8b88169af3afe332897db02056a28/Sample/wordcloud_Reaching%20whispering%20hollow.png?raw=true", | |
"sentence": "After hours of hiking, they finally reached Whispering Hollow.", | |
"example": "reach / After hours of hiking / they finally / Whispering Hollow (λμ¬ νν λ³ν κ°λ₯)" | |
}, | |
{ | |
"title": "6. Exploring the cave", | |
"image": "https://github.com/englissi/englissi/blob/00d4ac2ab7d8b88169af3afe332897db02056a28/Sample/wordcloud_Exploring%20the%20cave.png?raw=true", | |
"sentence": "Using their flashlights, they ventured deeper into the cave, guided by the markings on the map.", | |
"example": "they venture deeper into the cave / Using their flashlights / guide by the markings / on the map (λμ¬ νν λ³ν κ°λ₯)" | |
}, | |
{ | |
"title": "7. Finding the ancient chest", | |
"image": "https://github.com/englissi/englissi/blob/00d4ac2ab7d8b88169af3afe332897db02056a28/Sample/wordcloud_Finding%20the%20ancient%20chest.png?raw=true", | |
"sentence": "As they reached the heart of the cave, they discovered an ancient chest hidden behind a fallen boulder.", | |
"example": "discover an ancient chest / hidden behind / As they reach the heart of the cave / a fallen boulder (λμ¬ νν λ³ν κ°λ₯)" | |
}, | |
{ | |
"title": "8. Village celebration", | |
"image": "https://github.com/englissi/englissi/blob/00d4ac2ab7d8b88169af3afe332897db02056a28/Sample/wordcloud_Village%20celebration.png?raw=true", | |
"sentence": "The village celebrated their discovery, and the children were hailed as heroes.", | |
"example": "The village celebrate their discovery / the children / be hail / as heroes (λμ¬ νν λ³ν κ°λ₯)" | |
} | |
] | |
# Helper function to provide corrective feedback for verb errors | |
def provide_feedback(word, correct_word, form_type): | |
if form_type == "participle": | |
return f"λΆμ¬κ΅¬λ¬Έ '{word}'μ μ£Όμ μ μ£Όμ΄μ λΆμμ μ μ£Όμ΄κ° κ°μ κ²½μ°μ μ¬μ©λ©λλ€." | |
elif form_type == "past_simple": | |
return f"λμ¬ '{word}'μ κ³Όκ±°νμ '{correct_word}'μ λλ€." | |
elif form_type == "modal_auxiliary": | |
return f"μ‘°λμ¬ '{word}'λ νμ λμ¬ μνκ³Ό ν¨κ» μ¬μ©λ©λλ€." | |
elif form_type == "past_participle": | |
return f"λμ¬ '{word}'μ κ³Όκ±°λΆμ¬λ '{correct_word}'μ λλ€." | |
else: | |
return f"λμ¬ '{word}'μ ννκ° μ¬λ°λ₯΄μ§ μμ΅λλ€." | |
# Function to find the correct past tense of a verb | |
def get_correct_past_tense(word, sentence, original_sentence): | |
word = word.lower() | |
words = original_sentence.lower().split() | |
for i, w in enumerate(words): | |
if w.lower() == word: | |
return words[i] | |
return None | |
# Function to compare student's sentences with original sentences and provide feedback | |
def compare_sentences(sentences): | |
comparisons = [] | |
error_counts = { | |
"Past Simple": 0, | |
"Present Participle": 0, | |
"Past Participle": 0, | |
"Modal Auxiliary": 0 | |
} | |
verb_forms_info = { | |
"In the small mountain village of Echo Ridge, adventure was a part of everyday life.": [ | |
"Verb Form: Past Simple ('was')" | |
], | |
"One day, while exploring the local library, Alex stumbled upon an ancient map tucked inside a forgotten book on village lore.": [ | |
"Verb Form: Past Simple ('stumbled')", | |
"Participle Construction: Present Participle ('exploring')", | |
"Participle Construction: Past Participle ('tucked', 'forgotten')" | |
], | |
"Knowing the journey would be risky, he enlisted the help of his best friends, Mia and Sam.": [ | |
"Verb Form: Past Simple ('enlisted')", | |
"Participle Construction: Present Participle ('Knowing')", | |
"Auxiliary Verb: Modal ('would be')" | |
], | |
"Their journey began at dawn. They trekked through dense forests, crossed rushing streams, and climbed steep cliffs.": [ | |
"Verb Form: Past Simple ('began', 'trekked', 'crossed', 'climbed')" | |
], | |
"After hours of hiking, they finally reached Whispering Hollow.": [ | |
"Verb Form: Past Simple ('reached')", | |
"Participle Construction: Present Participle ('hiking')" | |
], | |
"Using their flashlights, they ventured deeper into the cave, guided by the markings on the map.": [ | |
"Verb Form: Past Simple ('ventured')", | |
"Participle Construction: Present Participle ('Using')", | |
"Participle Construction: Past Participle ('guided')" | |
], | |
"As they reached the heart of the cave, they discovered an ancient chest hidden behind a fallen boulder.": [ | |
"Verb Form: Past Simple ('reached', 'discovered')", | |
"Participle Construction: Past Participle ('hidden', 'fallen')" | |
], | |
"The village celebrated their discovery, and the children were hailed as heroes.": [ | |
"Verb Form: Past Simple ('celebrated')", | |
"Verb Form: Past Simple Passive ('were hailed')" | |
] | |
} | |
for i, item in enumerate(images_and_titles): | |
feedback = "" | |
student_sentence = sentences[i].strip() | |
original_sentence = item["sentence"].strip() | |
student_words = re.findall(r'\b\w+\b', student_sentence) | |
original_words = re.findall(r'\b\w+\b', original_sentence) | |
# Initial feedback header | |
feedback += f"**Your Sentence:** {student_sentence}\n\n**Original Sentence:** {original_sentence}\n\n" | |
# Verb forms in the original sentence | |
feedback += "Verb Forms in the Original Sentence:\n" | |
for verb_info in verb_forms_info[original_sentence]: | |
feedback += f"- {verb_info}\n" | |
feedback += "\n" | |
# Check for verb errors | |
for word in student_words: | |
if word.lower() not in [w.lower() for w in original_words]: | |
correct_word = get_correct_past_tense(word, student_sentence, original_sentence) | |
if correct_word: | |
feedback += provide_feedback(word, correct_word, "past_simple") + "\n" | |
error_counts["Past Simple"] += 1 | |
# Check for participle construction errors | |
participles = ["knowing", "exploring", "tucked", "forgotten", "guided", "hidden", "fallen"] | |
for participle in participles: | |
if re.search(rf'\b{participle}\b', student_sentence, re.IGNORECASE): | |
feedback += provide_feedback(participle, "", "participle") + "\n" | |
if participle in ["exploring", "Knowing", "hiking", "Using"]: | |
error_counts["Present Participle"] += 1 | |
else: | |
error_counts["Past Participle"] += 1 | |
# Check for modal auxiliary verb errors | |
if re.search(r'\bwould be\b', student_sentence, re.IGNORECASE) and 'would be' not in original_sentence.lower(): | |
feedback += provide_feedback("would be", "would be", "modal_auxiliary") + "\n" | |
error_counts["Modal Auxiliary"] += 1 | |
comparisons.append(f"**{item['title']}**\n\n{feedback}\n") | |
error_summary = ( | |
f"Past Simple Errors: {error_counts['Past Simple']}\n" | |
f"Present Participle Errors: {error_counts['Present Participle']}\n" | |
f"Past Participle Errors: {error_counts['Past Participle']}\n" | |
f"Modal Auxiliary Errors: {error_counts['Modal Auxiliary']}\n" | |
) | |
return "\n".join(comparisons) + "\n" + error_summary | |
# Function to show the student's final story | |
def show_final_story(sentences): | |
return "\n".join(sentences) | |
# Create the Gradio interface | |
with gr.Blocks() as iface: | |
gr.Markdown("# The Guardian's Secret: Rewriting Exercise") | |
sentence_inputs = [] | |
for item in images_and_titles: | |
with gr.Row(): | |
gr.Markdown(f"### {item['title']}") | |
gr.Image(value=item['image'], interactive=False) | |
gr.Markdown(f"**Example:** {item['example']}") | |
sentence_input = gr.Textbox(label="Your Sentence", interactive=True) | |
sentence_inputs.append(sentence_input) | |
finish_button = gr.Button("Finish") | |
comparison_output = gr.Markdown(visible=False) | |
final_story_output = gr.Markdown(visible=False) | |
def on_finish(*sentences): | |
comparison = compare_sentences(sentences) | |
final_story = show_final_story(sentences) | |
return gr.update(visible=True, value=comparison), gr.update(visible=True, value=final_story) | |
finish_button.click(fn=on_finish, inputs=sentence_inputs, outputs=[comparison_output, final_story_output]) | |
iface.launch() | |