Silapinto commited on
Commit
c0047b1
·
verified ·
1 Parent(s): 471a9f6

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +39 -35
  2. 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
- estat = {
27
- "index": 0,
28
- "correctes": 0,
29
- "incorrectes": 0,
30
- "errors": []
31
- }
32
 
33
- # Funció que processa les respostes
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
- estat["correctes"] += 1
43
  resposta_text = f"✅ Correcte! Bé fet.\n\n"
44
  else:
45
- estat["incorrectes"] += 1
46
- estat["errors"].append(pregunta_actual)
47
  resposta_text = f"❌ Incorrecte. La resposta correcta era **{correcte}**.\n{pregunta_actual['explicacio']}\n\n"
48
 
49
- estat["index"] += 1
50
 
51
- if estat["index"] < len(preguntes):
52
- seg_pregunta = preguntes[estat["index"]]
53
  opcions_text = "\n".join([f"- {opt}" for opt in seg_pregunta["opcions"]])
54
- return resposta_text + f"**Pregunta {estat['index'] + 1}:** {seg_pregunta['pregunta']}\n{opcions_text}"
55
  else:
56
- resum = f"\n🏁 Has acabat el test!\n✔ Correctes: {estat['correctes']}\n✖ Incorrectes: {estat['incorrectes']}\n"
57
- if estat["errors"]:
58
  resum += "\n🔍 Temes a repassar:\n"
59
- for e in estat["errors"]:
60
  resum += f"- {e['pregunta']}: {e['explicacio']}\n"
61
- return resposta_text + resum
62
 
63
- # Missatge inicial
64
  def inici():
65
- estat["index"] = 0
66
- estat["correctes"] = 0
67
- estat["incorrectes"] = 0
68
- estat["errors"] = []
 
 
69
  primera_pregunta = preguntes[0]
70
  opcions_text = "\n".join([f"- {opt}" for opt in primera_pregunta["opcions"]])
71
- return f"🎓 Benvingut al test de literatura catalana!\n**Pregunta 1:** {primera_pregunta['pregunta']}\n{opcions_text}"
72
-
73
- demo = gr.Interface(fn=xat, inputs="text", outputs="text", live=True)
74
- demo.load(inici, None, "text")
75
 
76
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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