Spaces:
Sleeping
Sleeping
File size: 7,859 Bytes
a688180 9f84061 fa013f7 4f329a5 fa013f7 4f329a5 fa013f7 9f84061 a688180 9f84061 a688180 |
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 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 |
import gradio as gr
import rpy2.robjects as robj
from rpy2.robjects import default_converter
from rpy2.robjects.conversion import localconverter
# define R functions globally
robj.r("""
add <- function(x,y) { return(x + y) }
multiply <- function(x,y) { return(x * y) }
sub <- function(x,y) { return(x - y) }
div <- function(x,y) { return(x / y) }
""")
def arithmetic(num1, num2, operation):
with localconverter(default_converter):
if operation == "Add":
res = robj.r(f"add({num1}, {num2})")
elif operation == "Multiply":
res = robj.r(f"multiply({num1}, {num2})")
elif operation == "Subtract":
res = robj.r(f"sub({num1}, {num2})")
elif operation == "Divide":
res = robj.r(f"div({num1}, {num2})")
return res[0]
custom_css = """
/* Define color variables for the dark theme */
:root {
--background-color: #121212;
--surface-color: #1e1e1e;
--primary-color: #3a6ea5;
--accent-color: #6d9eeb;
--text-primary: #ffffff;
--text-secondary: #b0b0b0;
--border-color: #2c2c2c;
--input-background: #2a2a2a;
--button-gradient-start: #4568dc;
--button-gradient-end: #0f4c81;
--box-shadow: 0 4px 6px rgba(0, 0, 0, 0.3);
}
/* Global styles for the Gradio app */
body {
background-color: var(--background-color) !important;
color: var(--text-primary) !important;
font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
}
/* Override Gradio's container styles */
.gradio-container {
max-width: 800px !important;
margin: 0 auto !important;
background-color: var(--background-color) !important;
}
/* Header styling */
h1, h2, h3, h4, h5, h6 {
color: var(--text-primary) !important;
font-weight: 600 !important;
}
/* Make the main title stand out */
h1 strong {
background: linear-gradient(135deg, #4568dc, #6d9eeb);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
background-clip: text;
}
/* Style the blocks and rows */
.block, .gr-box, .gr-form, .gr-panel {
background-color: var(--surface-color) !important;
border-radius: 8px !important;
border: 1px solid var(--border-color) !important;
box-shadow: var(--box-shadow) !important;
padding: 16px !important;
margin-bottom: 20px !important;
}
/* Style labels */
label, .gr-label {
color: var(--text-primary) !important;
font-weight: 500 !important;
margin-bottom: 8px !important;
}
/* Style number inputs */
input[type="number"], .gr-text-input, .gr-input {
background-color: var(--input-background) !important;
color: var(--text-primary) !important;
border: 1px solid var(--border-color) !important;
border-radius: 6px !important;
padding: 12px 16px !important;
font-size: 16px !important;
transition: border-color 0.3s ease, box-shadow 0.3s ease !important;
}
input[type="number"]:focus, .gr-text-input:focus, .gr-input:focus {
border-color: var(--accent-color) !important;
box-shadow: 0 0 0 2px rgba(109, 158, 235, 0.2) !important;
outline: none !important;
}
/* Style radio buttons */
.gr-radio-group label {
color: var(--text-secondary) !important;
}
.gr-radio {
accent-color: var(--accent-color) !important;
}
.gr-checkbox-label, .gr-radio-label {
color: var(--text-secondary) !important;
}
/* Override radio button containers */
.gr-radio-group, .gr-form {
background-color: var(--surface-color) !important;
padding: 12px !important;
border-radius: 8px !important;
border: 1px solid var(--border-color) !important;
}
/* Style button with gradient */
button, .gr-button {
background: linear-gradient(135deg, var(--button-gradient-start), var(--button-gradient-end)) !important;
color: white !important;
border: none !important;
padding: 12px 24px !important;
border-radius: 6px !important;
cursor: pointer !important;
font-weight: 600 !important;
letter-spacing: 0.5px !important;
transition: all 0.3s ease !important;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.2) !important;
text-transform: uppercase !important;
font-size: 14px !important;
width: auto !important;
display: inline-block !important;
}
button:hover, .gr-button:hover {
background: linear-gradient(135deg, #5273e3, #1a5fa0) !important;
box-shadow: 0 6px 8px rgba(0, 0, 0, 0.3) !important;
transform: translateY(-1px) !important;
}
button:active, .gr-button:active {
transform: translateY(1px) !important;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.3) !important;
}
/* Result section styling */
.gr-output-box {
background-color: var(--surface-color) !important;
border: 1px solid var(--border-color) !important;
border-radius: 8px !important;
padding: 16px !important;
}
/* Result number styling */
.gr-number-output {
font-size: 24px !important;
font-weight: 700 !important;
color: var(--accent-color) !important;
text-align: center !important;
}
/* For the result header */
h3 strong {
color: var(--accent-color) !important;
}
/* Add a subtle border to all rows */
.gr-row {
margin-bottom: 20px !important;
}
/* Remove footer */
footer {
display: none !important;
}
/* Fix any dark text on dark background issues */
.dark {
color: var(--text-primary) !important;
}
/* Additional spacing for the operation radio buttons */
#operation {
margin-top: 10px !important;
margin-bottom: 20px !important;
}
/* Add a subtle gradient background to the app container */
.gradio-app {
background: linear-gradient(160deg, #121212 0%, #1a1f2c 100%) !important;
min-height: 100vh !important;
}
/* Ensure number inputs don't have spinner arrows in Firefox */
input[type=number] {
-moz-appearance: textfield !important;
}
/* Remove spinner arrows from number inputs in other browsers */
input::-webkit-outer-spin-button,
input::-webkit-inner-spin-button {
-webkit-appearance: none !important;
margin: 0 !important;
}
/* Make the result stand out more */
#result {
font-size: 28px !important;
padding: 16px !important;
text-align: center !important;
background-color: rgba(58, 110, 165, 0.1) !important;
border-radius: 8px !important;
margin-top: 10px !important;
}
"""
with gr.Blocks(
title="R-Powered Calculator",
theme=gr.themes.Base(),
css = custom_css
) as app:
gr.HTML(
"""
<h1 style='text-align: center'>
<strong>R-Powered Calculator</strong>
</h1>
"""
)
with gr.Row():
num1 = gr.Number(label="Number 1")
num2 = gr.Number(label="Number 2")
with gr.Column():
operation = gr.Radio(choices=["Add", "Multiply", "Subtract", "Divide"],
value="Add",
label="Choose Arithmetic Operation",
min_width=100,
scale=0.3)
with gr.Row():
submit = gr.Button("Run Operation")
with gr.Column():
gr.HTML(
"""
<h3 style='text-align: center'>
<strong>Arithmetic Operation Result</strong>
</h3>
"""
)
result = gr.Number()
submit.click(arithmetic, inputs=[num1, num2, operation], outputs=[result])
app.launch(server_name="0.0.0.0", server_port=7860)
|