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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -48
app.py CHANGED
@@ -1,53 +1,24 @@
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()
 
1
  import gradio as gr
 
 
2
 
3
+ def calculator(num1, num2, operation):
4
+ if operation == "+":
 
5
  return num1 + num2
6
+ elif operation == "-":
7
  return num1 - num2
8
+ elif operation == "*":
9
  return num1 * num2
10
+ elif operation == "/":
11
+ if num2 != 0:
12
+ return num1 / num2
13
+ else:
14
+ return "Error: Division by zero"
15
+
16
+ iface = gr.Interface(fn=calculator,
17
+ inputs=[gr.inputs.Number(default=0, label="Num1"),
18
+ gr.inputs.Number(default=0, label="Num2"),
19
+ gr.inputs.Radio(["+", "-", "*", "/"], label="Operation")],
20
+ outputs=gr.outputs.Textbox(type="text", label="Result"),
21
+ title="Calculator with Buttons",
22
+ theme="compact")
23
+
24
+ iface.launch()