Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- app.py +174 -0
- image-removebg-preview (2).png +0 -0
- requirements.txt +7 -0
app.py
ADDED
@@ -0,0 +1,174 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from crewai import Agent, Task, Crew
|
3 |
+
from langchain_groq import ChatGroq
|
4 |
+
import streamlit as st
|
5 |
+
from PIL import Image
|
6 |
+
import base64
|
7 |
+
from io import BytesIO
|
8 |
+
import pandas as pd # Import pandas for handling data in tabular format
|
9 |
+
|
10 |
+
# Initialize the LLM for the Doctor Assistant
|
11 |
+
llm = ChatGroq(
|
12 |
+
groq_api_key="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",
|
13 |
+
model_name="llama3-70b-8192", # Replace with the actual model name
|
14 |
+
)
|
15 |
+
|
16 |
+
# Define the Doctor Assistant with a diagnostic goal
|
17 |
+
doctor_assistant = Agent(
|
18 |
+
role='Doctor Assistant',
|
19 |
+
goal='Collect detailed health information dynamically through a series of questions based on user responses.',
|
20 |
+
backstory=(
|
21 |
+
"You are a virtual doctor assistant who asks diagnostic questions based on user responses. "
|
22 |
+
"Your role is to gather health information before the user’s doctor consultation, adapting your questions as needed."
|
23 |
+
),
|
24 |
+
verbose=True,
|
25 |
+
llm=llm,
|
26 |
+
)
|
27 |
+
|
28 |
+
# Function to process user response and generate the next question
|
29 |
+
def get_next_question(response):
|
30 |
+
# Define the task for generating the next question based on user response
|
31 |
+
task_description = f"Generate the next diagnostic question based on the user's response: '{response}'"
|
32 |
+
|
33 |
+
# Set up the task for the assistant to generate a follow-up question
|
34 |
+
follow_up_task = Task(
|
35 |
+
description=task_description,
|
36 |
+
agent=doctor_assistant,
|
37 |
+
human_input=False,
|
38 |
+
expected_output="A contextually relevant follow-up question based on user response" # Placeholder for expected output
|
39 |
+
)
|
40 |
+
|
41 |
+
# Instantiate the crew and execute the task to get the next question
|
42 |
+
crew = Crew(
|
43 |
+
agents=[doctor_assistant],
|
44 |
+
tasks=[follow_up_task],
|
45 |
+
verbose=2,
|
46 |
+
)
|
47 |
+
result = crew.kickoff()
|
48 |
+
|
49 |
+
return result
|
50 |
+
|
51 |
+
# Load the image from the specified path
|
52 |
+
image_path = "Einstein\image\image-removebg-preview (2).png" # Adjust path if necessary
|
53 |
+
image = Image.open(image_path)
|
54 |
+
image = image.resize((300, 300))
|
55 |
+
|
56 |
+
# Convert image to base64 and display it
|
57 |
+
buffered = BytesIO()
|
58 |
+
image.save(buffered, format="PNG")
|
59 |
+
img_str = base64.b64encode(buffered.getvalue()).decode()
|
60 |
+
st.markdown("<h1 style='text-align: center;'>Doctor Assistant Chatbot</h1>", unsafe_allow_html=True)
|
61 |
+
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)
|
62 |
+
|
63 |
+
# Initialize session states for storing conversation history and user details
|
64 |
+
if "conversation" not in st.session_state:
|
65 |
+
st.session_state.conversation = []
|
66 |
+
if "user_details" not in st.session_state:
|
67 |
+
st.session_state.user_details = {}
|
68 |
+
|
69 |
+
# Display the conversation history
|
70 |
+
for turn in st.session_state.conversation:
|
71 |
+
role, content = turn
|
72 |
+
with st.chat_message(role):
|
73 |
+
st.markdown(content)
|
74 |
+
|
75 |
+
import pandas as pd
|
76 |
+
|
77 |
+
# Function to generate a concise report
|
78 |
+
def generate_report():
|
79 |
+
# Prepare patient detail data with key information only
|
80 |
+
patient_details = {
|
81 |
+
"Patient Name": st.session_state.user_details.get("name", 'N/A'),
|
82 |
+
"Patient Age": st.session_state.user_details.get("age", 'N/A'),
|
83 |
+
"Patient Gender": st.session_state.user_details.get("gender", 'N/A'),
|
84 |
+
"Patient Phone Number": st.session_state.user_details.get("phone_number", 'N/A'), # Assuming you have the phone number
|
85 |
+
}
|
86 |
+
|
87 |
+
# Create a DataFrame for patient details
|
88 |
+
details_df = pd.DataFrame.from_dict(patient_details, orient='index', columns=['Value'])
|
89 |
+
|
90 |
+
# Prepare keywords and summary of symptoms
|
91 |
+
symptoms_summary = []
|
92 |
+
for turn in st.session_state.conversation:
|
93 |
+
role, content = turn
|
94 |
+
if role == "user":
|
95 |
+
# Extract main keywords from user responses
|
96 |
+
symptoms_summary.append(content)
|
97 |
+
|
98 |
+
# Select only unique symptoms and key information
|
99 |
+
unique_symptoms = list(set(symptoms_summary))
|
100 |
+
|
101 |
+
# Prepare symptom keywords for display
|
102 |
+
symptoms_df = pd.DataFrame(unique_symptoms, columns=["Main Symptoms"])
|
103 |
+
|
104 |
+
# Display the report
|
105 |
+
report = f"""
|
106 |
+
## Patient Report
|
107 |
+
"""
|
108 |
+
|
109 |
+
return details_df, symptoms_df
|
110 |
+
|
111 |
+
|
112 |
+
|
113 |
+
|
114 |
+
# Initial input for user details
|
115 |
+
if not st.session_state.user_details:
|
116 |
+
name = st.text_input("Please enter your name:")
|
117 |
+
age = st.text_input("Please enter your age:")
|
118 |
+
gender = st.selectbox("Please select your gender:", ["Male", "Female", "Other"])
|
119 |
+
|
120 |
+
# Store user details in session state
|
121 |
+
if st.button("Submit Details"):
|
122 |
+
if name and age and gender:
|
123 |
+
st.session_state.user_details = {"name": name, "age": age, "gender": gender}
|
124 |
+
initial_question = "Thank you! Now, could you tell me what symptoms you're experiencing?"
|
125 |
+
st.session_state.conversation.append(("assistant", initial_question))
|
126 |
+
with st.chat_message("assistant"):
|
127 |
+
st.markdown(initial_question)
|
128 |
+
else:
|
129 |
+
st.warning("Please fill out all fields.")
|
130 |
+
|
131 |
+
# Check for user's response and generate the next question
|
132 |
+
if user_response := st.chat_input("Your response:"):
|
133 |
+
# Check for the end conversation keyword
|
134 |
+
if user_response.lower() in ["finish", "done", "end"]:
|
135 |
+
st.session_state.conversation.append(("user", user_response))
|
136 |
+
with st.chat_message("user"):
|
137 |
+
st.markdown(user_response)
|
138 |
+
|
139 |
+
# Generate and display the final report
|
140 |
+
report_df = generate_report()
|
141 |
+
st.table(report_df) # Display the report in table format
|
142 |
+
st.markdown("Thank you for your responses! The consultation has ended. Take care!", unsafe_allow_html=True)
|
143 |
+
st.stop() # Stop the app from proceeding further
|
144 |
+
|
145 |
+
# Append the user's response to the conversation
|
146 |
+
st.session_state.conversation.append(("user", user_response))
|
147 |
+
|
148 |
+
# Display the user's response immediately
|
149 |
+
with st.chat_message("user"):
|
150 |
+
st.markdown(user_response)
|
151 |
+
|
152 |
+
# Generate the next question based on the user's response
|
153 |
+
with st.spinner("Processing..."):
|
154 |
+
next_question = get_next_question(user_response)
|
155 |
+
|
156 |
+
# Append the assistant's next question to the conversation
|
157 |
+
st.session_state.conversation.append(("assistant", next_question))
|
158 |
+
|
159 |
+
# Display the assistant's response
|
160 |
+
with st.chat_message("assistant"):
|
161 |
+
st.markdown(next_question)
|
162 |
+
if st.button("Generate Report"):
|
163 |
+
details_df, symptoms_df = generate_report()
|
164 |
+
|
165 |
+
# Display patient details
|
166 |
+
st.markdown("### Patient Details")
|
167 |
+
st.table(details_df)
|
168 |
+
|
169 |
+
st.markdown("### Main Symptoms")
|
170 |
+
st.table(symptoms_df)
|
171 |
+
|
172 |
+
# Add an end line
|
173 |
+
st.markdown("---")
|
174 |
+
|
image-removebg-preview (2).png
ADDED
![]() |
requirements.txt
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
crewai
|
2 |
+
langchain_groq
|
3 |
+
streamlit
|
4 |
+
Pillow
|
5 |
+
base64
|
6 |
+
io
|
7 |
+
pandas
|