Einstein / app.py
mdik1's picture
Update app.py
b2fe11b verified
import os
from crewai import Agent, Task, Crew
from langchain_groq import ChatGroq
import streamlit as st
from PIL import Image
# Initialize the LLM for the Einstein Agent
llm = ChatGroq(
groq_api_key="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",
model_name="llama3-70b-8192", # Replace with the actual Einstein model name
)
# Define the Einstein Agent with a research-oriented goal
einstein_agent = Agent(
role='Einstein Agent',
goal='Provide in-depth answers and insights on various topics to help with research questions.',
backstory=(
"You are an Einstein Agent, skilled in gathering and synthesizing information across domains. Mainly in Physics. "
"Your role is to answer questions with a detailed and analytical approach."
),
verbose=True,
llm=llm,
)
def process_question_with_agent(question):
# Describe the task for the agent
task_description = f"Research and provide a detailed answer to the question: '{question}'"
# Define the task for the agent to generate a response to the question
research_task = Task(
description=task_description,
agent=einstein_agent,
human_input=False,
expected_output="According to user need response to the question" # Placeholder for expected output
)
# Instantiate the crew with the defined agent and task
crew = Crew(
agents=[einstein_agent],
tasks=[research_task],
verbose=2,
)
# Get the crew to work on the task and return the result
result = crew.kickoff()
return result
# Load the image from the specified path
image_path = "./image-removebg-preview (1).png" # Update with your image path
image = Image.open(image_path)
# Resize the image to 500x500
image = image.resize((300, 300))
# Set the title of your app with Markdown
st.markdown("<h1 style='text-align: center;'>Einstein Researcher Chatbot</h1>", unsafe_allow_html=True)
# Convert the image to base64 for embedding in HTML
import base64
from io import BytesIO
buffered = BytesIO()
image.save(buffered, format="PNG")
img_str = base64.b64encode(buffered.getvalue()).decode()
# Display the image and center it using HTML
st.markdown(f"<div style='text-align: center;'><img src='data:image/png;base64,{img_str}' width='300' height='300'/></div>", unsafe_allow_html=True)
# Initialize chat history
if "messages" not in st.session_state:
st.session_state.messages = []
# Display chat messages from history on app rerun
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.markdown(message["content"])
# React to user input
if prompt := st.chat_input("Ask a research question:"):
# Display user message in chat message container
st.chat_message("user").markdown(prompt)
# Add user message to chat history
st.session_state.messages.append({"role": "user", "content": prompt})
# Get the response from the Einstein Agent
with st.spinner("Processing..."):
response = process_question_with_agent(prompt)
# Display assistant response in chat message container
with st.chat_message("assistant"):
st.markdown(response)
# Add assistant response to chat history
st.session_state.messages.append({"role": "assistant", "content": response})