EngGloss / app.py
Anum15's picture
Update app.py
3143cfd verified
import streamlit as st
import requests
import os
# Title and instructions
st.set_page_config(page_title="EngGloss", layout="centered")
st.title("📘 EngGloss – Engineering Formula Explainer")
st.write("Enter any engineering formula or equation, and I'll explain it in simple terms.")
# API key and model setup
GROQ_API_KEY = os.getenv("EngGloss") # Using your actual environment variable name
MODEL_NAME = "llama3-8b-8192" # Updated model
# Input from user
user_input = st.text_input("Enter an engineering formula (e.g., V = IR):")
# Prompt template
def build_prompt(formula):
return (
"You are an expert engineering tutor and technical writer. A user has provided you with an engineering formula.\n\n"
"Your task is to provide a complete, beginner-friendly explanation of the formula. Follow this structure:\n\n"
"1. **Explain the formula** in clear, simple language (as if teaching a smart high school or early university student).\n"
"2. Include:\n"
" - What each symbol represents.\n"
" - The real-world **meaning and intuition** behind the formula.\n"
" - Where and how the formula is commonly used in engineering or science.\n"
" - A **step-by-step example** using realistic values.\n"
"3. Organize the explanation with:\n"
" - Headings\n"
" - Bullet points\n"
" - Equations (plain text or LaTeX-style)\n"
" - Short paragraphs\n"
"4. Avoid jargon unless necessary, and define any technical terms clearly.\n"
"5. **End with a brief summary** of the formula’s importance.\n\n"
f"Now explain the following formula:\n\n{formula}"
)
# Function to call Groq API
def get_explanation(prompt):
headers = {
"Authorization": f"Bearer {GROQ_API_KEY}",
"Content-Type": "application/json"
}
payload = {
"model": MODEL_NAME,
"messages": [
{"role": "user", "content": prompt}
]
}
response = requests.post("https://api.groq.com/openai/v1/chat/completions", headers=headers, json=payload)
if response.status_code == 200:
return response.json()["choices"][0]["message"]["content"]
else:
return f"❌ Error: {response.status_code}\n{response.text}"
# Output area
if user_input:
if GROQ_API_KEY is None:
st.error("API key not found. Please set the 'EngGloss' environment variable.")
else:
with st.spinner("Analyzing and generating explanation..."):
prompt = build_prompt(user_input)
result = get_explanation(prompt)
st.markdown("### 🧠 Explanation")
st.write(result)