CoolFace
Apppublic

mdik1/Einstein

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
app.py96 linesDownload Raw Back to root
1import os2from crewai import Agent, Task, Crew3from langchain_groq import ChatGroq4import streamlit as st5from PIL import Image6 7# Initialize the LLM for the Einstein Agent8llm = ChatGroq(9    groq_api_key="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",10    model_name="llama3-70b-8192",  # Replace with the actual Einstein model name11)12 13# Define the Einstein Agent with a research-oriented goal14einstein_agent = Agent(15    role='Einstein Agent',16    goal='Provide in-depth answers and insights on various topics to help with research questions.',17    backstory=(18        "You are an Einstein Agent, skilled in gathering and synthesizing information across domains. Mainly in Physics. "19        "Your role is to answer questions with a detailed and analytical approach."20    ),21    verbose=True,22    llm=llm,23)24 25def process_question_with_agent(question):26    # Describe the task for the agent27    task_description = f"Research and provide a detailed answer to the question: '{question}'"28    29    # Define the task for the agent to generate a response to the question30    research_task = Task(31        description=task_description,32        agent=einstein_agent,33        human_input=False,34        expected_output="According to user need response to the question"  # Placeholder for expected output35    )36 37    # Instantiate the crew with the defined agent and task38    crew = Crew(39        agents=[einstein_agent],40        tasks=[research_task],41        verbose=2,42    )43 44    # Get the crew to work on the task and return the result45    result = crew.kickoff()46    47    return result48 49# Load the image from the specified path50image_path = "./image-removebg-preview (1).png"  # Update with your image path51image = Image.open(image_path)52 53# Resize the image to 500x50054image = image.resize((300, 300))55 56# Set the title of your app with Markdown57st.markdown("<h1 style='text-align: center;'>Einstein Researcher Chatbot</h1>", unsafe_allow_html=True)58 59# Convert the image to base64 for embedding in HTML60import base6461from io import BytesIO62 63buffered = BytesIO()64image.save(buffered, format="PNG")65img_str = base64.b64encode(buffered.getvalue()).decode()66 67# Display the image and center it using HTML68st.markdown(f"<div style='text-align: center;'><img src='data:image/png;base64,{img_str}' width='300' height='300'/></div>", unsafe_allow_html=True)69 70# Initialize chat history71if "messages" not in st.session_state:72    st.session_state.messages = []73 74# Display chat messages from history on app rerun75for message in st.session_state.messages:76    with st.chat_message(message["role"]):77        st.markdown(message["content"])78 79# React to user input80if prompt := st.chat_input("Ask a research question:"):81    # Display user message in chat message container82    st.chat_message("user").markdown(prompt)83    # Add user message to chat history84    st.session_state.messages.append({"role": "user", "content": prompt})85 86    # Get the response from the Einstein Agent87    with st.spinner("Processing..."):88        response = process_question_with_agent(prompt)89    90    # Display assistant response in chat message container91    with st.chat_message("assistant"):92        st.markdown(response)93    94    # Add assistant response to chat history95    st.session_state.messages.append({"role": "assistant", "content": response})96