Update src/streamlit_app.py
Browse files- src/streamlit_app.py +35 -34
src/streamlit_app.py
CHANGED
|
@@ -695,63 +695,64 @@ with tabs[4]:
|
|
| 695 |
if not HF_TOKEN:
|
| 696 |
st.error("HF_TOKEN not detected in environment or secrets.toml.")
|
| 697 |
else:
|
| 698 |
-
|
| 699 |
-
|
|
|
|
| 700 |
headers = {
|
| 701 |
"Authorization": f"Bearer {HF_TOKEN}",
|
| 702 |
-
"Content-Type": "application/json"
|
| 703 |
}
|
| 704 |
|
| 705 |
-
#
|
| 706 |
prompt = textwrap.dedent(f"""
|
| 707 |
You are an expert metallurgical process advisor.
|
| 708 |
-
Analyze these operator recommendations and rewrite them
|
| 709 |
as a concise 3-line professional advisory note.
|
|
|
|
| 710 |
Recommendations: {recs}
|
| 711 |
Target variable: {target}
|
| 712 |
Use case: {use_case}
|
| 713 |
""")
|
| 714 |
|
|
|
|
| 715 |
payload = {
|
| 716 |
-
"
|
| 717 |
-
"
|
| 718 |
-
|
| 719 |
-
|
| 720 |
-
|
| 721 |
-
|
| 722 |
-
|
| 723 |
}
|
| 724 |
|
| 725 |
with st.spinner("Generating operator advisory (Llama 3-8B)…"):
|
| 726 |
try:
|
| 727 |
resp = requests.post(API_URL, headers=headers, json=payload, timeout=90)
|
|
|
|
| 728 |
if resp.status_code != 200:
|
| 729 |
-
st.warning(f"
|
| 730 |
else:
|
| 731 |
-
|
| 732 |
-
|
| 733 |
-
|
| 734 |
-
|
| 735 |
-
|
| 736 |
-
|
| 737 |
-
|
| 738 |
-
|
| 739 |
-
|
| 740 |
-
|
| 741 |
-
|
| 742 |
-
|
| 743 |
-
|
| 744 |
-
|
| 745 |
-
|
| 746 |
-
|
| 747 |
-
|
| 748 |
-
|
| 749 |
-
st.warning(f"Operator advisory skipped: invalid JSON. Raw response:\n{raw_text}")
|
| 750 |
except Exception as e:
|
| 751 |
st.warning(f"Operator advisory skipped: {e}")
|
| 752 |
|
| 753 |
-
except Exception as e:
|
| 754 |
-
st.warning(f"Operator advisory skipped: {e}")
|
| 755 |
|
| 756 |
|
| 757 |
# ----- Business Impact tab
|
|
|
|
| 695 |
if not HF_TOKEN:
|
| 696 |
st.error("HF_TOKEN not detected in environment or secrets.toml.")
|
| 697 |
else:
|
| 698 |
+
# Correct endpoint per Hugging Face Router API
|
| 699 |
+
API_URL = "https://router.huggingface.co/hf-inference/models/meta-llama/Llama-3-8B-Instruct"
|
| 700 |
+
|
| 701 |
headers = {
|
| 702 |
"Authorization": f"Bearer {HF_TOKEN}",
|
| 703 |
+
"Content-Type": "application/json",
|
| 704 |
}
|
| 705 |
|
| 706 |
+
# Prepare prompt
|
| 707 |
prompt = textwrap.dedent(f"""
|
| 708 |
You are an expert metallurgical process advisor.
|
| 709 |
+
Analyze these SHAP-based operator recommendations and rewrite them
|
| 710 |
as a concise 3-line professional advisory note.
|
| 711 |
+
|
| 712 |
Recommendations: {recs}
|
| 713 |
Target variable: {target}
|
| 714 |
Use case: {use_case}
|
| 715 |
""")
|
| 716 |
|
| 717 |
+
# HF Router supports both "inputs" and OpenAI-style "messages"
|
| 718 |
payload = {
|
| 719 |
+
"inputs": prompt,
|
| 720 |
+
"parameters": {
|
| 721 |
+
"max_new_tokens": 200,
|
| 722 |
+
"temperature": 0.5,
|
| 723 |
+
"top_p": 0.95,
|
| 724 |
+
"return_full_text": False
|
| 725 |
+
}
|
| 726 |
}
|
| 727 |
|
| 728 |
with st.spinner("Generating operator advisory (Llama 3-8B)…"):
|
| 729 |
try:
|
| 730 |
resp = requests.post(API_URL, headers=headers, json=payload, timeout=90)
|
| 731 |
+
|
| 732 |
if resp.status_code != 200:
|
| 733 |
+
st.warning(f"HF API error {resp.status_code}: {resp.text}")
|
| 734 |
else:
|
| 735 |
+
try:
|
| 736 |
+
data = resp.json()
|
| 737 |
+
text = ""
|
| 738 |
+
|
| 739 |
+
# The router returns list-based structure for text generation
|
| 740 |
+
if isinstance(data, list) and len(data) > 0 and "generated_text" in data[0]:
|
| 741 |
+
text = data[0]["generated_text"].strip()
|
| 742 |
+
elif isinstance(data, dict) and "generated_text" in data:
|
| 743 |
+
text = data["generated_text"].strip()
|
| 744 |
+
|
| 745 |
+
if text:
|
| 746 |
+
st.success(" Operator Advisory Generated:")
|
| 747 |
+
st.info(text)
|
| 748 |
+
else:
|
| 749 |
+
st.warning(f"Operator advisory skipped: no text returned.\nRaw response:\n{data}")
|
| 750 |
+
|
| 751 |
+
except json.JSONDecodeError:
|
| 752 |
+
st.warning(f"Operator advisory skipped: invalid JSON.\nRaw response:\n{resp.text}")
|
|
|
|
| 753 |
except Exception as e:
|
| 754 |
st.warning(f"Operator advisory skipped: {e}")
|
| 755 |
|
|
|
|
|
|
|
| 756 |
|
| 757 |
|
| 758 |
# ----- Business Impact tab
|