File size: 2,476 Bytes
a688180
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9f84061
9a2810e
 
 
4f329a5
9a2810e
 
fa013f7
 
9a2810e
 
 
 
 
fa013f7
 
9a2810e
 
 
 
fa013f7
 
9a2810e
 
e2c7d7c
9a2810e
fa013f7
 
9a2810e
fa013f7
d3f5b15
fa013f7
9f84061
a688180
 
9f84061
 
a688180
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bc45faa
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
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 = """
    /* Ultra minimal dark theme with only essential styling */

    /* Dark background and light text */
    body {
      background-color: #121212;
      color: #ffffff;
    }
    
    /* Make inputs visible on dark background */
    input, textarea, select {
      background-color: #2a2a2a;
      color: #ffffff;
      border: 1px solid #444;
    }
    
    /* Simple gradient button */
    button {
      background: linear-gradient(135deg, #4568dc, #0f4c81);
      color: white;
    }
    
    /* Ensure result is visible */
    #result, .output-class, .gr-number-output {
      color: #ffffff;
      font-size: large;
    }
    
    /* Hide footer */
    footer {
      display: none; !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)