rianders's picture
Update app.py
db8c25e verified
import streamlit as st
from openai import OpenAI
from dotenv import load_dotenv
import os
import requests
import hashlib
from git import Repo, exc
from crewai import Agent, Task, Crew
from langchain.tools import tool
# Load the OpenAI API key from the .env file
load_dotenv()
os.environ["OPENAI_API_KEY"] = userdata.get('OPENAI_API_KEY')
GITHUB_ACCESS_TOKEN = userdata.get('GITHUB_ACCESS_TOKEN')
# Initialize the OpenAI client
openai_client = OpenAI(api_key=os.getenv('OPENAI_API_KEY'))
def check_for_updates(repo_path, remote_name='origin', branch='main'):
"""
Check for updates in the specified Git repository using GitPython.
Input: repo_path - Path to the local git repository.
Output: A string indicating if updates are available or not.
"""
try:
# Open the existing repository
repo = Repo(repo_path)
if repo.bare:
raise Exception("Repository is bare.")
# Fetch updates from the remote repository
remote = repo.remotes[remote_name]
fetch_info = remote.fetch()
# Compare local and remote branches
local_commit = repo.heads[branch].commit
remote_commit = repo.refs[f'refs/remotes/{remote_name}/{branch}'].commit
if local_commit.hexsha != remote_commit.hexsha:
return "Updates are available."
else:
return "No updates available."
except exc.InvalidGitRepositoryError:
return "Invalid Git repository."
except Exception as e:
return f"Error: {str(e)}"
# Example usage
# repo_path = "/path/to/your/streamlit-repo"
# print(check_for_updates(repo_path))
# Define the custom tool for GitHub interaction
@tool
def github_streamlit_expert() -> str:
"""
Interacts with the GitHub API to check for updates in the Streamlit repository.
Output: Summary of recent changes or a message indicating no updates.
"""
# Here, you'll implement the logic to interact with the GitHub API
# and summarize the latest changes in the Streamlit repository.
# This is a placeholder implementation.
return "Checked GitHub. No new updates." # Replace with actual implementation.
# Define the custom tool for checking Streamlit documentation updates
@tool
def check_streamlit_updates(previous_hash: str) -> str:
"""
Checks for updates in the Streamlit documentation.
Input: previous_hash - The hash of the previously known state of the documentation.
Output: A tuple containing a message indicating update status and the new hash.
"""
doc_url = 'https://docs.streamlit.io/'
response = requests.get(doc_url)
content = response.text
current_hash = hashlib.md5(content.encode()).hexdigest()
if current_hash != previous_hash:
return f'Updates found in Streamlit documentation. New Hash: {current_hash}', current_hash
else:
return 'No updates in Streamlit documentation.', current_hash
# Define the Streamlit Documentation and GitHub Expert agent
streamlit_expert = Agent(
role='Streamlit Documentation and GitHub Expert',
goal='Stay updated with the latest Streamlit documentation and GitHub repository changes',
backstory='An AI agent specialized in monitoring and reporting the latest changes in Streamlit documentation and repository.',
tools=[check_streamlit_updates, github_streamlit_expert],
verbose=True
)
# Define a task for checking updates
update_task = Task(
description='Check for updates in the Streamlit documentation.',
agent=streamlit_expert
)
# Define a crew with the Streamlit Documentation Expert
crew = Crew(
agents=[streamlit_expert],
tasks=[update_task],
verbose=True
)
# Streamlit app setup
# Initialize a list to store chat history
chat_history = []
# Streamlit app setup
st.title('CrewAI Streamlit Expert - Chat Interface')
# Chat input
user_query = st.text_input("Your question:", key="user_input")
if st.button("Ask"):
# Create a task for the streamlit_expert
user_task = Task(description=user_query, agent=streamlit_expert)
# Add the task to crew and kickoff
crew.tasks.append(user_task)
response = crew.kickoff()
# Update chat history
chat_history.append(f"You: {user_query}")
chat_history.append(f"Streamlit Expert: {response}")
# Display chat history
for chat in chat_history:
st.text(chat)
# Button to check for updates in the Streamlit GitHub repository
if st.button('Check for Streamlit GitHub Updates'):
repo_path = "/path/to/your/streamlit-repo" # Path to your local Streamlit repository
update_status = check_for_updates(repo_path)
st.write(update_status)
if "Updates are available" in update_status:
pass
# Optionally, trigger further actions like updating the local repo or analyzing changes
# For example:
# response = analyze_new_changes(repo_path)
# st.write(response)