Spaces:
Runtime error
Runtime error
File size: 16,174 Bytes
1871536 f5ce1a8 1871536 e84a43c a6b51ba e84a43c 6894abe cd4dd2c 6894abe b8834bb 6894abe cd4dd2c 6894abe b8834bb 6894abe cd4dd2c 6894abe b8834bb 6894abe cd4dd2c 6894abe b8834bb 6894abe cd4dd2c 6894abe b8834bb 6894abe 48de81e 703b8c7 48de81e e84a43c 1947481 b8834bb 1947481 81f59b6 a92a969 81f59b6 043398a 81f59b6 a92a969 81f59b6 1947481 e8b4066 a92a969 043398a a92a969 043398a 3646b59 1947481 e8b4066 b8834bb 1947481 81f59b6 000da61 1947481 000da61 d7a2a02 1947481 70f6c84 8bbc798 445cf92 81f59b6 46ca9d7 8bbc798 b8834bb |
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 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 |
import streamlit as st
import requests
import json
import os
import pandas as pd
from sentence_transformers import CrossEncoder
import numpy as np
import re
from textwrap import dedent
import google.generativeai as genai
# Tool import
from crewai.tools.gemini_tools import GeminiSearchTools
from crewai.tools.mixtral_tools import MixtralSearchTools
from crewai.tools.zephyr_tools import ZephyrSearchTools
from crewai.tools.phi2_tools import Phi2SearchTools
# Google Langchain
from langchain_google_genai import GoogleGenerativeAI
#Crew imports
from crewai import Agent, Task, Crew, Process
# Retrieve API Key from Environment Variable
GOOGLE_AI_STUDIO = os.environ.get('GOOGLE_API_KEY')
# Ensure the API key is available
if not GOOGLE_AI_STUDIO:
raise ValueError("API key not found. Please set the GOOGLE_AI_STUDIO2 environment variable.")
# Set gemini_llm
gemini_llm = GoogleGenerativeAI(model="gemini-pro", google_api_key=GOOGLE_AI_STUDIO)
# CrewAI +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
def crewai_process_gemini(research_topic):
# Define your agents with roles and goals
GeminiAgent = Agent(
role='Summary Evaluator',
goal='To learn how to manage her anxiety in social situations through group therapy.',
backstory="""Skilled in running query evaluation""",
verbose=True,
allow_delegation=False,
llm = gemini_llm,
tools=[
GeminiSearchTools.gemini_search
]
)
# Create tasks for your agents
task1 = Task(
description=f"""Create a one paragraph summary of the {research_topic}""",
agent=GeminiAgent
)
# Instantiate your crew with a sequential process
crew = Crew(
agents=[GeminiAgent],
tasks=[task1],
verbose=2,
process=Process.sequential
)
# Get your crew to work!
result = crew.kickoff()
return result
def crewai_process_mixtral_crazy(research_topic):
# Define your agents with roles and goals
MixtralCrazyAgent = Agent(
role='Summary Evaluator',
goal='Evaluate the summary using the HHEM-Victara Tuner',
backstory="""Skilled in running query evaluation""",
verbose=True,
allow_delegation=False,
llm = gemini_llm,
tools=[
MixtralSearchTools.mixtral_crazy
]
)
# Create tasks for your agents
task1 = Task(
description=f"""Create a one paragraph summary of the {research_topic}""",
agent=MixtralCrazyAgent
)
# Instantiate your crew with a sequential process
crew = Crew(
agents=[MixtralCrazyAgent],
tasks=[task1],
verbose=2,
process=Process.sequential
)
# Get your crew to work!
result = crew.kickoff()
return result
def crewai_process_mixtral_normal(research_topic):
# Define your agents with roles and goals
MixtralNormalAgent = Agent(
role='Summary Evaluator',
goal='Evaluate the summary using the HHEM-Victara Tuner',
backstory="""Skilled in running query evaluation""",
verbose=True,
allow_delegation=False,
llm = gemini_llm,
tools=[
MixtralSearchTools.mixtral_normal
]
)
# Create tasks for your agents
task1 = Task(
description=f"""Create a one paragraph summary of the {research_topic}""",
agent=MixtralNormalAgent
)
# Instantiate your crew with a sequential process
crew = Crew(
agents=[MixtralNormalAgent],
tasks=[task1],
verbose=2,
process=Process.sequential
)
# Get your crew to work!
result = crew.kickoff()
return result
def crewai_process_zephyr_normal(research_topic):
# Define your agents with roles and goals
ZephrNormalAgent = Agent(
role='Summary Evaluator',
goal='Evaluate the summary using the HHEM-Victara Tuner',
backstory="""Skilled in running query evaluation""",
verbose=True,
allow_delegation=False,
llm = gemini_llm,
tools=[
ZephyrSearchTools.zephyr_normal
]
)
# Create tasks for your agents
task1 = Task(
description=f"""Create a one paragraph summary of the {research_topic}""",
agent=ZephrNormalAgent
)
# Instantiate your crew with a sequential process
crew = Crew(
agents=[ZephrNormalAgent],
tasks=[task1],
verbose=2,
process=Process.sequential
)
# Get your crew to work!
result = crew.kickoff()
return result
def crewai_process_phi2(research_topic):
# Define your agents with roles and goals
Phi2Agent = Agent(
role='Emily Mental Patient Graphic Designer Anxiety',
goal='Evaluate the summary using the HHEM-Victara Tuner',
backstory="""Skilled in running query evaluation""",
verbose=True,
allow_delegation=False,
llm = gemini_llm,
tools=[
Phi2SearchTools.phi2_search
]
)
# Create tasks for your agents
task1 = Task(
description=f"""Create a one paragraph summary of the {research_topic}""",
agent=Phi2Agent
)
# Instantiate your crew with a sequential process
crew = Crew(
agents=[Phi2Agent],
tasks=[task1],
verbose=2,
process=Process.sequential
)
# Get your crew to work!
result = crew.kickoff()
return result
# Initialize the HHEM model +++++++++++++++++++++++++++++++++++++++++++++++
model = CrossEncoder('vectara/hallucination_evaluation_model')
# Function to compute HHEM scores
def compute_hhem_scores(texts, summary):
pairs = [[text, summary] for text in texts]
scores = model.predict(pairs)
return scores
# Define the Vectara query function
def vectara_query(query: str, config: dict):
corpus_key = [{
"customerId": config["customer_id"],
"corpusId": config["corpus_id"],
"lexicalInterpolationConfig": {"lambda": config.get("lambda_val", 0.5)},
}]
data = {
"query": [{
"query": query,
"start": 0,
"numResults": config.get("top_k", 10),
"contextConfig": {
"sentencesBefore": 2,
"sentencesAfter": 2,
},
"corpusKey": corpus_key,
"summary": [{
"responseLang": "eng",
"maxSummarizedResults": 5,
}]
}]
}
headers = {
"x-api-key": config["api_key"],
"customer-id": config["customer_id"],
"Content-Type": "application/json",
}
response = requests.post(
headers=headers,
url="https://api.vectara.io/v1/query",
data=json.dumps(data),
)
if response.status_code != 200:
st.error(f"Query failed (code {response.status_code}, reason {response.reason}, details {response.text})")
return [], ""
result = response.json()
responses = result["responseSet"][0]["response"]
summary = result["responseSet"][0]["summary"][0]["text"]
res = [[r['text'], r['score']] for r in responses]
return res, summary
# Create the main app with three tabs
tab1, tab2, tab3, tab4 = st.tabs(["Synthetic Data", "Data Query", "HHEM-Victara Query Tuner", "Model Evaluation"])
with tab1:
st.header("Synthetic Data")
st.link_button("Create Synthetic Medical Data", "https://chat.openai.com/g/g-XyHciw52w-synthetic-clinical-data")
text1 = """You are an experienced medical doctor with extensive experience in creating medical records, when clicking "Create Data" create synthetic data with the following Elements similar to the given Example following the Condition. If a number is entered product that many synthetic cases varying the details, if not just produce one case.
Elements: Chief Complaint, History of Present Illness, Past Medical History, Medication History, Social History, Family History, Review of Systems, Physical Examination, Diagnostic Test Results, Assessment and Plan, Problem List
Condition: allow for a variety of different cases and make sure the illnesses are consistent. BE VERBOSE, PUT IN JSON FORMAT.
Example: [
Case Number: 1001
Chief Complaint (CC): "I've been having chest pain for the past two hours."
History of Present Illness (HPI): Mr. Michael Smith, a 65-year-old male with a history of hypertension and smoking, presents with acute, substernal chest pain that began 2 hours ago while resting. Describes the pain as "pressure-like," rated 7/10, radiating to the left arm. Denies nausea, vomiting, or shortness of breath. Reports similar, but milder, episodes over the past month, which he attributed to indigestion. No previous evaluation for this pain. Takes aspirin occasionally for headaches.
Past Medical History (PMH):
Hypertension, diagnosed 10 years ago, managed with lisinopril.
Type 2 diabetes mellitus, diagnosed 5 years ago, managed with metformin.
No known drug allergies.
Medication History:
Lisinopril 20 mg daily.
Metformin 500 mg twice daily.
Aspirin as needed for headaches.
Social History (SH):
Retired mechanic.
Smokes half a pack of cigarettes daily for the past 40 years.
Occasional alcohol use, denies illicit drug use.
Lives with spouse, has two adult children.
Family History (FH):
Father died of a heart attack at age 70.
Mother has type 2 diabetes and hypertension.
One brother, healthy.
Review of Systems (ROS): Negative for fever, cough, dyspnea, palpitations, abdominal pain, diarrhea, constipation, dysuria, or rash. Positive for recent episodes of mild, non-exertional chest discomfort as noted in HPI.
Physical Examination (PE):
General: Awake, alert, appears mildly distressed due to pain.
Vital Signs: BP 160/90 mmHg, HR 88 bpm, RR 16/min, Temp 98.6°F (37°C), O2 Sat 98% on room air.
HEENT: Pupils equal, round, reactive to light. Mucous membranes moist.
Cardiovascular: Regular rate and rhythm, no murmurs, rubs, or gallops. No peripheral edema.
Respiratory: Clear to auscultation bilaterally, no wheezes, rales, or rhonchi.
Abdomen: Soft, non-tender, non-distended, no guarding or rebound tenderness.
Extremities: No cyanosis, clubbing, or edema.
Diagnostic Test Results:
ECG shows ST-segment elevation in leads II, III, and aVF.
Troponin I level is elevated at 0.5 ng/mL (normal <0.04 ng/mL).
Assessment and Plan:
Assessment: Acute ST-elevation myocardial infarction (STEMI), likely secondary to coronary artery disease, given risk factors (hypertension, smoking, family history).
Plan:
Immediate cardiology consultation for possible cardiac catheterization.
Start aspirin 325 mg, clopidogrel 600 mg loading dose, and heparin infusion per acute coronary syndrome protocol.
Monitor vital signs and cardiac rhythm closely in the intensive care unit.
Adjust hypertension and diabetes medications as needed.
Smoking cessation counseling and referral to a smoking cessation program.
Patient education about heart disease, importance of medication adherence, and lifestyle modifications.
Plan for discharge with outpatient follow-up in cardiology clinic.
Problem List:
Acute ST-elevation myocardial infarction (STEMI).
Hypertension.
Type 2 diabetes mellitus.
Smoking.
]
"""
st.text_area('Algorithm:', text1, height=400)
with tab2:
st.header("Data Query")
st.link_button("Query & Summarize Data", "https://chat.openai.com/g/g-9tWqg4gRY-explore-summarize-medical-data")
text2 = """When clicking on "Search Data", request the Case Number. Search knowledge for SearchMyData where XXXX is the number given and give the Elements under SearchMyData . DO NOT SEARCH THE WEB.
Elements: Case Number: XXXX, Chief Complaint (CC), History of Present Illness (HPI), Past Medical History (PMH), Medication History, Social History (SH), Family History (FH), Review of Systems (ROS), Physical Examination (PE), Diagnostic Test Results, Assessment and Plan, Problem List
SearchMyData: "Case Number": XXXX, "Chief Complaint (CC)":
"""
st.text_area('Algorithm:', text2, height=250)
with tab3:
st.header("HHEM-Victara Query Tuner")
# User inputs
query = st.text_area("Enter your text for query tuning", "", height=100)
"""
Example: Ms. Sarah Lee, a 22-year-old female college student with a history of mild intermittent asthma, presented with worsening shortness of breath and chest tightness during exercise, necessitating increased use of her albuterol inhaler for temporary relief. The details for "Assessment and Plan" were not provided in the extracted data, so a comprehensive summary integrating both Chief Complaint and specific management or treatment plans cannot be completed based on the available information.
"""
lambda_val = st.slider("Lambda Value", min_value=0.0, max_value=1.0, value=0.5)
top_k = st.number_input("Top K Results", min_value=1, max_value=50, value=10)
if st.button("Query Vectara"):
config = {
"api_key": os.environ.get("VECTARA_API_KEY", ""),
"customer_id": os.environ.get("VECTARA_CUSTOMER_ID", ""),
"corpus_id": os.environ.get("VECTARA_CORPUS_ID", ""),
"lambda_val": lambda_val,
"top_k": top_k,
}
results, summary = vectara_query(query, config)
if results:
st.subheader("Summary")
st.write(summary)
st.subheader("Top Results")
# Extract texts from results
texts = [r[0] for r in results[:5]]
# Compute HHEM scores
scores = compute_hhem_scores(texts, summary)
# Prepare and display the dataframe
df = pd.DataFrame({'Fact': texts, 'HHEM Score': scores})
st.dataframe(df)
else:
st.write("No results found.")
with tab4:
st.header("Model Evaluation")
# User input for the research topic
research_topic = st.text_area('Enter your research topic:', '', height=100)
"""
Example: Ms. Sarah Lee, a 22-year-old female college student with a history of mild intermittent asthma, presented with worsening shortness of breath and chest tightness during exercise, necessitating increased use of her albuterol inhaler for temporary relief. The details for "Assessment and Plan" were not provided in the extracted data, so a comprehensive summary integrating both Chief Complaint and specific management or treatment plans cannot be completed based on the available information.
"""
# Selection box for the function to execute
process_selection = st.selectbox(
'Choose the process to run:',
('crewai_process_gemini', 'crewai_process_mixtral_crazy', 'crewai_process_mixtral_normal', 'crewai_process_zephyr_normal', 'crewai_process_phi2')
)
# Button to execute the chosen function
if st.button('Run Process'):
if research_topic: # Ensure there's a topic provided
if process_selection == 'crewai_process_gemini':
result = crewai_process_gemini(research_topic)
elif process_selection == 'crewai_process_mixtral_crazy':
result = crewai_process_mixtral_crazy(research_topic)
elif process_selection == 'crewai_process_mixtral_normal':
result = crewai_process_mixtral_normal(research_topic)
elif process_selection == 'crewai_process_zephyr_normal':
result = crewai_process_zephyr_normal(research_topic)
elif process_selection == 'crewai_process_phi2':
result = crewai_process_phi2(research_topic)
st.write(result)
else:
st.warning('Please enter a research topic.') |