File size: 2,250 Bytes
d94f40a
 
8199162
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2b9f7d6
 
795d968
2b9f7d6
 
 
 
d94f40a
2b9f7d6
 
 
 
 
 
8199162
 
 
 
 
 
 
 
 
bd931ab
 
d94f40a
8199162
 
 
 
 
e95cafc
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


import gradio as gr

# Define gravity constants for each planet as a percentage of Earth's gravity
MERCURY_GRAVITY = 0.376
VENUS_GRAVITY = 0.889
MARS_GRAVITY = 0.378
JUPITER_GRAVITY = 2.36
SATURN_GRAVITY = 1.081
URANUS_GRAVITY = 0.815
NEPTUNE_GRAVITY = 1.14

# Dictionary of gravity constants for easier planet validation
gravity_factors = {
    "Mercury": MERCURY_GRAVITY,
    "Venus": VENUS_GRAVITY,
    "Mars": MARS_GRAVITY,
    "Jupiter": JUPITER_GRAVITY,
    "Saturn": SATURN_GRAVITY,
    "Uranus": URANUS_GRAVITY,
    "Neptune": NEPTUNE_GRAVITY
}

# Function to calculate weight on a given planet
def calculate_weight(earth_weight, planet):
    try:
        gravity_constant = gravity_factors[planet]
        planetary_weight = earth_weight * gravity_constant
        return f"The equivalent weight on {planet}: {round(planetary_weight, 2)} kg"
    except KeyError:
        return "Invalid planet. Please choose a valid planet from the solar system."

# Gradio UI interface
def main():
    with gr.Blocks(css="""
        .gradio-container {
            background: url('https://www.pixelstalk.net/wp-content/uploads/images6/Solar-System-HD-Wallpaper-Computer.jpg') no-repeat center center fixed;
            background-size: cover;
            min-height: 100vh;
        }
        .gradio-container .output-box {
            animation: fadein 2s ease-in-out;
        }
        @keyframes fadein {
            from { opacity: 0; }
            to { opacity: 1; }
        }
    """) as demo:
        gr.Markdown("# Weight Calculator on Different Planets")
        gr.Markdown("Enter your weight on Earth and choose a planet to find your weight on it.")
        
        with gr.Row():
            earth_weight_input = gr.Number(label="Enter your weight on Earth (kg)", interactive=True)
            planet_input = gr.Dropdown(choices=list(gravity_factors.keys()), label="Choose a planet", interactive=True)
        
        output = gr.Textbox(label="Equivalent Weight on Planet", interactive=False)

        # Submit button and interaction
        button = gr.Button("Calculate Weight")
        button.click(calculate_weight, inputs=[earth_weight_input, planet_input], outputs=output)

    demo.launch()

if __name__ == "__main__":
    main()