ZeroTech commited on
Commit
f88cbcd
·
1 Parent(s): 2d950db

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -85
app.py CHANGED
@@ -1,95 +1,53 @@
1
  import gradio as gr
 
 
2
 
3
- # Define the calculator function
4
- def calculator(num1, num2, operation):
5
- if operation == "+":
6
  return num1 + num2
7
- elif operation == "-":
8
  return num1 - num2
9
- elif operation == "*":
10
  return num1 * num2
11
- elif operation == "/":
12
- if num2 != 0:
13
- return num1 / num2
14
- else:
15
- return "Error: Division by zero"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
 
17
- # Define the button labels and values
18
- button_labels = ["7", "8", "9", "/", "4", "5", "6", "*", "1", "2", "3", "-", "0", ".", "=", "+"]
19
 
20
- button_values = [
21
- [7, 8, 9, "/"],
22
- [4, 5, 6, "*"],
23
- [1, 2, 3, "-"],
24
- [0, ".", "=", "+"]
 
 
 
 
 
 
25
  ]
26
 
27
- # Define the interface
28
- iface = gr.Interface(
29
- fn=calculator,
30
- inputs=[
31
- gr.inputs.Number(default=0, label="Num1", key="num1"),
32
- gr.inputs.Number(default=0, label="Num2", key="num2"),
33
- gradio.inputs.InputGroup(button_labels, button_values, label="Operation", key="operation")
34
- ],
35
- outputs=gr.outputs.Textbox(type="text", label="Result"),
36
- title="Calculator with Buttons",
37
- theme="default",
38
- layout="unaligned",
39
- allow_flagging=False,
40
- allow_screenshot=False,
41
- allow_share=False
42
- )
43
 
44
- # Define the custom HTML and CSS for the interface
45
- html = """
46
- <style>
47
- .button-group {
48
- display: grid;
49
- grid-template-columns: repeat(4, 1fr);
50
- grid-gap: 10px;
51
- margin-top: 10px;
52
- margin-bottom: 20px;
53
- }
54
- .button-group button {
55
- font-size: 24px;
56
- font-weight: bold;
57
- color: white;
58
- background-color: #444444;
59
- border: none;
60
- border-radius: 5px;
61
- padding: 10px;
62
- cursor: pointer;
63
- }
64
- .button-group button:hover {
65
- background-color: #555555;
66
- }
67
- .button-group button:active {
68
- background-color: #666666;
69
- }
70
- .button-group button[data-value="="] {
71
- background-color: #009900;
72
- }
73
- .button-group button[data-value="+"],
74
- .button-group button[data-value="-"],
75
- .button-group button[data-value="*"],
76
- .button-group button[data-value="/"] {
77
- background-color: #ff6600;
78
- }
79
- .button-group button[data-value="."],
80
- .button-group button[data-value="0"] {
81
- grid-column: span 2;
82
- }
83
- .output-container {
84
- margin-top: 20px;
85
- }
86
- </style>
87
- <div class="input-container">
88
- <label for="num1">Num1:</label>
89
- <input id="num1" type="number" name="num1" value="{{ num1 }}" step="any" required>
90
- </div>
91
- <div class="input-container">
92
- <label for="num2">Num2:</label>
93
- <input id="num2" type="number" name="num2" value="{{ num2 }}" step="any" required>
94
- </div>
95
- <div class
 
1
  import gradio as gr
2
+ import numpy as np
3
+ from PIL import Image, ImageDraw, ImageFont
4
 
5
+
6
+ def calculate(num1, operator, num2):
7
+ if operator == "+":
8
  return num1 + num2
9
+ elif operator == "-":
10
  return num1 - num2
11
+ elif operator == "x":
12
  return num1 * num2
13
+ elif operator == "/":
14
+ return num1 / num2
15
+
16
+
17
+ def generate_image(num1, operator, num2, result):
18
+ # Define image size and font
19
+ image_size = (500, 200)
20
+ font_size = 80
21
+ font = ImageFont.truetype("arial.ttf", font_size)
22
+
23
+ # Create a blank image with white background
24
+ image = Image.new("RGB", image_size, (255, 255, 255))
25
+
26
+ # Draw the equation and result on the image
27
+ draw = ImageDraw.Draw(image)
28
+ equation = f"{num1} {operator} {num2} = {result}"
29
+ text_size = draw.textsize(equation, font=font)
30
+ position = ((image_size[0] - text_size[0]) / 2, (image_size[1] - text_size[1]) / 2)
31
+ draw.text(position, equation, font=font, fill=(0, 0, 0))
32
 
33
+ # Return the image as a numpy array
34
+ return np.array(image)
35
 
36
+
37
+ def calculator(num1, operator, num2):
38
+ result = calculate(num1, operator, num2)
39
+ image = generate_image(num1, operator, num2, result)
40
+ return image
41
+
42
+
43
+ inputs = [
44
+ gr.inputs.Number(label="Number 1"),
45
+ gr.inputs.Dropdown(["+", "-", "x", "/"], label="Operator"),
46
+ gr.inputs.Number(label="Number 2"),
47
  ]
48
 
49
+ outputs = gr.outputs.Image(type="numpy", label="Result")
50
+
51
+ interface = gr.Interface(fn=calculator, inputs=inputs, outputs=outputs, title="Calculator", theme="compact")
 
 
 
 
 
 
 
 
 
 
 
 
 
52
 
53
+ interface.launch()