KrishnaHuYaar commited on
Commit
723fa63
Β·
verified Β·
1 Parent(s): 9f19b12

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -84
app.py CHANGED
@@ -1,113 +1,86 @@
1
- import gradio as gr
2
-
3
- def string_converter(text):
4
- if not text:
5
- return "", "", "", "", 0, 0
6
-
7
- return (
8
- text.upper(),
9
- text.lower(),
10
- text.title(),
11
- text[::-1],
12
- len(text),
13
- len(text.split())
 
 
 
 
14
  )
 
 
 
 
 
 
15
 
16
  custom_css = """
17
- body {
18
- background-color: #f8fafc;
19
- }
20
  .gradio-container {
21
- max-width: 900px !important;
22
  }
23
- .output-box textarea {
24
- background-color: #f1f5f9 !important;
25
- }
26
- h1, h2 {
27
- text-align: center;
28
  }
29
  """
30
 
31
  with gr.Blocks(
32
- title="String Converter App",
33
  css=custom_css,
34
  theme=gr.themes.Soft()
35
  ) as app:
36
 
37
- gr.Markdown("# πŸ”€ String Converter")
38
- gr.Markdown(
39
- "A clean and interactive tool to transform text instantly."
40
- )
41
 
42
  with gr.Group():
43
- input_text = gr.Textbox(
44
- label="Input Text",
45
- placeholder="Type or paste your text here...",
46
- lines=4
47
  )
48
 
49
- with gr.Row():
50
- convert_btn = gr.Button("Convert", variant="primary")
51
- clear_btn = gr.Button("Clear", variant="secondary")
52
 
53
- with gr.Group():
54
- gr.Markdown("### πŸ”„ Converted Output")
 
 
 
55
 
56
- with gr.Row():
57
- upper_out = gr.Textbox(label="Uppercase", interactive=False)
58
- lower_out = gr.Textbox(label="Lowercase", interactive=False)
59
 
60
- with gr.Row():
61
- title_out = gr.Textbox(label="Title Case", interactive=False)
62
- reverse_out = gr.Textbox(label="Reversed String", interactive=False)
63
 
64
- with gr.Group():
65
- gr.Markdown("### πŸ“Š Text Statistics")
66
-
67
- with gr.Row():
68
- char_count_out = gr.Number(label="Character Count", interactive=False)
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
- # Live update while typing (UX upgrade)
86
- input_text.change(
87
- fn=string_converter,
88
- inputs=input_text,
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
- # Clear button logic
100
- clear_btn.click(
101
- fn=lambda: ("", "", "", "", 0, 0),
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__":