Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,55 +1,106 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import random
|
| 3 |
|
| 4 |
-
#
|
| 5 |
-
|
| 6 |
-
"Icyapa cya
|
| 7 |
-
"
|
| 8 |
-
"
|
| 9 |
-
"
|
| 10 |
-
"
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
def gusubiza(ikibazo):
|
| 18 |
-
return
|
| 19 |
|
| 20 |
-
# Gutegura quiz 5 random
|
| 21 |
def gutegura_quiz():
|
| 22 |
quiz = random.sample(ibibazo_bya_quiz, min(5, len(ibibazo_bya_quiz)))
|
| 23 |
quiz_questions = []
|
| 24 |
quiz_choices = []
|
| 25 |
for q, ans in quiz:
|
| 26 |
-
other_answers = list(set(
|
| 27 |
choices = random.sample(other_answers, min(2, len(other_answers))) + [ans]
|
| 28 |
random.shuffle(choices)
|
| 29 |
quiz_questions.append(q)
|
| 30 |
quiz_choices.append(choices)
|
| 31 |
return quiz_questions, quiz_choices
|
| 32 |
|
| 33 |
-
# Gusuzuma quiz
|
| 34 |
def gusuzuma_quiz(*user_answers_and_questions):
|
| 35 |
user_answers = user_answers_and_questions[:-1]
|
| 36 |
quiz_questions = user_answers_and_questions[-1]
|
| 37 |
score = 0
|
| 38 |
for i, ans in enumerate(user_answers):
|
| 39 |
-
correct =
|
| 40 |
if ans == correct:
|
| 41 |
score += 1
|
| 42 |
return f"Watoye neza {score}/{len(user_answers)}"
|
| 43 |
|
| 44 |
-
#
|
| 45 |
with gr.Blocks(theme=gr.themes.Soft(primary_hue="green", secondary_hue="yellow")) as demo:
|
| 46 |
gr.Markdown(
|
| 47 |
"""
|
| 48 |
-
# 🚗
|
| 49 |
### *Yakozwe na Viateur Irasubiza*
|
| 50 |
---
|
| 51 |
-
|
| 52 |
-
Hitamo ikibazo ubone igisubizo cyihuse kandi gisobanutse.
|
| 53 |
"""
|
| 54 |
)
|
| 55 |
|
|
@@ -57,34 +108,27 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="green", secondary_hue="yellow")
|
|
| 57 |
with gr.Row():
|
| 58 |
with gr.Column(scale=1):
|
| 59 |
ikibazo = gr.Dropdown(
|
| 60 |
-
choices=list(
|
| 61 |
-
label="➡️ Hitamo ikibazo
|
| 62 |
-
value=
|
| 63 |
)
|
| 64 |
kanda = gr.Button("🔍 Saba igisubizo")
|
| 65 |
with gr.Column(scale=1):
|
| 66 |
-
igisubizo = gr.Textbox(label="🧠 Igisubizo
|
| 67 |
|
| 68 |
kanda.click(fn=gusubiza, inputs=ikibazo, outputs=igisubizo)
|
| 69 |
|
| 70 |
-
gr.Markdown("---")
|
| 71 |
-
|
| 72 |
-
# Quiz section
|
| 73 |
gr.Markdown("## 📝 Quiz: Menya ibyo wize")
|
| 74 |
quiz_button = gr.Button("Tangira Quiz")
|
| 75 |
quiz_output = gr.Textbox(label="📝 Ibisubizo bya Quiz")
|
| 76 |
|
| 77 |
-
# States
|
| 78 |
quiz_questions_state = gr.State()
|
| 79 |
quiz_choices_state = gr.State()
|
| 80 |
-
|
| 81 |
-
# Widgets z'ibibazo 5 ziri hidden kugeza quiz itangijwe
|
| 82 |
radio_widgets = []
|
| 83 |
for i in range(5):
|
| 84 |
r = gr.Radio(choices=[], label=f"Ikibazo {i+1}", visible=False)
|
| 85 |
radio_widgets.append(r)
|
| 86 |
-
|
| 87 |
-
# Fungura quiz
|
| 88 |
def start_quiz():
|
| 89 |
questions, choices = gutegura_quiz()
|
| 90 |
outputs = []
|
|
@@ -98,28 +142,16 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="green", secondary_hue="yellow")
|
|
| 98 |
outputs.append(r)
|
| 99 |
return questions, choices, *outputs
|
| 100 |
|
| 101 |
-
quiz_button.click(
|
| 102 |
-
fn=start_quiz,
|
| 103 |
-
inputs=[],
|
| 104 |
-
outputs=[quiz_questions_state, quiz_choices_state] + radio_widgets
|
| 105 |
-
)
|
| 106 |
|
| 107 |
-
# Fungura button yo subiza quiz
|
| 108 |
submit_btn = gr.Button("💯 Subiza Quiz")
|
| 109 |
|
| 110 |
def submit_quiz(*answers):
|
| 111 |
-
# answers ni ibisubizo byose byatoranyijwe na user
|
| 112 |
questions = quiz_questions_state.value
|
| 113 |
return gusuzuma_quiz(*answers, questions)
|
| 114 |
|
| 115 |
submit_btn.click(fn=submit_quiz, inputs=radio_widgets, outputs=quiz_output)
|
| 116 |
|
| 117 |
-
gr.Markdown(
|
| 118 |
-
"""
|
| 119 |
-
---
|
| 120 |
-
**© 2025 Viateur Irasubiza**
|
| 121 |
-
🛣️ *AI ifasha Abanyarwanda kwiga amategeko y’umuhanda mu buryo bworoshye n’igihe gito.*
|
| 122 |
-
"""
|
| 123 |
-
)
|
| 124 |
|
| 125 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import random
|
| 3 |
|
| 4 |
+
# --- TEGURA IBIBAZO 200 ---
|
| 5 |
+
templates = [
|
| 6 |
+
"Icyapa cya {} gisobanura iki?",
|
| 7 |
+
"Ni ryari ugomba {}?",
|
| 8 |
+
"Iyo uhura na {}, ukora iki?",
|
| 9 |
+
"Ukoresha {} igihe utwaye imodoka ni byiza?",
|
| 10 |
+
"Ni gute wakwitwara igihe {}?"
|
| 11 |
+
]
|
| 12 |
+
|
| 13 |
+
ibimenyetso = [
|
| 14 |
+
"STOP", "Yield", "No Entry", "One Way", "Attention École",
|
| 15 |
+
"Roundabout", "No Parking", "No U-Turn", "Traffic Light", "Pedestrian Crossing"
|
| 16 |
+
]
|
| 17 |
+
|
| 18 |
+
ibikorwa = [
|
| 19 |
+
"uhagarara", "ugabanya umuvuduko", "uhindura umuhanda",
|
| 20 |
+
"uhagarika imodoka", "ucana amatara y’imodoka", "ukoresha seatbelt"
|
| 21 |
+
]
|
| 22 |
+
|
| 23 |
+
situasiyo = [
|
| 24 |
+
"umwana ari ku muhanda", "imodoka ikuvuye imbere", "umuderevu uri mu nzira y’abanyamaguru",
|
| 25 |
+
"amashanyarazi y’imodoka atangiye kugorana", "umuhanda uriho uruhurirane rw’imodoka"
|
| 26 |
+
]
|
| 27 |
+
|
| 28 |
+
ibisubizo_dict = {}
|
| 29 |
+
|
| 30 |
+
while len(ibisubizo_dict) < 200:
|
| 31 |
+
template = random.choice(templates)
|
| 32 |
+
if "{}" in template:
|
| 33 |
+
if "Icyapa" in template:
|
| 34 |
+
obj = random.choice(ibimenyetso)
|
| 35 |
+
elif "ugomba" in template or "ukora" in template:
|
| 36 |
+
obj = random.choice(ibikorwa + situasiyo)
|
| 37 |
+
else:
|
| 38 |
+
obj = random.choice(ibimenyetso + ibikorwa + situasiyo)
|
| 39 |
+
else:
|
| 40 |
+
obj = ""
|
| 41 |
+
|
| 42 |
+
ikibazo = template.format(obj)
|
| 43 |
+
|
| 44 |
+
if "STOP" in ikibazo:
|
| 45 |
+
igisubizo = "Ugomba guhagarara neza mbere yo gukomeza, urebe ko umuhanda utekanye."
|
| 46 |
+
elif "Yield" in ikibazo:
|
| 47 |
+
igisubizo = "Utegereza izindi modoka mbere yo gukomeza."
|
| 48 |
+
elif "No Entry" in ikibazo:
|
| 49 |
+
igisubizo = "Ntukinjire muri uwo muhanda."
|
| 50 |
+
elif "One Way" in ikibazo:
|
| 51 |
+
igisubizo = "Umuhanda uriho imodoka zinyura mu cyerekezo kimwe gusa."
|
| 52 |
+
elif "Attention École" in ikibazo:
|
| 53 |
+
igisubizo = "Kigaragaza ko hafi yaho hari ishuri, ugomba kugabanya umuvuduko no kwitonda."
|
| 54 |
+
elif "Roundabout" in ikibazo:
|
| 55 |
+
igisubizo = "Urazenguruka ku mizunguruko ugaha umuhanda abari imbere."
|
| 56 |
+
elif "No Parking" in ikibazo:
|
| 57 |
+
igisubizo = "Ntukabike imodoka muri icyo gice cy’umuhanda."
|
| 58 |
+
elif "Pedestrian" in ikibazo or "umwana" in ikibazo:
|
| 59 |
+
igisubizo = "Ugomba kugabanya umuvuduko kandi ube maso cyane."
|
| 60 |
+
elif "seatbelt" in ikibazo:
|
| 61 |
+
igisubizo = "Buri gihe ugomba kwambara seatbelt."
|
| 62 |
+
else:
|
| 63 |
+
igisubizo = "Icyo ni igikorwa cyiza cyo gukoresha mu mutekano w’umuhanda."
|
| 64 |
+
|
| 65 |
+
if ikibazo not in ibisubizo_dict:
|
| 66 |
+
ibisubizo_dict[ikibazo] = igisubizo
|
| 67 |
+
|
| 68 |
+
ibibazo_bya_quiz = list(ibisubizo_dict.items())
|
| 69 |
+
|
| 70 |
+
# --- FUNCTIONS ---
|
| 71 |
def gusubiza(ikibazo):
|
| 72 |
+
return ibisubizo_dict.get(ikibazo, "Nta bisubizo bihari kuri icyo kibazo.")
|
| 73 |
|
|
|
|
| 74 |
def gutegura_quiz():
|
| 75 |
quiz = random.sample(ibibazo_bya_quiz, min(5, len(ibibazo_bya_quiz)))
|
| 76 |
quiz_questions = []
|
| 77 |
quiz_choices = []
|
| 78 |
for q, ans in quiz:
|
| 79 |
+
other_answers = list(set(ibisubizo_dict.values()) - {ans})
|
| 80 |
choices = random.sample(other_answers, min(2, len(other_answers))) + [ans]
|
| 81 |
random.shuffle(choices)
|
| 82 |
quiz_questions.append(q)
|
| 83 |
quiz_choices.append(choices)
|
| 84 |
return quiz_questions, quiz_choices
|
| 85 |
|
|
|
|
| 86 |
def gusuzuma_quiz(*user_answers_and_questions):
|
| 87 |
user_answers = user_answers_and_questions[:-1]
|
| 88 |
quiz_questions = user_answers_and_questions[-1]
|
| 89 |
score = 0
|
| 90 |
for i, ans in enumerate(user_answers):
|
| 91 |
+
correct = ibisubizo_dict[quiz_questions[i]]
|
| 92 |
if ans == correct:
|
| 93 |
score += 1
|
| 94 |
return f"Watoye neza {score}/{len(user_answers)}"
|
| 95 |
|
| 96 |
+
# --- GRADIO INTERFACE ---
|
| 97 |
with gr.Blocks(theme=gr.themes.Soft(primary_hue="green", secondary_hue="yellow")) as demo:
|
| 98 |
gr.Markdown(
|
| 99 |
"""
|
| 100 |
+
# 🚗 AI y’Amategeko y’Umuhanda 🇷🇼
|
| 101 |
### *Yakozwe na Viateur Irasubiza*
|
| 102 |
---
|
| 103 |
+
Menya amategeko y’umuhanda mu buryo bworoshye, mu Kinyarwanda.
|
|
|
|
| 104 |
"""
|
| 105 |
)
|
| 106 |
|
|
|
|
| 108 |
with gr.Row():
|
| 109 |
with gr.Column(scale=1):
|
| 110 |
ikibazo = gr.Dropdown(
|
| 111 |
+
choices=list(ibisubizo_dict.keys()),
|
| 112 |
+
label="➡️ Hitamo ikibazo",
|
| 113 |
+
value=list(ibisubizo_dict.keys())[0]
|
| 114 |
)
|
| 115 |
kanda = gr.Button("🔍 Saba igisubizo")
|
| 116 |
with gr.Column(scale=1):
|
| 117 |
+
igisubizo = gr.Textbox(label="🧠 Igisubizo", lines=4)
|
| 118 |
|
| 119 |
kanda.click(fn=gusubiza, inputs=ikibazo, outputs=igisubizo)
|
| 120 |
|
|
|
|
|
|
|
|
|
|
| 121 |
gr.Markdown("## 📝 Quiz: Menya ibyo wize")
|
| 122 |
quiz_button = gr.Button("Tangira Quiz")
|
| 123 |
quiz_output = gr.Textbox(label="📝 Ibisubizo bya Quiz")
|
| 124 |
|
|
|
|
| 125 |
quiz_questions_state = gr.State()
|
| 126 |
quiz_choices_state = gr.State()
|
|
|
|
|
|
|
| 127 |
radio_widgets = []
|
| 128 |
for i in range(5):
|
| 129 |
r = gr.Radio(choices=[], label=f"Ikibazo {i+1}", visible=False)
|
| 130 |
radio_widgets.append(r)
|
| 131 |
+
|
|
|
|
| 132 |
def start_quiz():
|
| 133 |
questions, choices = gutegura_quiz()
|
| 134 |
outputs = []
|
|
|
|
| 142 |
outputs.append(r)
|
| 143 |
return questions, choices, *outputs
|
| 144 |
|
| 145 |
+
quiz_button.click(fn=start_quiz, inputs=[], outputs=[quiz_questions_state, quiz_choices_state] + radio_widgets)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 146 |
|
|
|
|
| 147 |
submit_btn = gr.Button("💯 Subiza Quiz")
|
| 148 |
|
| 149 |
def submit_quiz(*answers):
|
|
|
|
| 150 |
questions = quiz_questions_state.value
|
| 151 |
return gusuzuma_quiz(*answers, questions)
|
| 152 |
|
| 153 |
submit_btn.click(fn=submit_quiz, inputs=radio_widgets, outputs=quiz_output)
|
| 154 |
|
| 155 |
+
gr.Markdown("**© 2025 Viateur Irasubiza** 🛣️ AI ifasha Abanyarwanda kwiga amategeko y’umuhanda mu buryo bworoshye n’igihe gito.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 156 |
|
| 157 |
demo.launch()
|