-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathapp.py
129 lines (115 loc) · 5.34 KB
/
app.py
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# app.py
import streamlit as st
from agents import AgentManager
from utils.logger import logger
import os
from dotenv import load_dotenv
# Load environment variables from .env if present
load_dotenv()
def main():
st.set_page_config(page_title="Multi-Agent AI System", layout="wide")
st.title("Multi-Agent AI System with Collaboration and Validation")
st.sidebar.title("Select Task")
task = st.sidebar.selectbox("Choose a task:", [
"Summarize Medical Text",
"Write and Refine Research Article",
"Sanitize Medical Data (PHI)"
])
agent_manager = AgentManager(max_retries=2, verbose=True)
if task == "Summarize Medical Text":
summarize_section(agent_manager)
elif task == "Write and Refine Research Article":
write_and_refine_article_section(agent_manager)
elif task == "Sanitize Medical Data (PHI)":
sanitize_data_section(agent_manager)
def summarize_section(agent_manager):
st.header("Summarize Medical Text")
text = st.text_area("Enter medical text to summarize:", height=200)
if st.button("Summarize"):
if text:
main_agent = agent_manager.get_agent("summarize")
validator_agent = agent_manager.get_agent("summarize_validator")
with st.spinner("Summarizing..."):
try:
summary = main_agent.execute(text)
st.subheader("Summary:")
st.write(summary)
except Exception as e:
st.error(f"Error: {e}")
logger.error(f"SummarizeAgent Error: {e}")
return
with st.spinner("Validating summary..."):
try:
validation = validator_agent.execute(original_text=text, summary=summary)
st.subheader("Validation:")
st.write(validation)
except Exception as e:
st.error(f"Validation Error: {e}")
logger.error(f"SummarizeValidatorAgent Error: {e}")
else:
st.warning("Please enter some text to summarize.")
def write_and_refine_article_section(agent_manager):
st.header("Write and Refine Research Article")
topic = st.text_input("Enter the topic for the research article:")
outline = st.text_area("Enter an outline (optional):", height=150)
if st.button("Write and Refine Article"):
if topic:
writer_agent = agent_manager.get_agent("write_article")
refiner_agent = agent_manager.get_agent("refiner")
validator_agent = agent_manager.get_agent("validator")
with st.spinner("Writing article..."):
try:
draft = writer_agent.execute(topic, outline)
st.subheader("Draft Article:")
st.write(draft)
except Exception as e:
st.error(f"Error: {e}")
logger.error(f"WriteArticleAgent Error: {e}")
return
with st.spinner("Refining article..."):
try:
refined_article = refiner_agent.execute(draft)
st.subheader("Refined Article:")
st.write(refined_article)
except Exception as e:
st.error(f"Refinement Error: {e}")
logger.error(f"RefinerAgent Error: {e}")
return
with st.spinner("Validating article..."):
try:
validation = validator_agent.execute(topic=topic, article=refined_article)
st.subheader("Validation:")
st.write(validation)
except Exception as e:
st.error(f"Validation Error: {e}")
logger.error(f"ValidatorAgent Error: {e}")
else:
st.warning("Please enter a topic for the research article.")
def sanitize_data_section(agent_manager):
st.header("Sanitize Medical Data (PHI)")
medical_data = st.text_area("Enter medical data to sanitize:", height=200)
if st.button("Sanitize Data"):
if medical_data:
main_agent = agent_manager.get_agent("sanitize_data")
validator_agent = agent_manager.get_agent("sanitize_data_validator")
with st.spinner("Sanitizing data..."):
try:
sanitized_data = main_agent.execute(medical_data)
st.subheader("Sanitized Data:")
st.write(sanitized_data)
except Exception as e:
st.error(f"Error: {e}")
logger.error(f"SanitizeDataAgent Error: {e}")
return
with st.spinner("Validating sanitized data..."):
try:
validation = validator_agent.execute(original_data=medical_data, sanitized_data=sanitized_data)
st.subheader("Validation:")
st.write(validation)
except Exception as e:
st.error(f"Validation Error: {e}")
logger.error(f"SanitizeDataValidatorAgent Error: {e}")
else:
st.warning("Please enter medical data to sanitize.")
if __name__ == "__main__":
main()