Update src/streamlit_app.py
Browse files830 unmodified lines831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861830 unmodified lines# --- Call HF Llama-3-70B-Instruct API for summary ---import requestsHF_TOKEN = os.getenv("HF_TOKEN")if not HF_TOKEN:st.error("HF_TOKEN not found in secrets or environment.")else:st.success("HF_TOKEN detected successfully.")API_URL = "https://api-inference.huggingface.co/models/meta-llama/Llama-3-70B-Instruct"headers = {"Authorization": f"Bearer {HF_TOKEN}"}prompt = f"""You are an expert metallurgical process advisor.Based on these recommendations:{recommendations}Target: {target}Use case: {use_case}Summarize in three concise, professional lines what the operator should do this shift."""payload = {"inputs": prompt, "parameters": {"max_new_tokens": 150, "temperature": 0.6}}with st.spinner("Generating operator note (Llama-3-70B)…"):resp = requests.post(API_URL, headers=headers, json=payload, timeout=90)text = resp.json()[0].get("generated_text","").strip()st.info(text)else:st.info("No suitable base model found.")except Exception as e:st.warning(f"Operator advisory skipped: {e}")# ----- Business Impact tabwith tabs[5]:830 unmodified lines831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889830 unmodified lines# --- Call HF Llama-3-70B-Instruct API for summary ---# --- Call HF Llama-3-70B-Instruct API for summary (robust + debug-safe) ---import requests, json, textwrapHF_TOKEN = os.getenv("HF_TOKEN") # Works on Hugging Face Spacesif not HF_TOKEN:st.error("HF_TOKEN not detected. Check the Secrets tab in your Space settings.")else:API_URL = "https://api-inference.huggingface.co/models/meta-llama/Llama-3-8B-Instruct"headers = {"Authorization": f"Bearer {HF_TOKEN}"}prompt = textwrap.dedent(f"""You are an expert metallurgical process advisor.Based on these SHAP-derived recommendations:{recommendations}Target: {target}Use case: {use_case}Summarize in three concise, professional lines what the operator should do this shift.""")payload = {"inputs": prompt,"parameters": {"max_new_tokens": 150, "temperature": 0.6}}with st.spinner("Generating operator note (Llama-3-8B)…"):resp = requests.post(API_URL, headers=headers, json=payload, timeout=90)# --- Debug section (safe, no secrets printed) ---try:data = resp.json()st.caption("Raw HF response:")st.json(data)except Exception as ex:st.warning(f"HF raw response parse error: {ex}")st.text(resp.text)data = None# --- Extract generated text robustly ---text = ""if isinstance(data, list) and len(data) > 0 and "generated_text" in data[0]:text = data[0]["generated_text"].strip()elif isinstance(data, dict) and "generated_text" in data:text = data["generated_text"].strip()elif isinstance(data, str):text = data.strip()if text:st.success("✅ Operator Advisory Generated:")st.info(text)else:st.warning("Operator advisory skipped: no text returned from model.")# ----- Business Impact tabwith tabs[5]:
src/streamlit_app.py+49-21