File size: 3,329 Bytes
52597ea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b2fe11b
52597ea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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})