singhn9 commited on
Commit
e71a3a2
·
verified ·
1 Parent(s): 2da398d

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +48 -21
src/streamlit_app.py CHANGED
@@ -690,38 +690,65 @@ with tabs[4]:
690
 
691
  # --- Hugging Face advisory ---
692
  import requests, json, textwrap
 
693
  HF_TOKEN = os.getenv("HF_TOKEN")
694
  if not HF_TOKEN:
695
- st.error("HF_TOKEN not detected.")
696
  else:
697
- API_URL = "https://router.huggingface.co/hf-inference/models/meta-llama/Llama-3-8B-Instruct"
698
- headers = {"Authorization": f"Bearer {HF_TOKEN}"}
 
 
 
 
 
699
  prompt = textwrap.dedent(f"""
700
- You are an expert metallurgical advisor.
 
 
701
  Recommendations: {recs}
702
- Target: {target}
703
  Use case: {use_case}
704
- Summarize in three professional lines for the shift operator.
705
  """)
706
- payload = {"inputs": prompt, "parameters": {"max_new_tokens": 120, "temperature": 0.6}}
707
- with st.spinner("Generating advisory (Llama-3-8B)…"):
708
- resp = requests.post(API_URL, headers=headers, json=payload, timeout=90)
 
 
 
 
 
 
 
 
 
709
  try:
710
- data = resp.json()
711
- text = ""
712
- if isinstance(data, list) and len(data) > 0 and "generated_text" in data[0]:
713
- text = data[0]["generated_text"].strip()
714
- elif isinstance(data, dict) and "generated_text" in data:
715
- text = data["generated_text"].strip()
716
- if text:
717
- st.success("✅ Operator Advisory Generated:")
718
- st.info(text)
719
  else:
720
- st.warning("Operator advisory skipped: no text returned.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
721
  except Exception as e:
722
  st.warning(f"Operator advisory skipped: {e}")
723
- except Exception as e:
724
- st.warning(f"Operator advisory skipped: {e}")
725
 
726
 
727
  # ----- Business Impact tab
 
690
 
691
  # --- Hugging Face advisory ---
692
  import requests, json, textwrap
693
+
694
  HF_TOKEN = os.getenv("HF_TOKEN")
695
  if not HF_TOKEN:
696
+ st.error("HF_TOKEN not detected in environment or secrets.toml.")
697
  else:
698
+ API_URL = "https://api-inference.huggingface.co/v1/chat/completions"
699
+ headers = {
700
+ "Authorization": f"Bearer {HF_TOKEN}",
701
+ "Content-Type": "application/json"
702
+ }
703
+
704
+ # Safer prompt handling
705
  prompt = textwrap.dedent(f"""
706
+ You are an expert metallurgical process advisor.
707
+ Analyze these operator recommendations and rewrite them
708
+ as a concise 3-line professional advisory note.
709
  Recommendations: {recs}
710
+ Target variable: {target}
711
  Use case: {use_case}
 
712
  """)
713
+
714
+ payload = {
715
+ "model": "meta-llama/Llama-3-8B-Instruct",
716
+ "messages": [
717
+ {"role": "system", "content": "You are an expert metallurgical advisor."},
718
+ {"role": "user", "content": prompt}
719
+ ],
720
+ "max_tokens": 200,
721
+ "temperature": 0.5
722
+ }
723
+
724
+ with st.spinner("Generating operator advisory (Llama 3-8B)…"):
725
  try:
726
+ resp = requests.post(API_URL, headers=headers, json=payload, timeout=90)
727
+ if resp.status_code != 200:
728
+ st.warning(f" HF API error {resp.status_code}: {resp.text}")
 
 
 
 
 
 
729
  else:
730
+ raw_text = resp.text.strip()
731
+ if not raw_text:
732
+ st.warning("Operator advisory skipped: empty HF response body.")
733
+ else:
734
+ try:
735
+ data = resp.json()
736
+ # OpenAI-compatible schema
737
+ text = ""
738
+ if "choices" in data and len(data["choices"]) > 0:
739
+ text = data["choices"][0]["message"]["content"].strip()
740
+ elif isinstance(data, list) and "generated_text" in data[0]:
741
+ text = data[0]["generated_text"].strip()
742
+ if text:
743
+ st.success("Operator Advisory Generated:")
744
+ st.info(text)
745
+ else:
746
+ st.warning(f"Operator advisory skipped: could not parse text.\nRaw HF output:\n{raw_text}")
747
+ except Exception:
748
+ st.warning(f"Operator advisory skipped: invalid JSON. Raw response:\n{raw_text}")
749
  except Exception as e:
750
  st.warning(f"Operator advisory skipped: {e}")
751
+
 
752
 
753
 
754
  # ----- Business Impact tab