Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,114 +1,92 @@
|
|
| 1 |
import os
|
| 2 |
import asyncio
|
| 3 |
import streamlit as st
|
| 4 |
-
from pydantic import BaseModel
|
| 5 |
from dotenv import load_dotenv
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
InputGuardrail,
|
| 9 |
-
GuardrailFunctionOutput,
|
| 10 |
-
Runner,
|
| 11 |
-
OpenAIChatCompletionsModel,
|
| 12 |
-
RunConfig,
|
| 13 |
-
AsyncOpenAI,
|
| 14 |
-
)
|
| 15 |
|
| 16 |
# ------------------------------
|
| 17 |
# 1. Load environment variables
|
| 18 |
# ------------------------------
|
| 19 |
load_dotenv()
|
| 20 |
-
GEMINI_API_KEY = os.
|
| 21 |
|
| 22 |
if not GEMINI_API_KEY:
|
| 23 |
-
st.error("β GEMINI_API_KEY is not set in
|
| 24 |
st.stop()
|
| 25 |
|
| 26 |
# ------------------------------
|
| 27 |
-
# 2. Gemini client
|
| 28 |
-
# ------------------------------
|
| 29 |
-
external_client = AsyncOpenAI(
|
| 30 |
-
api_key=GEMINI_API_KEY,
|
| 31 |
-
base_url="https://generativelanguage.googleapis.com/v1beta/openai/",
|
| 32 |
-
)
|
| 33 |
-
|
| 34 |
-
# ------------------------------
|
| 35 |
-
# 3. Gemini model
|
| 36 |
# ------------------------------
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
openai_client=external_client,
|
| 40 |
-
)
|
| 41 |
|
| 42 |
# ------------------------------
|
| 43 |
-
#
|
| 44 |
-
# ------------------------------
|
| 45 |
-
config = RunConfig(
|
| 46 |
-
model=model,
|
| 47 |
-
model_provider=external_client,
|
| 48 |
-
tracing_disabled=True
|
| 49 |
-
)
|
| 50 |
-
|
| 51 |
-
# ------------------------------
|
| 52 |
-
# 5. Guardrail output schema
|
| 53 |
# ------------------------------
|
| 54 |
class RequirementOutput(BaseModel):
|
| 55 |
is_requirement: bool
|
| 56 |
reasoning: str
|
| 57 |
|
| 58 |
# ------------------------------
|
| 59 |
-
#
|
| 60 |
# ------------------------------
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
"You are a filter agent. "
|
| 65 |
-
"Your job is to decide if the user query is safe, relevant, and should be answered. "
|
| 66 |
-
"- If the query is about math, history, or general learning β set is_requirement=True. "
|
| 67 |
-
"- If the query is harmful, unsafe, spam, or irrelevant β set is_requirement=False. "
|
| 68 |
-
"Always explain your reasoning briefly."
|
| 69 |
-
),
|
| 70 |
-
output_type=RequirementOutput,
|
| 71 |
-
)
|
| 72 |
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
|
|
|
| 87 |
|
| 88 |
# ------------------------------
|
| 89 |
-
#
|
| 90 |
# ------------------------------
|
| 91 |
-
async def
|
| 92 |
-
|
| 93 |
-
|
| 94 |
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
)
|
| 99 |
|
| 100 |
# ------------------------------
|
| 101 |
-
#
|
| 102 |
# ------------------------------
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
|
| 110 |
# ------------------------------
|
| 111 |
-
#
|
| 112 |
# ------------------------------
|
| 113 |
st.set_page_config(page_title="Multiple Agents App", page_icon="π€", layout="centered")
|
| 114 |
st.title("π€ Multiple Agents Tutor (Math + History)")
|
|
@@ -118,11 +96,13 @@ user_query = st.text_input("Enter your question:", placeholder="e.g., What is 10
|
|
| 118 |
submit = st.button("Get Answer")
|
| 119 |
|
| 120 |
if submit and user_query.strip():
|
| 121 |
-
async def
|
| 122 |
-
|
| 123 |
-
|
|
|
|
|
|
|
| 124 |
|
| 125 |
with st.spinner("Thinking... π€"):
|
| 126 |
-
answer = asyncio.run(
|
| 127 |
st.success("β
Answer:")
|
| 128 |
st.write(answer)
|
|
|
|
| 1 |
import os
|
| 2 |
import asyncio
|
| 3 |
import streamlit as st
|
|
|
|
| 4 |
from dotenv import load_dotenv
|
| 5 |
+
import google.generativeai as genai
|
| 6 |
+
from pydantic import BaseModel
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
# ------------------------------
|
| 9 |
# 1. Load environment variables
|
| 10 |
# ------------------------------
|
| 11 |
load_dotenv()
|
| 12 |
+
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
|
| 13 |
|
| 14 |
if not GEMINI_API_KEY:
|
| 15 |
+
st.error("β GEMINI_API_KEY is not set in Secrets.")
|
| 16 |
st.stop()
|
| 17 |
|
| 18 |
# ------------------------------
|
| 19 |
+
# 2. Configure Gemini client
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
# ------------------------------
|
| 21 |
+
genai.configure(api_key=GEMINI_API_KEY)
|
| 22 |
+
model = genai.GenerativeModel("gemini-1.5-flash")
|
|
|
|
|
|
|
| 23 |
|
| 24 |
# ------------------------------
|
| 25 |
+
# 3. Guardrail output schema
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
# ------------------------------
|
| 27 |
class RequirementOutput(BaseModel):
|
| 28 |
is_requirement: bool
|
| 29 |
reasoning: str
|
| 30 |
|
| 31 |
# ------------------------------
|
| 32 |
+
# 4. Guardrail check
|
| 33 |
# ------------------------------
|
| 34 |
+
async def requirement_guardrail(query: str) -> RequirementOutput:
|
| 35 |
+
guardrail_prompt = f"""
|
| 36 |
+
Decide if the user query should be answered.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
+
Query: {query}
|
| 39 |
+
|
| 40 |
+
Rules:
|
| 41 |
+
- If query is about math, history, or learning β is_requirement=True.
|
| 42 |
+
- If query is unsafe, spam, or irrelevant β is_requirement=False.
|
| 43 |
+
Always explain your reasoning.
|
| 44 |
+
"""
|
| 45 |
+
|
| 46 |
+
resp = model.generate_content(guardrail_prompt)
|
| 47 |
+
text = resp.text.strip()
|
| 48 |
+
|
| 49 |
+
if "true" in text.lower():
|
| 50 |
+
return RequirementOutput(is_requirement=True, reasoning=text)
|
| 51 |
+
else:
|
| 52 |
+
return RequirementOutput(is_requirement=False, reasoning=text)
|
| 53 |
|
| 54 |
# ------------------------------
|
| 55 |
+
# 5. Math & History agents
|
| 56 |
# ------------------------------
|
| 57 |
+
async def math_tutor(query: str) -> str:
|
| 58 |
+
resp = model.generate_content(f"Explain step by step: {query}")
|
| 59 |
+
return resp.text
|
| 60 |
|
| 61 |
+
async def history_tutor(query: str) -> str:
|
| 62 |
+
resp = model.generate_content(f"Explain clearly: {query}")
|
| 63 |
+
return resp.text
|
|
|
|
| 64 |
|
| 65 |
# ------------------------------
|
| 66 |
+
# 6. Triage agent
|
| 67 |
# ------------------------------
|
| 68 |
+
async def triage_agent(query: str) -> str:
|
| 69 |
+
triage_prompt = f"""
|
| 70 |
+
Decide which agent should answer.
|
| 71 |
+
|
| 72 |
+
Query: {query}
|
| 73 |
+
|
| 74 |
+
Options: Math Tutor or History Tutor.
|
| 75 |
+
Reply with exactly one: "math" or "history".
|
| 76 |
+
"""
|
| 77 |
+
|
| 78 |
+
resp = model.generate_content(triage_prompt)
|
| 79 |
+
decision = resp.text.strip().lower()
|
| 80 |
+
|
| 81 |
+
if "math" in decision:
|
| 82 |
+
return await math_tutor(query)
|
| 83 |
+
elif "history" in decision:
|
| 84 |
+
return await history_tutor(query)
|
| 85 |
+
else:
|
| 86 |
+
return "π€ Sorry, I could not decide which tutor should handle this question."
|
| 87 |
|
| 88 |
# ------------------------------
|
| 89 |
+
# 7. Streamlit UI
|
| 90 |
# ------------------------------
|
| 91 |
st.set_page_config(page_title="Multiple Agents App", page_icon="π€", layout="centered")
|
| 92 |
st.title("π€ Multiple Agents Tutor (Math + History)")
|
|
|
|
| 96 |
submit = st.button("Get Answer")
|
| 97 |
|
| 98 |
if submit and user_query.strip():
|
| 99 |
+
async def run_query():
|
| 100 |
+
guardrail = await requirement_guardrail(user_query)
|
| 101 |
+
if not guardrail.is_requirement:
|
| 102 |
+
return f"β Blocked by guardrail: {guardrail.reasoning}"
|
| 103 |
+
return await triage_agent(user_query)
|
| 104 |
|
| 105 |
with st.spinner("Thinking... π€"):
|
| 106 |
+
answer = asyncio.run(run_query())
|
| 107 |
st.success("β
Answer:")
|
| 108 |
st.write(answer)
|