Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +155 -136
src/streamlit_app.py
CHANGED
|
@@ -1,137 +1,156 @@
|
|
| 1 |
-
import
|
| 2 |
-
|
| 3 |
-
from
|
| 4 |
-
from
|
| 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 |
-
st.
|
| 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 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
st.text(context_text)
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import streamlit as st
|
| 3 |
+
from supabase.client import create_client
|
| 4 |
+
from langchain_google_genai import GoogleGenerativeAIEmbeddings, ChatGoogleGenerativeAI
|
| 5 |
+
from streamlit_mic_recorder import speech_to_text
|
| 6 |
+
|
| 7 |
+
# Helper function to get secrets from HF or Streamlit
|
| 8 |
+
def get_secret(key):
|
| 9 |
+
# 1. Try environment variables (Hugging Face default)
|
| 10 |
+
value = os.environ.get(key)
|
| 11 |
+
if value:
|
| 12 |
+
return value
|
| 13 |
+
# 2. Try Streamlit Secrets (Hugging Face / Streamlit fallback)
|
| 14 |
+
try:
|
| 15 |
+
return st.secrets[key]
|
| 16 |
+
except Exception:
|
| 17 |
+
return None
|
| 18 |
+
|
| 19 |
+
# --- 1. SETTINGS & KEYS ---
|
| 20 |
+
# --- 1. CONFIG FROM HUGGING FACE SECRETS ---
|
| 21 |
+
SUPABASE_URL = st.secrets["SUPABASE_URL"]
|
| 22 |
+
SUPABASE_KEY = st.secrets["SUPABASE_KEY"]
|
| 23 |
+
GEMINI_KEY = st.secrets["GEMINI_KEY"]
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
# Safety Check: If any are missing, show a clear message in the UI
|
| 27 |
+
if not all([SUPABASE_URL, SUPABASE_KEY, GEMINI_KEY]):
|
| 28 |
+
st.error("🚨 Configuration Error: Secrets not found in Hugging Face Settings.")
|
| 29 |
+
st.info("Check 'Settings -> Variables and secrets' and ensure names match EXACTLY.")
|
| 30 |
+
st.stop()
|
| 31 |
+
|
| 32 |
+
# --- 2. INITIALIZE CLIENTS ---
|
| 33 |
+
# Using Gemini 3 Flash (Stable 2026 version) for the fastest industrial response
|
| 34 |
+
supabase = create_client(SUPABASE_URL, SUPABASE_KEY)
|
| 35 |
+
llm = ChatGoogleGenerativeAI(
|
| 36 |
+
model="gemini-2.5-flash-lite", # Exact name from your list
|
| 37 |
+
google_api_key=GEMINI_KEY,
|
| 38 |
+
temperature=0.1,
|
| 39 |
+
max_output_tokens=1000
|
| 40 |
+
)
|
| 41 |
+
embeddings = GoogleGenerativeAIEmbeddings(
|
| 42 |
+
model="models/gemini-embedding-001",
|
| 43 |
+
google_api_key=GEMINI_KEY,
|
| 44 |
+
task_type="retrieval_query" # Critical for the app search to work
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
# --- 3. UI SETUP ---
|
| 48 |
+
st.set_page_config(page_title="Industrial SOP Agent", page_icon="🔧", layout="wide")
|
| 49 |
+
|
| 50 |
+
# Custom CSS for a professional "Dark Mode" factory look
|
| 51 |
+
st.markdown("""
|
| 52 |
+
<style>
|
| 53 |
+
.main { background-color: #0e1117; }
|
| 54 |
+
.stButton>button { width: 100%; border-radius: 5px; height: 3em; background-color: #ff4b4b; color: white; }
|
| 55 |
+
</style>
|
| 56 |
+
""", unsafe_allow_html=True)
|
| 57 |
+
|
| 58 |
+
st.title("👨🔧 Smart Factory SOP Assistant")
|
| 59 |
+
st.caption("Troubleshoot Siemens VFDs, ESP32 Controllers, and more in real-time.")
|
| 60 |
+
|
| 61 |
+
# --- 4. SIDEBAR: THE MACHINE LIBRARY ---
|
| 62 |
+
st.sidebar.header("📚 Machine Library")
|
| 63 |
+
|
| 64 |
+
# Fetch unique manuals currently in your Supabase database
|
| 65 |
+
try:
|
| 66 |
+
response = supabase.table("documents").select("metadata->manual").execute()
|
| 67 |
+
# Extract unique names from metadata
|
| 68 |
+
all_manuals = list(set([item['manual'] for item in response.data])) if response.data else []
|
| 69 |
+
except:
|
| 70 |
+
all_manuals = []
|
| 71 |
+
|
| 72 |
+
selected_manual = st.sidebar.selectbox(
|
| 73 |
+
"Which machine are you working on?",
|
| 74 |
+
["All Machines"] + all_manuals
|
| 75 |
+
)
|
| 76 |
+
|
| 77 |
+
target_language = st.sidebar.radio("Response Language", ["Hindi (हिंदी)", "English", "Marathi (मराठी)"])
|
| 78 |
+
|
| 79 |
+
st.sidebar.divider()
|
| 80 |
+
st.sidebar.info("Tip: Use the microphone icon to describe a fault code or sound.")
|
| 81 |
+
|
| 82 |
+
# --- 5. INPUT SECTION ---
|
| 83 |
+
st.write("### 🔍 What is the issue?")
|
| 84 |
+
col1, col2 = st.columns([0.85, 0.15])
|
| 85 |
+
|
| 86 |
+
with col2:
|
| 87 |
+
# Voice input (Configured for Indian accents)
|
| 88 |
+
voice_input = speech_to_text(
|
| 89 |
+
language='hi-IN' if target_language != "English" else 'en-US',
|
| 90 |
+
start_prompt="🎙️",
|
| 91 |
+
stop_prompt="⏹️",
|
| 92 |
+
key='speech'
|
| 93 |
+
)
|
| 94 |
+
|
| 95 |
+
with col1:
|
| 96 |
+
user_query = st.text_input(
|
| 97 |
+
"Describe the fault (e.g., 'F0001 error on Siemens VFD')",
|
| 98 |
+
value=voice_input if voice_input else "",
|
| 99 |
+
placeholder="Type or speak your issue here..."
|
| 100 |
+
)
|
| 101 |
+
|
| 102 |
+
# --- 6. AGENT LOGIC ---
|
| 103 |
+
if user_query:
|
| 104 |
+
with st.spinner("Consulting technical manuals..."):
|
| 105 |
+
# A. Convert query to vector (3072 dimensions)
|
| 106 |
+
q_vector = embeddings.embed_query(user_query)
|
| 107 |
+
|
| 108 |
+
# B. Metadata Filtering logic
|
| 109 |
+
search_filter = {} if selected_manual == "All Machines" else {"manual": selected_manual}
|
| 110 |
+
|
| 111 |
+
# C. Search Database
|
| 112 |
+
# Simplified RPC call if the first one fails
|
| 113 |
+
# Simplified RPC call if the first one fails
|
| 114 |
+
matches = supabase.rpc('match_documents', {
|
| 115 |
+
'query_embedding': q_vector,
|
| 116 |
+
'match_threshold': 0.25,
|
| 117 |
+
'match_count': 4,
|
| 118 |
+
'filter': search_filter if selected_manual != "All Machines" else {}
|
| 119 |
+
}).execute()
|
| 120 |
+
|
| 121 |
+
if not matches.data:
|
| 122 |
+
st.warning("⚠️ No specific match found in the manuals. Try using the exact fault code.")
|
| 123 |
+
else:
|
| 124 |
+
# D. Compile Context
|
| 125 |
+
context_text = ""
|
| 126 |
+
for m in matches.data:
|
| 127 |
+
source = m['metadata'].get('manual', 'Unknown')
|
| 128 |
+
page = m['metadata'].get('page', '?')
|
| 129 |
+
context_text += f"\n[Source: {source}, Page: {page}]\n{m['content']}\n"
|
| 130 |
+
|
| 131 |
+
# E. Generate Engineering Response
|
| 132 |
+
prompt = f"""
|
| 133 |
+
You are a Senior Maintenance Engineer.
|
| 134 |
+
User Language: {target_language}
|
| 135 |
+
Context from Manuals: {context_text}
|
| 136 |
+
|
| 137 |
+
Worker's Issue: {user_query}
|
| 138 |
+
|
| 139 |
+
INSTRUCTIONS:
|
| 140 |
+
1. Provide the answer ONLY in {target_language}.
|
| 141 |
+
2. Start with a '⚠️ SAFETY CHECK' if high voltage or mechanical danger is involved.
|
| 142 |
+
3. Provide a clear, numbered 'Action Plan'.
|
| 143 |
+
4. State which manual and page number you found the answer on.
|
| 144 |
+
5. If the manual doesn't contain the answer, say "I cannot find this in our library."
|
| 145 |
+
"""
|
| 146 |
+
|
| 147 |
+
result = llm.invoke(prompt)
|
| 148 |
+
|
| 149 |
+
# F. Display Result
|
| 150 |
+
st.markdown("---")
|
| 151 |
+
st.markdown(f"### 🛠️ Solution for: *{selected_manual}*")
|
| 152 |
+
st.write(result.content)
|
| 153 |
+
|
| 154 |
+
# Show Sources for verification
|
| 155 |
+
with st.expander("View Source Snippets (Audit Trail)"):
|
| 156 |
st.text(context_text)
|