singhn9 commited on
Commit
4605aa4
·
verified ·
1 Parent(s): 05e5007

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. 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
- API_URL = "https://router.huggingface.co/hf-inference/v1/chat/completions"
699
-
 
700
  headers = {
701
  "Authorization": f"Bearer {HF_TOKEN}",
702
- "Content-Type": "application/json"
703
  }
704
 
705
- # Safer prompt handling
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
- "model": "meta-llama/Llama-3-8B-Instruct",
717
- "messages": [
718
- {"role": "system", "content": "You are an expert metallurgical advisor."},
719
- {"role": "user", "content": prompt}
720
- ],
721
- "max_tokens": 200,
722
- "temperature": 0.5
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" HF API error {resp.status_code}: {resp.text}")
730
  else:
731
- raw_text = resp.text.strip()
732
- if not raw_text:
733
- st.warning("Operator advisory skipped: empty HF response body.")
734
- else:
735
- try:
736
- data = resp.json()
737
- # OpenAI-compatible schema
738
- text = ""
739
- if "choices" in data and len(data["choices"]) > 0:
740
- text = data["choices"][0]["message"]["content"].strip()
741
- elif isinstance(data, list) and "generated_text" in data[0]:
742
- text = data[0]["generated_text"].strip()
743
- if text:
744
- st.success("Operator Advisory Generated:")
745
- st.info(text)
746
- else:
747
- st.warning(f"Operator advisory skipped: could not parse text.\nRaw HF output:\n{raw_text}")
748
- except Exception:
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