Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,60 +1,92 @@
|
|
| 1 |
-
|
| 2 |
-
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
import pandas as pd
|
| 4 |
-
|
| 5 |
-
import
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
|
|
|
| 2 |
import pandas as pd
|
| 3 |
+
import requests
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
# ----------------------------
|
| 7 |
+
# Configuration
|
| 8 |
+
# ----------------------------
|
| 9 |
+
st.set_page_config(page_title="Instrumentation Standards AI Assistant", layout="wide")
|
| 10 |
+
|
| 11 |
+
# ----------------------------
|
| 12 |
+
# Load Data
|
| 13 |
+
# ----------------------------
|
| 14 |
+
@st.cache_data
|
| 15 |
+
def load_data():
|
| 16 |
+
df = pd.read_excel("data.xlsx", engine='openpyxl')
|
| 17 |
+
return df
|
| 18 |
+
|
| 19 |
+
df = load_data()
|
| 20 |
+
|
| 21 |
+
# ----------------------------
|
| 22 |
+
# GROQ API Summarizer
|
| 23 |
+
# ----------------------------
|
| 24 |
+
GROQ_API_KEY = os.getenv("Standardssearch") # Set this in your environment or .streamlit/secrets.toml
|
| 25 |
+
|
| 26 |
+
def summarize_with_groq(text, model="llama3-70b-8192"):
|
| 27 |
+
url = "https://api.groq.com/openai/v1/chat/completions"
|
| 28 |
+
headers = {
|
| 29 |
+
"Authorization": f"Bearer {GROQ_API_KEY}",
|
| 30 |
+
"Content-Type": "application/json"
|
| 31 |
+
}
|
| 32 |
+
payload = {
|
| 33 |
+
"model": model,
|
| 34 |
+
"messages": [
|
| 35 |
+
{"role": "system", "content": "You are a helpful assistant summarizing industrial standards for instrumentation and control engineers."},
|
| 36 |
+
{"role": "user", "content": f"Summarize the following technical standard for a professional audience:\n\n{text}"}
|
| 37 |
+
],
|
| 38 |
+
"temperature": 0.5
|
| 39 |
}
|
| 40 |
+
|
| 41 |
+
response = requests.post(url, headers=headers, json=payload)
|
| 42 |
+
if response.status_code == 200:
|
| 43 |
+
return response.json()["choices"][0]["message"]["content"]
|
| 44 |
+
else:
|
| 45 |
+
return f"Error: {response.status_code} - {response.text}"
|
| 46 |
+
|
| 47 |
+
# ----------------------------
|
| 48 |
+
# UI Layout
|
| 49 |
+
# ----------------------------
|
| 50 |
+
|
| 51 |
+
st.title("📘 Instrumentation & Control Engineering Standards")
|
| 52 |
+
st.markdown("This app helps Instrumentation & Control Engineers search and understand global engineering standards.")
|
| 53 |
+
st.markdown("**Standards included:** ANSI, API, ASME, ASTM, BS, IEC, ISA, ISO, MSS, NACE, NAMUR, NFPA, PIP, EN, and more.")
|
| 54 |
+
|
| 55 |
+
# Search Filters
|
| 56 |
+
st.subheader("🔍 Search for Industrial Standards")
|
| 57 |
+
col1, col2 = st.columns(2)
|
| 58 |
+
|
| 59 |
+
with col1:
|
| 60 |
+
entity_input = st.text_input("Enter Standard Entity").strip()
|
| 61 |
+
|
| 62 |
+
with col2:
|
| 63 |
+
name_input = st.text_input("Enter Standard Name").strip()
|
| 64 |
+
|
| 65 |
+
# Filtered Results
|
| 66 |
+
if entity_input or name_input:
|
| 67 |
+
filtered_df = df[
|
| 68 |
+
df["Standards Entity"].str.contains(entity_input, case=False, na=False) &
|
| 69 |
+
df["Standard Name"].str.contains(name_input, case=False, na=False)
|
| 70 |
+
]
|
| 71 |
+
|
| 72 |
+
if not filtered_df.empty:
|
| 73 |
+
st.success(f"✅ Found {len(filtered_df)} matching standard(s). Click to expand.")
|
| 74 |
+
|
| 75 |
+
for index, row in filtered_df.iterrows():
|
| 76 |
+
with st.expander(f"📘 {row['Standard Name']}"):
|
| 77 |
+
st.markdown(f"**Entity:** {row['Standards Entity']}")
|
| 78 |
+
st.markdown(f"**Description:** {row['Description']}")
|
| 79 |
+
|
| 80 |
+
if st.button("Summarize with AI", key=f"summarize_{index}"):
|
| 81 |
+
with st.spinner("Querying GROQ model for summary..."):
|
| 82 |
+
summary = summarize_with_groq(row["Description"])
|
| 83 |
+
st.markdown("### ✨ AI Summary")
|
| 84 |
+
st.write(summary)
|
| 85 |
+
else:
|
| 86 |
+
st.warning("No matching standards found.")
|
| 87 |
+
else:
|
| 88 |
+
st.info("Enter at least one search term to begin.")
|
| 89 |
+
|
| 90 |
+
# Footer
|
| 91 |
+
st.markdown("---")
|
| 92 |
+
st.caption("Built with ❤️ for Control & Instrumentation Engineers | Powered by GROQ + Streamlit")
|