MBG0903's picture
Update src/app.py
e19a2b6 verified
!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: