aashraychegu commited on
Commit
2b1edbe
·
1 Parent(s): 8e7d008

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -44
app.py CHANGED
@@ -4,14 +4,10 @@ from app_vars import *
4
  from nltk import edit_distance
5
  import gradio as g
6
 
7
- global name, url, correct, wrong, previous_correctness, skipped, prevurl, coft, qnum
8
- name, url = get_rock_name_url()
9
- prevurl = ""
10
- correct = 0
11
- wrong = 0
12
- skipped = 0
13
- coft = 0
14
- qnum = 0
15
 
16
 
17
  def check_2_strings(a: str, b: str):
@@ -26,78 +22,103 @@ def get_name(
26
  ans: str,
27
  feedback: str,
28
  ):
29
- global name, url, correct, wrong, skipped, prevurl, coft, qnum
30
  out = ""
31
  fb = ""
32
  stats = ""
33
- qnum += 1
34
  if len(ans) >= 1:
35
  ans = ans.strip()
36
- deviance = check_2_strings(ans, name)
37
  if ans in ["s", "/", "skip", "n", "r"]:
38
- fb += "# The answer is: " + name
39
- skipped += 1
40
- prevurl = url
41
  elif deviance <= 26:
42
- fb += "## Correct! - The answer is: " + name
43
 
44
- # print("Before:", url, prevurl, url == prevurl)
45
- if prevurl != url:
46
- coft += 1
47
- # print("after:", url, prevurl, url == prevurl)
48
- name, url = get_rock_name_url()
49
- correct += 1
50
- # print("after2:", url, prevurl, url == prevurl, "\n\n\n")
51
  else:
52
- wrong += 1
53
- fb += f"## Wrong! The answer isn't '{ans}'.\n # Press s, r, or n to see the answer."
54
- prevurl = url
55
 
56
- stats = f" ### Question {qnum}: \n {fb} \n ## Correct: {correct} | Wrong: {wrong} | Correct on first try : {coft} | Skipped: {skipped} | Total: {correct + wrong + skipped} \n Your current deviance from the correct answer is: {deviance}"
 
 
 
 
 
 
 
 
57
  out = ""
58
  else:
59
  out = ans
60
  stats = feedback
61
  return (
62
- url,
63
  out,
 
64
  stats,
65
- f"""### [Click here to research the rock](https://www.mindat.org/search.php?search='{name}')""",
66
  )
67
 
68
 
69
  with g.Blocks() as A:
70
  title = g.Markdown("# Practice Rocks and Minerals. By Aashray")
 
 
 
 
 
 
 
 
71
  with g.Row() as row:
72
- with g.Column(min_width = 1000, scale = 5):
73
- img = g.Image(
74
- url,
 
 
 
75
  type="filepath",
76
- tool="select"
77
  )
78
  with g.Column(variant="panel"):
79
- with g.Group():
80
- inp = g.Textbox(
81
- placeholder="Enter the rock name",
82
- show_label=False,
83
- max_lines=1,
84
- )
85
  feedback = g.Markdown(
86
- "### Type in the name of the rock and press enter to get started."
 
 
 
 
 
 
 
 
 
 
 
87
  )
88
- l = g.Markdown(
89
- f"""\n## [**Click here to research the rock**](https://www.mindat.org/search.php?search='{name}')"""
90
  )
91
  inp.submit(
92
  fn=get_name,
93
- inputs=[inp, feedback],
94
  outputs=[
95
  img,
96
  inp,
97
  feedback,
98
- l,
 
99
  ],
100
- scroll_to_output=True,
101
  )
102
 
103
  A.launch(
 
4
  from nltk import edit_distance
5
  import gradio as g
6
 
7
+ state = g.State(
8
+ {a: b for a, b in (zip(["name", "url"], get_rock_name_url()))}
9
+ | dict(correct=0, wrong=0, skipped=0, prevurl="", coft=0, qnum=1)
10
+ )
 
 
 
 
11
 
12
 
13
  def check_2_strings(a: str, b: str):
 
22
  ans: str,
23
  feedback: str,
24
  ):
25
+ # global state.value["name"], state.value["url"], state.value["correct"], state.value["wrong"], state.value["skipped"], state.value["prevurl"], state.value["coft"], state.value["qnum"]
26
  out = ""
27
  fb = ""
28
  stats = ""
29
+ state.value["qnum"] += 1
30
  if len(ans) >= 1:
31
  ans = ans.strip()
32
+ deviance = check_2_strings(ans, state.value["name"])
33
  if ans in ["s", "/", "skip", "n", "r"]:
34
+ fb += "The answer is: " + state.value["name"]
35
+ state.value["skipped"] += 1
36
+ state.value["prevurl"] = state.value["url"]
37
  elif deviance <= 26:
38
+ fb += f"""Correct! - The answer is: {state.value["name"]}"""
39
 
40
+ # print("Before:", state.value["url"], state.value["prevurl"], state.value["url"] == state.value["prevurl"])
41
+ if state.value["prevurl"] != state.value["url"]:
42
+ state.value["coft"] += 1
43
+ # print("after:", state.value["url"], state.value["prevurl"], state.value["url"] == state.value["prevurl"])
44
+ state.value["name"], state.value["url"] = get_rock_name_url()
45
+ state.value["correct"] += 1
46
+ # print("after2:", state.value["url"], state.value["prevurl"], state.value["url"] == state.value["prevurl"], "\n\n\n")
47
  else:
48
+ state.value["wrong"] += 1
49
+ fb += f"\n### Wrong! The answer isn't {ans}. \n ### Press s, r, or n to see the answer."
50
+ state.value["prevurl"] = state.value["url"]
51
 
52
+ stats = f"""
53
+ Statistic | Value
54
+ ---: | :---
55
+ Correct | {state.value["correct"]}
56
+ Wrong | {state.value["wrong"]}
57
+ Correct on first try | {state.value["coft"]}
58
+ Skipped | {state.value["skipped"]}
59
+ Total | {state.value["correct"] + state.value["wrong"] + state.value["skipped"]}
60
+ """
61
  out = ""
62
  else:
63
  out = ans
64
  stats = feedback
65
  return (
66
+ state.value["url"],
67
  out,
68
+ f"""## Question {state.value["qnum"]}:\n ### {fb}""",
69
  stats,
70
+ rf"""### [Click here to research the rock](https://www.mindat.org/search.php?search={state.value["name"].strip().replace(" ", "%20")})""",
71
  )
72
 
73
 
74
  with g.Blocks() as A:
75
  title = g.Markdown("# Practice Rocks and Minerals. By Aashray")
76
+ with g.Row():
77
+ inp = g.Textbox(
78
+ placeholder="""Enter the rock/mineral's name here. Press s, r, or n to skip the question.""",
79
+ show_label=False,
80
+ max_lines=1,
81
+ autofocus=True,
82
+ autoscroll=True,
83
+ )
84
  with g.Row() as row:
85
+ with g.Column(
86
+ scale=7,
87
+ min_width=1000,
88
+ ):
89
+ img = g.ImageEditor(
90
+ state.value["url"],
91
  type="filepath",
92
+ sources=[],
93
  )
94
  with g.Column(variant="panel"):
 
 
 
 
 
 
95
  feedback = g.Markdown(
96
+ f"""### Question {state.value["qnum"]}: Type in the name of the rock/mineral to get started!"""
97
+ )
98
+ stats = g.Markdown(
99
+ f"""
100
+ Statistic | Value
101
+ ---: | :---
102
+ Correct | {state.value["correct"]}
103
+ Wrong | {state.value["wrong"]}
104
+ Correct on first try | {state.value["coft"]}
105
+ Skipped | {state.value["skipped"]}
106
+ Total | {state.value["correct"] + state.value["wrong"] + state.value["skipped"]}
107
+ """
108
  )
109
+ link = g.Markdown(
110
+ f"""### [Click here to research the rock](https://www.mindat.org/search.php?search={state.value["name"].strip().replace(" ", "%20")})"""
111
  )
112
  inp.submit(
113
  fn=get_name,
114
+ inputs=[inp, stats],
115
  outputs=[
116
  img,
117
  inp,
118
  feedback,
119
+ stats,
120
+ link,
121
  ],
 
122
  )
123
 
124
  A.launch(