Spaces:
Runtime error
Runtime error
Create src/app.py
Browse files- src/app.py +98 -0
src/app.py
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
import streamlit as st
|
| 3 |
+
from openai import OpenAI
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
st.set_page_config(
|
| 7 |
+
page_title="Civil Engineering Numerical Solver",
|
| 8 |
+
page_icon="📐",
|
| 9 |
+
layout="centered"
|
| 10 |
+
)
|
| 11 |
+
|
| 12 |
+
PRIMARY_COLOR = "#0F2C59"
|
| 13 |
+
|
| 14 |
+
st.markdown(
|
| 15 |
+
f"""
|
| 16 |
+
<h1 style='text-align:center; color:{PRIMARY_COLOR};'>
|
| 17 |
+
Procelevate – Civil Engineering Numerical Solver
|
| 18 |
+
</h1>
|
| 19 |
+
<p style='text-align:center; font-size:18px;'>
|
| 20 |
+
Solve Civil Engineering numericals with step-by-step explanations.
|
| 21 |
+
</p>
|
| 22 |
+
""",
|
| 23 |
+
unsafe_allow_html=True,
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
# Load API key from HF Secrets
|
| 27 |
+
api_key = os.getenv("OPENAI_API_KEY")
|
| 28 |
+
|
| 29 |
+
if not api_key:
|
| 30 |
+
st.warning("⚠️ Add OPENAI_API_KEY in Space → Settings → Secrets")
|
| 31 |
+
else:
|
| 32 |
+
client = OpenAI(api_key=api_key)
|
| 33 |
+
|
| 34 |
+
st.sidebar.header("Instructions")
|
| 35 |
+
st.sidebar.write("""
|
| 36 |
+
1. Choose subject
|
| 37 |
+
2. Enter any Civil Engineering numerical
|
| 38 |
+
3. Click **Solve Numerical**
|
| 39 |
+
4. Get formulas, steps & final answer
|
| 40 |
+
""")
|
| 41 |
+
|
| 42 |
+
subjects = [
|
| 43 |
+
"Structural Engineering",
|
| 44 |
+
"RCC / Concrete Design",
|
| 45 |
+
"Steel Design",
|
| 46 |
+
"Soil Mechanics / Geotech",
|
| 47 |
+
"Fluid Mechanics",
|
| 48 |
+
"Transportation Engineering",
|
| 49 |
+
"Surveying",
|
| 50 |
+
"Environmental Engineering",
|
| 51 |
+
]
|
| 52 |
+
|
| 53 |
+
subject_choice = st.selectbox("Select Subject", subjects)
|
| 54 |
+
|
| 55 |
+
problem = st.text_area(
|
| 56 |
+
"Enter the Numerical",
|
| 57 |
+
height=180,
|
| 58 |
+
placeholder="Example: A 4m beam carries 15 kN/m UDL. Find max bending moment."
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
if st.button("Solve Numerical", use_container_width=True):
|
| 62 |
+
if not api_key:
|
| 63 |
+
st.error("Missing API key. Add OPENAI_API_KEY in Space settings.")
|
| 64 |
+
elif not problem.strip():
|
| 65 |
+
st.error("Please enter a numerical problem.")
|
| 66 |
+
else:
|
| 67 |
+
with st.spinner("Solving… please wait ⏳"):
|
| 68 |
+
|
| 69 |
+
prompt = f"""
|
| 70 |
+
You are an expert Civil Engineering tutor. Solve the numerical step-by-step.
|
| 71 |
+
|
| 72 |
+
Subject: {subject_choice}
|
| 73 |
+
|
| 74 |
+
Numerical:
|
| 75 |
+
{problem}
|
| 76 |
+
|
| 77 |
+
Provide:
|
| 78 |
+
1. Formula (with symbol meanings)
|
| 79 |
+
2. Substitution with units
|
| 80 |
+
3. Step-by-step solution
|
| 81 |
+
4. Final answer with correct units
|
| 82 |
+
5. ASCII diagram (if useful)
|
| 83 |
+
6. Concept explanation
|
| 84 |
+
7. Common mistakes students make
|
| 85 |
+
"""
|
| 86 |
+
|
| 87 |
+
try:
|
| 88 |
+
response = client.chat.completions.create(
|
| 89 |
+
model="gpt-4.1",
|
| 90 |
+
messages=[{"role": "user", "content": prompt}]
|
| 91 |
+
)
|
| 92 |
+
result = response.choices[0].message["content"]
|
| 93 |
+
|
| 94 |
+
st.success("Solved!")
|
| 95 |
+
st.markdown("### 🧮 Step-by-Step Solution")
|
| 96 |
+
st.write(result)
|
| 97 |
+
|
| 98 |
+
except Exception as e:
|