|
| 1 | +import streamlit as st |
| 2 | +from dotenv import load_dotenv |
| 3 | +from pathlib import Path |
| 4 | +import os |
| 5 | + |
| 6 | +# Import Camel-AI and OWL modules |
| 7 | +from camel.models import ModelFactory |
| 8 | +from camel.types import ModelPlatformType, ModelType |
| 9 | +from camel.logger import set_log_level |
| 10 | +from camel.societies import RolePlaying |
| 11 | +from camel.toolkits import ( |
| 12 | + ExcelToolkit, |
| 13 | + SearchToolkit, |
| 14 | + CodeExecutionToolkit, |
| 15 | +) |
| 16 | +from owl.utils import run_society |
| 17 | +from owl.utils import DocumentProcessingToolkit |
| 18 | + |
| 19 | +# Set log level to see detailed logs (optional) |
| 20 | +set_log_level("DEBUG") |
| 21 | + |
| 22 | +# Load environment variables from .env file if available |
| 23 | + |
| 24 | +load_dotenv() |
| 25 | + |
| 26 | +def construct_society(question: str) -> RolePlaying: |
| 27 | + r"""Construct a society of agents based on the given question. |
| 28 | +
|
| 29 | + Args: |
| 30 | + question (str): The task or question to be addressed by the society. |
| 31 | +
|
| 32 | + Returns: |
| 33 | + RolePlaying: A configured society of agents ready to address the question. |
| 34 | + """ |
| 35 | + |
| 36 | + # Create models for different components |
| 37 | + models = { |
| 38 | + "user": ModelFactory.create( |
| 39 | + model_platform=ModelPlatformType.OPENAI, |
| 40 | + model_type=ModelType.GPT_4O, |
| 41 | + model_config_dict={"temperature": 0}, |
| 42 | + ), |
| 43 | + "assistant": ModelFactory.create( |
| 44 | + model_platform=ModelPlatformType.OPENAI, |
| 45 | + model_type=ModelType.GPT_4O, |
| 46 | + model_config_dict={"temperature": 0}, |
| 47 | + ), |
| 48 | + } |
| 49 | + |
| 50 | + # Configure toolkits |
| 51 | + tools = [ |
| 52 | + *CodeExecutionToolkit(sandbox="subprocess", verbose=True).get_tools(), |
| 53 | + SearchToolkit().search_duckduckgo, |
| 54 | + SearchToolkit().search_wiki, |
| 55 | + SearchToolkit().search_baidu, |
| 56 | + *ExcelToolkit().get_tools(), |
| 57 | + ] |
| 58 | + |
| 59 | + # Configure agent roles and parameters |
| 60 | + user_agent_kwargs = {"model": models["user"]} |
| 61 | + assistant_agent_kwargs = {"model": models["assistant"], "tools": tools} |
| 62 | + |
| 63 | + # Configure task parameters |
| 64 | + task_kwargs = { |
| 65 | + "task_prompt": question, |
| 66 | + "with_task_specify": False, |
| 67 | + } |
| 68 | + |
| 69 | + # Create and return the society |
| 70 | + society = RolePlaying( |
| 71 | + **task_kwargs, |
| 72 | + user_role_name="user", |
| 73 | + user_agent_kwargs=user_agent_kwargs, |
| 74 | + assistant_role_name="assistant", |
| 75 | + assistant_agent_kwargs=assistant_agent_kwargs, |
| 76 | + ) |
| 77 | + |
| 78 | + return society |
| 79 | + |
| 80 | +def summarize_section(): |
| 81 | + st.header("Summarize Medical Text") |
| 82 | + text = st.text_area("Enter medical text to summarize:", height=200) |
| 83 | + if st.button("Summarize"): |
| 84 | + if text: |
| 85 | + # Create a task prompt for summarization |
| 86 | + task_prompt = f"Summarize the following medical text:\n\n{text}" |
| 87 | + society = construct_society(task_prompt) |
| 88 | + with st.spinner("Running summarization society..."): |
| 89 | + answer, chat_history, token_count = run_society(society) |
| 90 | + st.subheader("Summary:") |
| 91 | + st.write(answer) |
| 92 | + st.write(chat_history) |
| 93 | + else: |
| 94 | + st.warning("Please enter some text to summarize.") |
| 95 | + |
| 96 | +def write_and_refine_article_section(): |
| 97 | + st.header("Write and Refine Research Article") |
| 98 | + topic = st.text_input("Enter the topic for the research article:") |
| 99 | + outline = st.text_area("Enter an outline (optional):", height=150) |
| 100 | + if st.button("Write and Refine Article"): |
| 101 | + if topic: |
| 102 | + # Create a task prompt for article writing and refinement |
| 103 | + task_prompt = f"Write a research article on the topic: {topic}." |
| 104 | + if outline.strip(): |
| 105 | + task_prompt += f" Use the following outline as guidance:\n{outline}" |
| 106 | + society = construct_society(task_prompt) |
| 107 | + with st.spinner("Running research article society..."): |
| 108 | + print(task_prompt) |
| 109 | + answer, chat_history, token_count = run_society(society) |
| 110 | + st.subheader("Article:") |
| 111 | + st.write(answer) |
| 112 | + st.write(chat_history) |
| 113 | + else: |
| 114 | + st.warning("Please enter a topic for the research article.") |
| 115 | + |
| 116 | +def sanitize_data_section(): |
| 117 | + st.header("Sanitize Medical Data (PHI)") |
| 118 | + data = st.text_area("Enter medical data to sanitize:", height=200) |
| 119 | + if st.button("Sanitize Data"): |
| 120 | + if data: |
| 121 | + # Create a task prompt for data sanitization |
| 122 | + task_prompt = f"Sanitize the following medical data by removing any protected health information (PHI):\n\n{data}" |
| 123 | + society = construct_society(task_prompt) |
| 124 | + with st.spinner("Running data sanitization society..."): |
| 125 | + answer, chat_history, token_count = run_society(society) |
| 126 | + st.subheader("Sanitized Data:") |
| 127 | + st.write(answer) |
| 128 | + st.write(chat_history) |
| 129 | + else: |
| 130 | + st.warning("Please enter medical data to sanitize.") |
| 131 | + |
| 132 | +def main(): |
| 133 | + st.set_page_config(page_title="Multi-Agent AI System with Camel & OWL", layout="wide") |
| 134 | + st.title("Multi-Agent AI System with Camel-AI and OWL") |
| 135 | + |
| 136 | + st.sidebar.title("Select Task") |
| 137 | + task = st.sidebar.selectbox("Choose a task:", [ |
| 138 | + "Summarize Medical Text", |
| 139 | + "Write and Refine Research Article", |
| 140 | + "Sanitize Medical Data (PHI)" |
| 141 | + ]) |
| 142 | + |
| 143 | + if task == "Summarize Medical Text": |
| 144 | + summarize_section() |
| 145 | + elif task == "Write and Refine Research Article": |
| 146 | + write_and_refine_article_section() |
| 147 | + elif task == "Sanitize Medical Data (PHI)": |
| 148 | + sanitize_data_section() |
| 149 | + |
| 150 | +if __name__ == "__main__": |
| 151 | + main() |
0 commit comments