Update app.py
Browse files
app.py
CHANGED
|
@@ -1,113 +1,86 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
custom_css = """
|
| 17 |
-
body {
|
| 18 |
-
background-color: #f8fafc;
|
| 19 |
-
}
|
| 20 |
.gradio-container {
|
| 21 |
-
max-width:
|
| 22 |
}
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
}
|
| 26 |
-
h1, h2 {
|
| 27 |
-
text-align: center;
|
| 28 |
}
|
| 29 |
"""
|
| 30 |
|
| 31 |
with gr.Blocks(
|
| 32 |
-
title="
|
| 33 |
css=custom_css,
|
| 34 |
theme=gr.themes.Soft()
|
| 35 |
) as app:
|
| 36 |
|
| 37 |
-
gr.Markdown("#
|
| 38 |
-
gr.Markdown(
|
| 39 |
-
"A clean and interactive tool to transform text instantly."
|
| 40 |
-
)
|
| 41 |
|
| 42 |
with gr.Group():
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
)
|
| 48 |
|
| 49 |
-
|
| 50 |
-
convert_btn = gr.Button("Convert", variant="primary")
|
| 51 |
-
clear_btn = gr.Button("Clear", variant="secondary")
|
| 52 |
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
|
| 60 |
-
|
| 61 |
-
title_out = gr.Textbox(label="Title Case", interactive=False)
|
| 62 |
-
reverse_out = gr.Textbox(label="Reversed String", interactive=False)
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
word_count_out = gr.Number(label="Word Count", interactive=False)
|
| 70 |
-
|
| 71 |
-
# Button click
|
| 72 |
-
convert_btn.click(
|
| 73 |
-
fn=string_converter,
|
| 74 |
-
inputs=input_text,
|
| 75 |
-
outputs=[
|
| 76 |
-
upper_out,
|
| 77 |
-
lower_out,
|
| 78 |
-
title_out,
|
| 79 |
-
reverse_out,
|
| 80 |
-
char_count_out,
|
| 81 |
-
word_count_out
|
| 82 |
-
]
|
| 83 |
)
|
| 84 |
|
| 85 |
-
#
|
| 86 |
-
|
| 87 |
-
fn=
|
| 88 |
-
inputs=
|
| 89 |
-
outputs=[
|
| 90 |
-
upper_out,
|
| 91 |
-
lower_out,
|
| 92 |
-
title_out,
|
| 93 |
-
reverse_out,
|
| 94 |
-
char_count_out,
|
| 95 |
-
word_count_out
|
| 96 |
-
]
|
| 97 |
)
|
| 98 |
|
| 99 |
-
#
|
| 100 |
-
|
| 101 |
-
fn=lambda: ("",
|
| 102 |
inputs=None,
|
| 103 |
-
outputs=[
|
| 104 |
-
upper_out,
|
| 105 |
-
lower_out,
|
| 106 |
-
title_out,
|
| 107 |
-
reverse_out,
|
| 108 |
-
char_count_out,
|
| 109 |
-
word_count_out
|
| 110 |
-
]
|
| 111 |
)
|
| 112 |
|
| 113 |
if __name__ == "__main__":
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import time
|
| 3 |
+
|
| 4 |
+
SAMPLE_TEXT = "The quick brown fox jumps over the lazy dog."
|
| 5 |
+
|
| 6 |
+
def typing_stats(user_input, start_time):
|
| 7 |
+
if not user_input:
|
| 8 |
+
return 0, 0, start_time
|
| 9 |
+
|
| 10 |
+
elapsed = time.time() - start_time
|
| 11 |
+
minutes = elapsed / 60 if elapsed > 0 else 1/60
|
| 12 |
+
|
| 13 |
+
char_count = len(user_input)
|
| 14 |
+
wpm = (char_count / 5) / minutes
|
| 15 |
+
|
| 16 |
+
correct_chars = sum(
|
| 17 |
+
1 for u, s in zip(user_input, SAMPLE_TEXT) if u == s
|
| 18 |
)
|
| 19 |
+
accuracy = (correct_chars / max(len(SAMPLE_TEXT), 1)) * 100
|
| 20 |
+
|
| 21 |
+
return round(wpm, 2), round(accuracy, 2), start_time
|
| 22 |
+
|
| 23 |
+
def start_timer():
|
| 24 |
+
return time.time()
|
| 25 |
|
| 26 |
custom_css = """
|
|
|
|
|
|
|
|
|
|
| 27 |
.gradio-container {
|
| 28 |
+
max-width: 700px !important;
|
| 29 |
}
|
| 30 |
+
textarea {
|
| 31 |
+
font-size: 16px !important;
|
|
|
|
|
|
|
|
|
|
| 32 |
}
|
| 33 |
"""
|
| 34 |
|
| 35 |
with gr.Blocks(
|
| 36 |
+
title="Light Typer App",
|
| 37 |
css=custom_css,
|
| 38 |
theme=gr.themes.Soft()
|
| 39 |
) as app:
|
| 40 |
|
| 41 |
+
gr.Markdown("# β¨οΈ Light Typer")
|
| 42 |
+
gr.Markdown("Practice typing and track your speed & accuracy.")
|
|
|
|
|
|
|
| 43 |
|
| 44 |
with gr.Group():
|
| 45 |
+
gr.Markdown("### π Type the text below")
|
| 46 |
+
reference_text = gr.Textbox(
|
| 47 |
+
value=SAMPLE_TEXT,
|
| 48 |
+
interactive=False
|
| 49 |
)
|
| 50 |
|
| 51 |
+
start_time = gr.State(0.0)
|
|
|
|
|
|
|
| 52 |
|
| 53 |
+
user_input = gr.Textbox(
|
| 54 |
+
label="Start Typing",
|
| 55 |
+
placeholder="Start typing here...",
|
| 56 |
+
lines=3
|
| 57 |
+
)
|
| 58 |
|
| 59 |
+
with gr.Row():
|
| 60 |
+
wpm_out = gr.Number(label="WPM", interactive=False)
|
| 61 |
+
accuracy_out = gr.Number(label="Accuracy (%)", interactive=False)
|
| 62 |
|
| 63 |
+
reset_btn = gr.Button("Reset", variant="secondary")
|
|
|
|
|
|
|
| 64 |
|
| 65 |
+
# Start timer on first input
|
| 66 |
+
user_input.focus(
|
| 67 |
+
fn=start_timer,
|
| 68 |
+
inputs=None,
|
| 69 |
+
outputs=start_time
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
)
|
| 71 |
|
| 72 |
+
# Update stats live
|
| 73 |
+
user_input.change(
|
| 74 |
+
fn=typing_stats,
|
| 75 |
+
inputs=[user_input, start_time],
|
| 76 |
+
outputs=[wpm_out, accuracy_out, start_time]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
)
|
| 78 |
|
| 79 |
+
# Reset
|
| 80 |
+
reset_btn.click(
|
| 81 |
+
fn=lambda: ("", 0, 0, 0.0),
|
| 82 |
inputs=None,
|
| 83 |
+
outputs=[user_input, wpm_out, accuracy_out, start_time]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
)
|
| 85 |
|
| 86 |
if __name__ == "__main__":
|