SHAMIL SHAHBAZ AWAN commited on
Commit
8199162
·
verified ·
1 Parent(s): 5709483

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -0
app.py ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+
3
+ # Define gravity constants for each planet as a percentage of Earth's gravity
4
+ MERCURY_GRAVITY = 0.376
5
+ VENUS_GRAVITY = 0.889
6
+ MARS_GRAVITY = 0.378
7
+ JUPITER_GRAVITY = 2.36
8
+ SATURN_GRAVITY = 1.081
9
+ URANUS_GRAVITY = 0.815
10
+ NEPTUNE_GRAVITY = 1.14
11
+
12
+ # Dictionary of gravity constants for easier planet validation
13
+ gravity_factors = {
14
+ "Mercury": MERCURY_GRAVITY,
15
+ "Venus": VENUS_GRAVITY,
16
+ "Mars": MARS_GRAVITY,
17
+ "Jupiter": JUPITER_GRAVITY,
18
+ "Saturn": SATURN_GRAVITY,
19
+ "Uranus": URANUS_GRAVITY,
20
+ "Neptune": NEPTUNE_GRAVITY
21
+ }
22
+
23
+ # Function to calculate weight on a given planet
24
+ def calculate_weight(earth_weight, planet):
25
+ try:
26
+ gravity_constant = gravity_factors[planet]
27
+ planetary_weight = earth_weight * gravity_constant
28
+ return f"The equivalent weight on {planet}: {round(planetary_weight, 2)} kg"
29
+ except KeyError:
30
+ return "Invalid planet. Please choose a valid planet from the solar system."
31
+
32
+ # Gradio UI interface
33
+ def main():
34
+ with gr.Blocks() as demo:
35
+ gr.Markdown("# Weight Calculator on Different Planets")
36
+ gr.Markdown("Enter your weight on Earth and choose a planet to find your weight on it.")
37
+
38
+ with gr.Row():
39
+ earth_weight_input = gr.Number(label="Enter your weight on Earth (kg)", interactive=True)
40
+ planet_input = gr.Dropdown(choices=list(gravity_factors.keys()), label="Choose a planet", interactive=True)
41
+
42
+ output = gr.Textbox(label="Equivalent Weight on Planet", interactive=False)
43
+
44
+ # Submit button and interaction
45
+ button = gr.Button("Calculate Weight")
46
+ button.click(calculate_weight, inputs=[earth_weight_input, planet_input], outputs=output)
47
+
48
+ demo.launch()
49
+
50
+ if __name__ == "__main__":
51
+ main()