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