pkraman06's picture
Update app.py
5df3999 verified
Raw
History Blame Contribute Delete
4.22 kB
# app.py
import os
import streamlit as st
import model # Importing our structured backend module
# --- 1. Web Page Meta Architecture ---
st.set_page_config(
page_title="Custom Multi-Agent Persona Sandbox",
page_icon="🧬",
layout="wide"
)
st.title("🧬 Custom Multi-Agent Persona Sandbox")
st.caption("Select your specialized AI expert, inject live files/source code, and preview reviewed data outputs.")
# --- 2. Sidebar Layout Engine (Token & LLM Settings) ---
st.sidebar.header("βš™οΈ LLM Infrastructure Configuration")
# Extract defaults from Environment Secrets if available
hf_token_env = os.getenv("HUGGINGFACEHUB_API_TOKEN") or os.getenv("HF_TOKEN")
openai_token_env = os.getenv("OPENAI_API_KEY")
engine_provider = st.sidebar.radio(
"Choose LLM Engine Provider",
options=["Hugging Face Serverless (Default)", "OpenAI (Optional Override)"]
)
# Conditional infrastructure state rendering
user_token = ""
if engine_provider == "Hugging Face Serverless (Default)":
user_token = st.sidebar.text_input(
"Hugging Face Access Token",
value=hf_token_env if hf_token_env else "",
type="password",
placeholder="hf_..."
)
selected_model = st.sidebar.selectbox(
"Model Option",
options=["meta-llama/Meta-Llama-3-8B-Instruct", "mistralai/Mistral-7B-Instruct-v0.2"]
)
else:
user_token = st.sidebar.text_input(
"OpenAI API Key",
value=openai_token_env if openai_token_env else "",
type="password",
placeholder="sk-..."
)
selected_model = st.sidebar.selectbox(
"Model Option",
options=["gpt-4o-mini", "gpt-4-turbo", "gpt-3.5-turbo"]
)
# --- 3. Interactive Main Canvas Columns ---
col_left, col_right = st.columns([1, 1])
with col_left:
st.subheader("πŸ“‹ Step 1: Design Agent Workspace")
selected_persona = st.selectbox(
"Choose Your Target AI Agent Persona:",
options=list(model.AGENT_PERSONAS.keys())
)
user_prompt = st.text_area(
"✍️ Task Objective / Question:",
placeholder="Type your core question or instructions for the agent here...",
height=120
)
with col_right:
st.subheader("πŸ“Ž Step 2: Inject Knowledge Base Assets")
uploaded_files = st.file_uploader(
"Upload reference materials (.py, .ipynb, .pdf, .txt)",
accept_multiple_files=True
)
# --- 4. Processing and Core Execution Trigger ---
if st.button("πŸš€ Run Agent Pipeline", type="primary"):
if not user_prompt.strip():
st.warning("Please specify a prompt instruction or goal before running.")
else:
# Build document knowledge context block
injected_context = ""
if uploaded_files:
with st.spinner("Extracting multi-format source data elements..."):
for uploaded_f in uploaded_files:
# Parse the bytes using our backend function
file_content = model.parse_uploaded_file_content(
uploaded_f.name,
uploaded_f.read()
)
injected_context += file_content
# Trigger Multi-Agent Crew
with st.status("🧠 Agents Collaborating... (Generating & Auditing Answers)", expanded=True) as status:
try:
final_output = model.run_agent_pipeline(
provider=engine_provider,
model_name=selected_model,
token=user_token,
persona_key=selected_persona,
user_prompt=user_prompt,
context_text=injected_context
)
status.update(label="βœ… Review Passed & Processing Complete!", state="complete")
# Render Clean Reviewed Content out to markdown canvas
st.subheader(f"🏁 Final Output (Reviewed by QA Auditor)")
st.markdown(final_output)
except Exception as e:
status.update(label="❌ Pipeline Execution Failed", state="error")
st.error(f"An error occurred during agent orchestration: `{str(e)}`")