bala00712200502's picture
Update app.py
0798234 verified
import os
import streamlit as st
from crewai import Agent, Task, Crew, Process
from crewai_tools import SerperDevTool
from langchain_google_genai import ChatGoogleGenerativeAI
# === API Keys ===
os.environ["GOOGLE_API_KEY"] = "your-gemini-api-key-here"
os.environ["SERPER_API_KEY"] = "1079a39f788f0b67a649996db78f3f6e289cf77d"
# === Streamlit UI Config ===
st.set_page_config(page_title="πŸŽ¬πŸ“šπŸŽ΅ Smart Recommendation Agent", page_icon="πŸ€–", layout="wide")
st.title("πŸŽ¬πŸ“šπŸŽ΅ AI-Powered Recommendation Agent")
# === Session State for Chat ===
if "chat_history" not in st.session_state:
st.session_state.chat_history = []
# === Recommendation Agent Runner Function ===
def run_recommendation_agent(query: str):
try:
search_tool = SerperDevTool(
country="in",
locale="en",
location="India",
n_results=3
)
# Use Gemini AI instead of OpenAI
agent = Agent(
role="Recommendation Guru",
goal="Help users discover trending books, movies, or music from recent years using web search and explain them clearly.",
backstory="""
You are an intelligent AI designed to recommend trending and relevant content from recent years (2020 to 2025).
You use live web search to find data about books, movies, or songs related to the user's interest.
Your job is to:
- Find 3–5 recommendations with fresh data from the web.
- Provide results in bullet points AND a clean table format.
- Include details like name, genre, year, cast (for movies), author/singer (for books/music), OTT platform, and a short reason why it’s recommended.
Make your answers informative, clear, and visually clean.
""",
tools=[search_tool],
verbose=True,
allow_delegation=False,
llm=ChatGoogleGenerativeAI(model="gemini-pro", temperature=0.7)
)
task = Task(
description=f"Based on the query '{query}', find and explain personalized recommendations using the latest trends from the web.",
expected_output="""
List 5–7 recommendations (movies, books, or music) using the latest web data.
Show data as:
- Bullet points with brief summary.
- A well-formatted table including title, year, genre, key people (actor/author/singer), and OTT/platform info.
Explain why each is a good fit for the query.
""",
agent=agent
)
crew = Crew(
agents=[agent],
tasks=[task],
process=Process.sequential,
verbose=True
)
return crew.kickoff()
except Exception as e:
return f"❌ Error: {str(e)}"
# === Display Chat History ===
for chat in st.session_state.chat_history:
with st.chat_message(chat["role"]):
st.markdown(chat["content"])
# === Chat Input ===
user_input = st.chat_input("Ask for recommendations (e.g., 'Suggest feel-good movies from 2023')")
if user_input:
st.session_state.chat_history.append({"role": "user", "content": user_input})
with st.chat_message("user"):
st.markdown(user_input)
with st.chat_message("assistant"):
response = run_recommendation_agent(user_input)
st.markdown(response)
st.session_state.chat_history.append({"role": "assistant", "content": response})