File size: 2,719 Bytes
0c9d9e9
e19a2b6
48cc1d5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
!pip install streamlit --upgrade
!pip install OpenAI
import pandas as pd
import streamlit as st
from openai import OpenAI
import os

st.set_page_config(
    page_title="Civil Engineering Numerical Solver",
    page_icon="📐",
    layout="centered"
)

PRIMARY_COLOR = "#0F2C59"

st.markdown(
    f"""
    <h1 style='text-align:center; color:{PRIMARY_COLOR};'>
        Procelevate – Civil Engineering Numerical Solver
    </h1>
    <p style='text-align:center; font-size:18px;'>
        Solve Civil Engineering numericals with step-by-step explanations.
    </p>
    """,
    unsafe_allow_html=True,
)

# Load API key from HF Secrets
api_key = os.getenv("OPENAI_API_KEY")

if not api_key:
    st.warning("⚠️ Add OPENAI_API_KEY in Space → Settings → Secrets")
else:
    client = OpenAI(api_key=api_key)

st.sidebar.header("Instructions")
st.sidebar.write("""
1. Choose subject  
2. Enter any Civil Engineering numerical  
3. Click **Solve Numerical**  
4. Get formulas, steps & final answer  
""")

subjects = [
    "Structural Engineering",
    "RCC / Concrete Design",
    "Steel Design",
    "Soil Mechanics / Geotech",
    "Fluid Mechanics",
    "Transportation Engineering",
    "Surveying",
    "Environmental Engineering",
]

subject_choice = st.selectbox("Select Subject", subjects)

problem = st.text_area(
    "Enter the Numerical",
    height=180,
    placeholder="Example: A 4m beam carries 15 kN/m UDL. Find max bending moment."
)

if st.button("Solve Numerical", use_container_width=True):
    if not api_key:
        st.error("Missing API key. Add OPENAI_API_KEY in Space settings.")
    elif not problem.strip():
        st.error("Please enter a numerical problem.")
    else:
        with st.spinner("Solving… please wait ⏳"):

            prompt = f"""
            You are an expert Civil Engineering tutor. Solve the numerical step-by-step.

            Subject: {subject_choice}

            Numerical:
            {problem}

            Provide:
            1. Formula (with symbol meanings)
            2. Substitution with units
            3. Step-by-step solution
            4. Final answer with correct units
            5. ASCII diagram (if useful)
            6. Concept explanation
            7. Common mistakes students make
            """

            try:
                response = client.chat.completions.create(
                    model="gpt-4.1",
                    messages=[{"role": "user", "content": prompt}]
                )
                result = response.choices[0].message["content"]

                st.success("Solved!")
                st.markdown("### 🧮 Step-by-Step Solution")
                st.write(result)

            except Exception as e: