Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +39 -35
- requirements.txt +1 -0
app.py
CHANGED
|
@@ -1,7 +1,5 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import random
|
| 3 |
|
| 4 |
-
# Preguntes tipus test
|
| 5 |
preguntes = [
|
| 6 |
{
|
| 7 |
"pregunta": "Qui és l'autor de *Tirant lo Blanc*?",
|
|
@@ -23,54 +21,60 @@ preguntes = [
|
|
| 23 |
}
|
| 24 |
]
|
| 25 |
|
| 26 |
-
|
| 27 |
-
"index"
|
| 28 |
-
|
| 29 |
-
"incorrectes": 0,
|
| 30 |
-
"errors": []
|
| 31 |
-
}
|
| 32 |
|
| 33 |
-
|
| 34 |
-
def xat(resposta):
|
| 35 |
-
if estat["index"] >= len(preguntes):
|
| 36 |
-
return "Ja has acabat el test. Torna a carregar per començar de nou."
|
| 37 |
-
|
| 38 |
-
pregunta_actual = preguntes[estat["index"]]
|
| 39 |
correcte = pregunta_actual["correcta"]
|
| 40 |
|
| 41 |
if resposta == correcte:
|
| 42 |
-
|
| 43 |
resposta_text = f"✅ Correcte! Bé fet.\n\n"
|
| 44 |
else:
|
| 45 |
-
|
| 46 |
-
|
| 47 |
resposta_text = f"❌ Incorrecte. La resposta correcta era **{correcte}**.\n{pregunta_actual['explicacio']}\n\n"
|
| 48 |
|
| 49 |
-
|
| 50 |
|
| 51 |
-
if
|
| 52 |
-
seg_pregunta = preguntes[
|
| 53 |
opcions_text = "\n".join([f"- {opt}" for opt in seg_pregunta["opcions"]])
|
| 54 |
-
return resposta_text + f"**Pregunta {
|
| 55 |
else:
|
| 56 |
-
resum = f"\n🏁 Has acabat el test!\n✔ Correctes: {
|
| 57 |
-
if
|
| 58 |
resum += "\n🔍 Temes a repassar:\n"
|
| 59 |
-
for e in
|
| 60 |
resum += f"- {e['pregunta']}: {e['explicacio']}\n"
|
| 61 |
-
return resposta_text + resum
|
| 62 |
|
| 63 |
-
# Missatge inicial
|
| 64 |
def inici():
|
| 65 |
-
estat
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
|
|
|
|
|
|
| 69 |
primera_pregunta = preguntes[0]
|
| 70 |
opcions_text = "\n".join([f"- {opt}" for opt in primera_pregunta["opcions"]])
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
demo = gr.Interface(fn=xat, inputs="text", outputs="text", live=True)
|
| 74 |
-
demo.load(inici, None, "text")
|
| 75 |
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
|
|
|
|
| 3 |
preguntes = [
|
| 4 |
{
|
| 5 |
"pregunta": "Qui és l'autor de *Tirant lo Blanc*?",
|
|
|
|
| 21 |
}
|
| 22 |
]
|
| 23 |
|
| 24 |
+
def xat(resposta, state):
|
| 25 |
+
if state["index"] >= len(preguntes):
|
| 26 |
+
return "Ja has acabat el test. Torna a carregar per començar de nou.", state
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
+
pregunta_actual = preguntes[state["index"]]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
correcte = pregunta_actual["correcta"]
|
| 30 |
|
| 31 |
if resposta == correcte:
|
| 32 |
+
state["correctes"] += 1
|
| 33 |
resposta_text = f"✅ Correcte! Bé fet.\n\n"
|
| 34 |
else:
|
| 35 |
+
state["incorrectes"] += 1
|
| 36 |
+
state["errors"].append(pregunta_actual)
|
| 37 |
resposta_text = f"❌ Incorrecte. La resposta correcta era **{correcte}**.\n{pregunta_actual['explicacio']}\n\n"
|
| 38 |
|
| 39 |
+
state["index"] += 1
|
| 40 |
|
| 41 |
+
if state["index"] < len(preguntes):
|
| 42 |
+
seg_pregunta = preguntes[state["index"]]
|
| 43 |
opcions_text = "\n".join([f"- {opt}" for opt in seg_pregunta["opcions"]])
|
| 44 |
+
return resposta_text + f"**Pregunta {state['index'] + 1}:** {seg_pregunta['pregunta']}\n{opcions_text}", state
|
| 45 |
else:
|
| 46 |
+
resum = f"\n🏁 Has acabat el test!\n✔ Correctes: {state['correctes']}\n✖ Incorrectes: {state['incorrectes']}\n"
|
| 47 |
+
if state["errors"]:
|
| 48 |
resum += "\n🔍 Temes a repassar:\n"
|
| 49 |
+
for e in state["errors"]:
|
| 50 |
resum += f"- {e['pregunta']}: {e['explicacio']}\n"
|
| 51 |
+
return resposta_text + resum, state
|
| 52 |
|
|
|
|
| 53 |
def inici():
|
| 54 |
+
estat = {
|
| 55 |
+
"index": 0,
|
| 56 |
+
"correctes": 0,
|
| 57 |
+
"incorrectes": 0,
|
| 58 |
+
"errors": []
|
| 59 |
+
}
|
| 60 |
primera_pregunta = preguntes[0]
|
| 61 |
opcions_text = "\n".join([f"- {opt}" for opt in primera_pregunta["opcions"]])
|
| 62 |
+
missatge_inicial = f"🎓 Benvingut al test de literatura catalana!\n**Pregunta 1:** {primera_pregunta['pregunta']}\n{opcions_text}"
|
| 63 |
+
return missatge_inicial, estat
|
|
|
|
|
|
|
| 64 |
|
| 65 |
+
with gr.Blocks() as demo:
|
| 66 |
+
estat = gr.State({
|
| 67 |
+
"index": 0,
|
| 68 |
+
"correctes": 0,
|
| 69 |
+
"incorrectes": 0,
|
| 70 |
+
"errors": []
|
| 71 |
+
})
|
| 72 |
+
txt = gr.Textbox(label="Escriu la teva resposta aquí:")
|
| 73 |
+
out = gr.Textbox(label="Resultats i preguntes", lines=15)
|
| 74 |
+
|
| 75 |
+
btn = gr.Button("Comença")
|
| 76 |
+
|
| 77 |
+
btn.click(fn=inici, outputs=[out, estat])
|
| 78 |
+
txt.submit(fn=xat, inputs=[txt, estat], outputs=[out, estat])
|
| 79 |
+
|
| 80 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
gradio
|