akhaliq HF Staff commited on
Commit
dcb6f38
·
verified ·
1 Parent(s): 716b385

Update app.py from anycoder

Browse files
Files changed (1) hide show
  1. app.py +191 -123
app.py CHANGED
@@ -1,201 +1,269 @@
1
  import gradio as gr
2
 
3
- def calculator(num1, operation, num2):
4
  """Perform basic arithmetic operations."""
5
- if operation == "➕ Add":
6
- return num1 + num2
7
- elif operation == "➖ Subtract":
8
- return num1 - num2
9
- elif operation == "✖️ Multiply":
10
- return num1 * num2
11
- elif operation == "➗ Divide":
12
- if num2 == 0:
13
- raise gr.Error("Cannot divide by zero!")
14
- return num1 / num2
15
-
16
- # Custom CSS for modern, mobile-friendly design
 
 
 
17
  custom_css = """
18
- /* Container and layout */
19
  .gradio-container {
20
- max-width: 500px !important;
21
  margin: 0 auto !important;
 
22
  }
23
 
24
- /* Header styling */
25
- .header-title {
26
  text-align: center;
27
- font-size: 2.5rem !important;
 
 
 
 
28
  font-weight: 700 !important;
29
  margin-bottom: 0.5rem !important;
30
- background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
31
- -webkit-background-clip: text;
32
- -webkit-text-fill-color: transparent;
33
- background-clip: text;
34
  }
35
 
36
- .header-subtitle {
37
- text-align: center;
38
- font-size: 0.95rem !important;
39
  color: #64748b !important;
40
- margin-bottom: 2rem !important;
41
  }
42
 
43
- /* Input fields */
44
- .input-group {
45
- margin-bottom: 1rem !important;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
  }
47
 
48
  /* Result display */
49
- .result-display {
50
  background: linear-gradient(135deg, #667eea15 0%, #764ba215 100%) !important;
51
- border-radius: 12px !important;
52
  padding: 1.5rem !important;
53
- margin-top: 1rem !important;
 
54
  }
55
 
56
- /* Button styling */
57
- .calculate-btn button {
58
- background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important;
59
- border: none !important;
60
  font-weight: 600 !important;
61
- font-size: 1.1rem !important;
62
- padding: 0.75rem 2rem !important;
63
- transition: transform 0.2s ease, box-shadow 0.2s ease !important;
64
  }
65
 
66
- .calculate-btn button:hover {
67
- transform: translateY(-2px) !important;
68
- box-shadow: 0 8px 16px rgba(102, 126, 234, 0.3) !important;
 
 
 
 
 
 
 
 
 
 
 
69
  }
70
 
71
- /* Examples section */
72
  .examples-title {
73
- font-size: 0.9rem !important;
74
  font-weight: 600 !important;
75
- color: #475569 !important;
76
- margin-top: 1.5rem !important;
77
- margin-bottom: 0.75rem !important;
78
  }
79
 
80
- /* Mobile responsiveness */
81
- @media (max-width: 640px) {
82
- .header-title {
83
- font-size: 2rem !important;
84
  }
85
 
86
- .gradio-container {
87
- padding: 1rem !important;
88
  }
89
 
90
- .calculate-btn button {
91
- width: 100% !important;
92
- font-size: 1rem !important;
 
 
 
 
 
 
 
 
93
  }
94
  }
95
 
96
- /* Radio button styling */
97
- .radio-group label {
98
- font-size: 1rem !important;
99
- padding: 0.5rem !important;
100
- }
101
-
102
- /* Number input styling */
103
- input[type="number"] {
104
- font-size: 1.1rem !important;
105
- padding: 0.75rem !important;
106
  }
107
  """
108
 
109
- # Build the Gradio app
110
- with gr.Blocks(fill_height=False) as demo:
111
  # Header
112
- gr.Markdown("# 🧮 Calculator", elem_classes="header-title")
113
- gr.Markdown(
114
- "Simple & elegant calculator built with [anycoder](https://huggingface.co/spaces/akhaliq/anycoder)",
115
- elem_classes="header-subtitle"
116
- )
 
117
 
118
- # Input section
119
- with gr.Group():
 
120
  num1 = gr.Number(
121
  label="First Number",
122
- placeholder="0",
123
  value=0,
124
- elem_classes="input-group"
125
  )
126
 
 
127
  operation = gr.Radio(
128
- choices=["➕ Add", "➖ Subtract", "✖️ Multiply", "➗ Divide"],
129
- value="➕ Add",
 
 
 
 
 
130
  label="Operation",
131
- elem_classes="input-group"
132
  )
133
 
 
134
  num2 = gr.Number(
135
  label="Second Number",
136
- placeholder="0",
137
  value=0,
138
- elem_classes="input-group"
139
  )
140
-
141
- # Calculate button
142
- calculate_btn = gr.Button(
143
- "Calculate",
144
- variant="primary",
145
- size="lg",
146
- elem_classes="calculate-btn"
147
- )
148
-
149
- # Result section
150
- with gr.Group(elem_classes="result-display"):
151
  result = gr.Number(
152
  label="Result",
153
  interactive=False,
154
- show_label=True
155
  )
156
 
157
  # Examples section
158
- gr.Markdown("**Quick Examples**", elem_classes="examples-title")
159
- gr.Examples(
160
- examples=[
161
- [45, "➕ Add", 3],
162
- [100, "➖ Subtract", 25],
163
- [12, "✖️ Multiply", 8],
164
- [144, "➗ Divide", 12],
165
- ],
166
- inputs=[num1, operation, num2],
167
- outputs=[result],
168
- fn=calculator,
169
- cache_examples=False
170
- )
171
-
172
- # Event handlers
173
- calculate_btn.click(
174
- fn=calculator,
175
- inputs=[num1, operation, num2],
176
- outputs=[result],
177
- api_visibility="public"
178
- )
179
 
180
- # Also calculate on Enter key
181
- num1.submit(calculator, [num1, operation, num2], result)
182
- num2.submit(calculator, [num1, operation, num2], result)
 
 
 
 
 
183
 
184
  if __name__ == "__main__":
185
  demo.launch(
186
  theme=gr.themes.Soft(
187
  primary_hue="indigo",
188
- secondary_hue="purple",
189
  neutral_hue="slate",
190
  font=gr.themes.GoogleFont("Inter"),
191
- text_size="lg",
192
  spacing_size="md",
193
  radius_size="lg"
194
  ).set(
195
- button_primary_background_fill="linear-gradient(135deg, #667eea 0%, #764ba2 100%)",
196
- button_primary_background_fill_hover="linear-gradient(135deg, #5568d3 0%, #6a4291 100%)",
197
  block_title_text_weight="600",
198
- block_label_text_weight="500",
 
 
 
199
  ),
200
  css=custom_css,
201
  footer_links=[
 
1
  import gradio as gr
2
 
3
+ def calculate(num1, operation, num2):
4
  """Perform basic arithmetic operations."""
5
+ try:
6
+ if operation == "add":
7
+ return num1 + num2
8
+ elif operation == "subtract":
9
+ return num1 - num2
10
+ elif operation == "multiply":
11
+ return num1 * num2
12
+ elif operation == "divide":
13
+ if num2 == 0:
14
+ raise gr.Error("Cannot divide by zero!")
15
+ return num1 / num2
16
+ except Exception as e:
17
+ raise gr.Error(f"Calculation error: {str(e)}")
18
+
19
+ # Modern, minimal CSS
20
  custom_css = """
21
+ /* Global container */
22
  .gradio-container {
23
+ max-width: 420px !important;
24
  margin: 0 auto !important;
25
+ padding: 1.5rem 1rem !important;
26
  }
27
 
28
+ /* Header */
29
+ .calc-header {
30
  text-align: center;
31
+ margin-bottom: 2rem !important;
32
+ }
33
+
34
+ .calc-title {
35
+ font-size: 2rem !important;
36
  font-weight: 700 !important;
37
  margin-bottom: 0.5rem !important;
38
+ color: #1e293b !important;
 
 
 
39
  }
40
 
41
+ .calc-subtitle {
42
+ font-size: 0.875rem !important;
 
43
  color: #64748b !important;
 
44
  }
45
 
46
+ /* Calculator card */
47
+ .calc-card {
48
+ background: #ffffff !important;
49
+ border-radius: 20px !important;
50
+ padding: 1.5rem !important;
51
+ box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06) !important;
52
+ }
53
+
54
+ /* Input styling */
55
+ .calc-input label {
56
+ font-size: 0.875rem !important;
57
+ font-weight: 600 !important;
58
+ color: #475569 !important;
59
+ margin-bottom: 0.5rem !important;
60
+ }
61
+
62
+ .calc-input input {
63
+ font-size: 1.25rem !important;
64
+ text-align: center !important;
65
+ border-radius: 12px !important;
66
+ border: 2px solid #e2e8f0 !important;
67
+ transition: all 0.2s ease !important;
68
+ }
69
+
70
+ .calc-input input:focus {
71
+ border-color: #6366f1 !important;
72
+ box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.1) !important;
73
+ }
74
+
75
+ /* Operation buttons */
76
+ .operation-btns {
77
+ display: grid !important;
78
+ grid-template-columns: repeat(4, 1fr) !important;
79
+ gap: 0.75rem !important;
80
+ margin: 1.5rem 0 !important;
81
+ }
82
+
83
+ .operation-btns button {
84
+ height: 60px !important;
85
+ font-size: 1.5rem !important;
86
+ border-radius: 12px !important;
87
+ border: 2px solid #e2e8f0 !important;
88
+ background: #ffffff !important;
89
+ color: #475569 !important;
90
+ font-weight: 600 !important;
91
+ transition: all 0.2s ease !important;
92
+ cursor: pointer !important;
93
+ }
94
+
95
+ .operation-btns button:hover {
96
+ background: #f8fafc !important;
97
+ border-color: #6366f1 !important;
98
+ transform: translateY(-2px) !important;
99
+ }
100
+
101
+ .operation-btns button.selected {
102
+ background: #6366f1 !important;
103
+ color: white !important;
104
+ border-color: #6366f1 !important;
105
  }
106
 
107
  /* Result display */
108
+ .result-box {
109
  background: linear-gradient(135deg, #667eea15 0%, #764ba215 100%) !important;
110
+ border-radius: 16px !important;
111
  padding: 1.5rem !important;
112
+ margin-top: 1.5rem !important;
113
+ border: 2px solid #e0e7ff !important;
114
  }
115
 
116
+ .result-box label {
117
+ font-size: 0.875rem !important;
 
 
118
  font-weight: 600 !important;
119
+ color: #4338ca !important;
120
+ text-transform: uppercase !important;
121
+ letter-spacing: 0.05em !important;
122
  }
123
 
124
+ .result-box input {
125
+ font-size: 2rem !important;
126
+ font-weight: 700 !important;
127
+ text-align: center !important;
128
+ color: #4338ca !important;
129
+ background: transparent !important;
130
+ border: none !important;
131
+ }
132
+
133
+ /* Examples */
134
+ .examples-section {
135
+ margin-top: 2rem !important;
136
+ padding-top: 1.5rem !important;
137
+ border-top: 1px solid #e2e8f0 !important;
138
  }
139
 
 
140
  .examples-title {
141
+ font-size: 0.875rem !important;
142
  font-weight: 600 !important;
143
+ color: #64748b !important;
144
+ text-align: center !important;
145
+ margin-bottom: 1rem !important;
146
  }
147
 
148
+ /* Mobile optimization */
149
+ @media (max-width: 480px) {
150
+ .gradio-container {
151
+ padding: 1rem 0.75rem !important;
152
  }
153
 
154
+ .calc-title {
155
+ font-size: 1.75rem !important;
156
  }
157
 
158
+ .calc-card {
159
+ padding: 1.25rem !important;
160
+ }
161
+
162
+ .operation-btns button {
163
+ height: 55px !important;
164
+ font-size: 1.25rem !important;
165
+ }
166
+
167
+ .result-box input {
168
+ font-size: 1.75rem !important;
169
  }
170
  }
171
 
172
+ /* Footer link styling */
173
+ footer a {
174
+ color: #6366f1 !important;
175
+ font-weight: 600 !important;
 
 
 
 
 
 
176
  }
177
  """
178
 
179
+ # Build the app with minimal components
180
+ with gr.Blocks() as demo:
181
  # Header
182
+ with gr.Group(elem_classes="calc-header"):
183
+ gr.Markdown("# 🧮 Calculator", elem_classes="calc-title")
184
+ gr.Markdown(
185
+ "Built with [anycoder](https://huggingface.co/spaces/akhaliq/anycoder)",
186
+ elem_classes="calc-subtitle"
187
+ )
188
 
189
+ # Calculator card
190
+ with gr.Group(elem_classes="calc-card"):
191
+ # First number
192
  num1 = gr.Number(
193
  label="First Number",
 
194
  value=0,
195
+ elem_classes="calc-input"
196
  )
197
 
198
+ # Operation selector (simplified to radio)
199
  operation = gr.Radio(
200
+ choices=[
201
+ ("➕", "add"),
202
+ ("➖", "subtract"),
203
+ ("✖️", "multiply"),
204
+ ("➗", "divide")
205
+ ],
206
+ value="add",
207
  label="Operation",
208
+ elem_classes="operation-btns"
209
  )
210
 
211
+ # Second number
212
  num2 = gr.Number(
213
  label="Second Number",
 
214
  value=0,
215
+ elem_classes="calc-input"
216
  )
217
+
218
+ # Result display
 
 
 
 
 
 
 
 
 
219
  result = gr.Number(
220
  label="Result",
221
  interactive=False,
222
+ elem_classes="result-box"
223
  )
224
 
225
  # Examples section
226
+ with gr.Group(elem_classes="examples-section"):
227
+ gr.Markdown("**Try these examples**", elem_classes="examples-title")
228
+ gr.Examples(
229
+ examples=[
230
+ [45, "add", 3],
231
+ [100, "subtract", 25],
232
+ [12, "multiply", 8],
233
+ [144, "divide", 12],
234
+ ],
235
+ inputs=[num1, operation, num2],
236
+ outputs=[result],
237
+ fn=calculate,
238
+ cache_examples=False
239
+ )
 
 
 
 
 
 
 
240
 
241
+ # Auto-calculate on any input change
242
+ for component in [num1, num2, operation]:
243
+ component.change(
244
+ fn=calculate,
245
+ inputs=[num1, operation, num2],
246
+ outputs=[result],
247
+ api_visibility="public"
248
+ )
249
 
250
  if __name__ == "__main__":
251
  demo.launch(
252
  theme=gr.themes.Soft(
253
  primary_hue="indigo",
254
+ secondary_hue="blue",
255
  neutral_hue="slate",
256
  font=gr.themes.GoogleFont("Inter"),
257
+ text_size="md",
258
  spacing_size="md",
259
  radius_size="lg"
260
  ).set(
261
+ body_background_fill="#f8fafc",
 
262
  block_title_text_weight="600",
263
+ block_label_text_weight="600",
264
+ button_primary_background_fill="#6366f1",
265
+ button_primary_background_fill_hover="#4f46e5",
266
+ button_primary_text_color="white",
267
  ),
268
  css=custom_css,
269
  footer_links=[