Spaces:
Runtime error
Runtime error
File size: 3,177 Bytes
4f0685d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
import os
import asyncio
import streamlit as st
from dotenv import load_dotenv
from agent import root_agent
from google.adk.sessions import InMemorySessionService
from google.adk.runners import Runner
from google.genai.types import Content, Part
# Load environment variables
load_dotenv()
GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
if not GOOGLE_API_KEY:
st.error("โ GOOGLE_API_KEY is missing! Please set it in a .env file.")
st.stop()
# Streamlit page setup
st.set_page_config(page_title="๐ฎ Task Gamifier", layout="wide")
st.title("๐
Gamify & Schedule Tasks")
# -------------------------------
# ๐ช Exit Button & Session Reset
# -------------------------------
if st.button("๐ Exit & Restart Session"):
for key in ["session", "runner", "history"]:
st.session_state.pop(key, None)
st.success("โ
Session restarted.")
st.rerun()
# -------------------------------
# ๐ง Initialize session + runner
# -------------------------------
if "session_service" not in st.session_state:
st.session_state.session_service = InMemorySessionService()
if "session" not in st.session_state:
st.session_state.session = asyncio.run(
st.session_state.session_service.create_session(
app_name="task_gamifier_app",
user_id="user-001"
)
)
if "runner" not in st.session_state:
st.session_state.runner = Runner(
app_name=st.session_state.session.app_name,
agent=root_agent,
session_service=st.session_state.session_service
)
if "history" not in st.session_state:
st.session_state.history = []
# -------------------------------
# โ๏ธ Task Input Form
# -------------------------------
with st.form(key="task_form", clear_on_submit=True):
task_input = st.text_input("๐ What task do you want to gamify & schedule?", key="task_input")
submitted = st.form_submit_button("๐ Submit")
if submitted and task_input:
async def process():
final_response = None
async for event in st.session_state.runner.run_async(
session_id=st.session_state.session.id,
user_id=st.session_state.session.user_id,
new_message=Content(role="user", parts=[Part(text=task_input)])
):
if event.is_final_response():
final_response = event.content.parts[0].text
return final_response
try:
response = asyncio.run(process())
st.session_state.history.append((task_input, response))
st.rerun()
except Exception as e:
st.error(f"โ ๏ธ Error: {e}")
# -------------------------------
# ๐ฌ Chat History
# -------------------------------
st.subheader("๐ฌ Interaction History")
for user_input, agent_response in reversed(st.session_state.history):
st.markdown(f"**You:** {user_input}")
st.markdown(f"๐ค **Agent:** {agent_response}")
st.markdown("---")
# -------------------------------
# ๐งน Clear Chat History
# -------------------------------
if st.button("Clear Chat Only"):
st.session_state.history = []
st.rerun()
|