ThoughtBridgeAI / app.py
aakashaldankar's picture
Update app.py
944abc4 verified
#!/usr/bin/env python3
import os
import gradio as gr
from crewai import Agent, Task, Crew, Process, LLM
# ========== LLM WRAPPER SETUP ==========
# Configure your OpenAI API key via environment variable
openai_api_key = os.getenv("OPEN_API_KEY")
if not openai_api_key:
raise RuntimeError("Please set the OPENAI_API_KEY environment variable.")
# Instantiate the LLM wrapper
llm = LLM(model="openai/gpt-4o", api_key=openai_api_key)
# ========== AGENT DEFINITIONS ==========
parser_agent = Agent(
role="Parser Agent",
goal="Understand and structure unstructured user thoughts",
backstory=(
"You're great at breaking down user inputs into project insights, "
"actionable tasks, and mood notes."
),
llm=llm,
verbose=False
)
memory_agent = Agent(
role="Memory Checker Agent",
goal="Identify recurring topics in past logs",
backstory=(
"You're responsible for detecting topics mentioned multiple times."
),
llm=llm,
verbose=False
)
executor_agent = Agent(
role="Execution Agent",
goal="Confirm actions and simulate execution based on insights",
backstory=(
"You're the executor: propose confirmations and mock-execute tasks."
),
llm=llm,
verbose=False
)
# ========== BUSINESS LOGIC ==========
def process_thoughts(thought_dump: str) -> str:
"""
Run the CrewAI workflow: parse, check memory, and execute.
Returns the final summary string.
"""
# Define the Task
main_task = Task(
description=(
"""You will coordinate the understanding of the following user brain-dump:
--- START OF USER THOUGHTS ---
{thought_dump}
--- END ---
Step 1: Ask the Parser Agent to extract:
- Any project-related insights
- Actionable tasks
- Health or mood notes
Step 2: Ask the Memory Agent to check for repeating patterns in the thoughts (like repeated mentioned words)
Step 3: Based on the parsed output and memory findings, ask the Execution Agent to:
- Propose a confirmation message to the user
- If the user agrees (simulate yes), execute the corresponding actions (print logs as mock)
Finally, return the final actions taken.
"""
),
expected_output="A summary of actions executed like tasks created, insights logged, and health notes recorded.",
agent=executor_agent
)
# Build and run the Crew
crew = Crew(
agents=[parser_agent, memory_agent, executor_agent],
tasks=[main_task],
process=Process.sequential,
verbose=False
)
result = crew.kickoff({"thought_dump":thought_dump})
return result
# ========== Gradio UI ==========
with gr.Blocks(title="ThoughtBridge AI") as demo:
gr.Markdown("# ThoughtBridge AI: Bridge your mind’s streams into actionable insights")
inp = gr.Textbox(
lines=8,
placeholder="Enter your brain-dump here...",
label="Your Thoughts"
)
out = gr.Textbox(
lines=10,
label="Results"
)
run_btn = gr.Button("Process")
run_btn.click(
fn=process_thoughts,
inputs=inp,
outputs=out
)
# Launch the app
if __name__ == "__main__":
demo.launch()