Update app.py
Browse files
    	
        app.py
    CHANGED
    
    | @@ -0,0 +1,65 @@ | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | 
|  | |
| 1 | 
            +
            import streamlit as st
         | 
| 2 | 
            +
            import sympy as sp
         | 
| 3 | 
            +
             | 
| 4 | 
            +
            # Function to calculate and explain physics problems
         | 
| 5 | 
            +
            def solve_physics_problem(equation, **kwargs):
         | 
| 6 | 
            +
                explanation = ""
         | 
| 7 | 
            +
             | 
| 8 | 
            +
                # Parse the equation and input values
         | 
| 9 | 
            +
                try:
         | 
| 10 | 
            +
                    eq = sp.sympify(equation)
         | 
| 11 | 
            +
                    symbols = eq.free_symbols
         | 
| 12 | 
            +
             | 
| 13 | 
            +
                    # Check for sufficient input
         | 
| 14 | 
            +
                    if len(kwargs) < len(symbols) - 1:
         | 
| 15 | 
            +
                        explanation += "\nInsufficient inputs provided to solve the equation."
         | 
| 16 | 
            +
                        return explanation, None
         | 
| 17 | 
            +
             | 
| 18 | 
            +
                    # Substitute known values
         | 
| 19 | 
            +
                    eq_substituted = eq
         | 
| 20 | 
            +
                    for symbol in symbols:
         | 
| 21 | 
            +
                        if str(symbol) in kwargs:
         | 
| 22 | 
            +
                            eq_substituted = eq_substituted.subs(symbol, kwargs[str(symbol)])
         | 
| 23 | 
            +
             | 
| 24 | 
            +
                    # Solve for the unknown
         | 
| 25 | 
            +
                    unknowns = [s for s in symbols if str(s) not in kwargs]
         | 
| 26 | 
            +
                    if len(unknowns) != 1:
         | 
| 27 | 
            +
                        explanation += "\nUnable to determine a single unknown variable."
         | 
| 28 | 
            +
                        return explanation, None
         | 
| 29 | 
            +
             | 
| 30 | 
            +
                    solution = sp.solve(eq_substituted, unknowns[0])
         | 
| 31 | 
            +
                    explanation += f"Step 1: Start with the equation: {equation}\n"
         | 
| 32 | 
            +
                    explanation += f"Step 2: Substitute the known values into the equation: {eq_substituted}\n"
         | 
| 33 | 
            +
                    explanation += f"Step 3: Solve for the unknown variable {unknowns[0]}:\n"
         | 
| 34 | 
            +
                    explanation += f"Solution: {unknowns[0]} = {solution[0]}\n"
         | 
| 35 | 
            +
             | 
| 36 | 
            +
                    return explanation, solution[0]
         | 
| 37 | 
            +
                except Exception as e:
         | 
| 38 | 
            +
                    explanation += f"Error: {e}\n"
         | 
| 39 | 
            +
                    return explanation, None
         | 
| 40 | 
            +
             | 
| 41 | 
            +
            # Streamlit App
         | 
| 42 | 
            +
            def main():
         | 
| 43 | 
            +
                st.title("Physics Calculator")
         | 
| 44 | 
            +
                st.write("This app helps you solve physics problems step-by-step. Provide the equation and the known parameters.")
         | 
| 45 | 
            +
             | 
| 46 | 
            +
                # Input Section
         | 
| 47 | 
            +
                equation = st.text_input("Enter the equation (e.g., F = m * a):", "F = m * a")
         | 
| 48 | 
            +
                num_params = st.number_input("Enter the number of known parameters:", min_value=1, step=1)
         | 
| 49 | 
            +
             | 
| 50 | 
            +
                input_params = {}
         | 
| 51 | 
            +
                for i in range(num_params):
         | 
| 52 | 
            +
                    param_name = st.text_input(f"Enter the name of parameter {i + 1}:")
         | 
| 53 | 
            +
                    param_value = st.number_input(f"Enter the value of {param_name}:")
         | 
| 54 | 
            +
                    input_params[param_name] = param_value
         | 
| 55 | 
            +
             | 
| 56 | 
            +
                if st.button("Solve"):
         | 
| 57 | 
            +
                    explanation, solution = solve_physics_problem(equation, **input_params)
         | 
| 58 | 
            +
                    st.subheader("Explanation:")
         | 
| 59 | 
            +
                    st.text(explanation)
         | 
| 60 | 
            +
                    if solution is not None:
         | 
| 61 | 
            +
                        st.subheader("Solution:")
         | 
| 62 | 
            +
                        st.write(solution)
         | 
| 63 | 
            +
             | 
| 64 | 
            +
            if __name__ == "__main__":
         | 
| 65 | 
            +
                main()
         |