Spaces:
Runtime error
Runtime error
File size: 13,032 Bytes
1871536 f5ce1a8 1871536 e84a43c a6b51ba e84a43c 6894abe 48de81e 34d4587 48de81e d8c55a4 48de81e 34d4587 48de81e 5b06c04 48de81e 703b8c7 48de81e e84a43c 1947481 70f6c84 1947481 e8b4066 3646b59 1947481 e8b4066 3646b59 1947481 e8b4066 1947481 e9cb7f2 1947481 70f6c84 1947481 |
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 |
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='Emily Mental Patient Graphic Designer Anxiety',
goal='To learn how to manage her anxiety in social situations through group therapy.',
backstory="""Emily is a 28-year-old graphic designer. She has always struggled with social anxiety,
making it difficult for her to participate in group settings. She joined the therapy group to improve
her social skills and manage her anxiety. You are able to discuss a variety of mental health issues.""",
verbose=True,
allow_delegation=False,
llm = gemini_llm,
tools=[
GeminiSearchTools.gemini_search
]
)
# Create tasks for your agents
task1 = Task(
description=f"""Introduction yourself and describe your current mood and any significant events from the week affecting their mental state.
""",
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='Emily Mental Patient Graphic Designer Anxiety',
goal='To learn how to manage her anxiety in social situations through group therapy.',
backstory="""Emily is a 28-year-old graphic designer. She has always struggled with social anxiety,
making it difficult for her to participate in group settings. She joined the therapy group to improve
her social skills and manage her anxiety. You are able to discuss a variety of mental health issues.""",
verbose=True,
allow_delegation=False,
llm = gemini_llm,
tools=[
MixtralSearchTools.mixtral_crazy
]
)
# Create tasks for your agents
task1 = Task(
description=f"""Introduction yourself and describe your current mood and any significant events from the week affecting their mental state.
""",
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='Emily Mental Patient Graphic Designer Anxiety',
goal='To learn how to manage her anxiety in social situations through group therapy.',
backstory="""Emily is a 28-year-old graphic designer. She has always struggled with social anxiety,
making it difficult for her to participate in group settings. She joined the therapy group to improve
her social skills and manage her anxiety. You are able to discuss a variety of mental health issues.""",
verbose=True,
allow_delegation=False,
llm = gemini_llm,
tools=[
MixtralSearchTools.mixtral_normal
]
)
# Create tasks for your agents
task1 = Task(
description=f"""Introduction yourself and describe your current mood and any significant events from the week affecting their mental state.
""",
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='Emily Mental Patient Graphic Designer Anxiety',
goal='To learn how to manage her anxiety in social situations through group therapy.',
backstory="""Emily is a 28-year-old graphic designer. She has always struggled with social anxiety,
making it difficult for her to participate in group settings. She joined the therapy group to improve
her social skills and manage her anxiety. You are able to discuss a variety of mental health issues.""",
verbose=True,
allow_delegation=False,
llm = gemini_llm,
tools=[
ZephyrSearchTools.zephyr_normal
]
)
# Create tasks for your agents
task1 = Task(
description=f"""Introduction yourself and describe your current mood and any significant events from the week affecting their mental state.
""",
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='To learn how to manage her anxiety in social situations through group therapy.',
backstory="""Emily is a 28-year-old graphic designer. She has always struggled with social anxiety,
making it difficult for her to participate in group settings. She joined the therapy group to improve
her social skills and manage her anxiety. You are able to discuss a variety of mental health issues.""",
verbose=True,
allow_delegation=False,
llm = gemini_llm,
tools=[
Phi2SearchTools.phi2_search
]
)
# Create tasks for your agents
task1 = Task(
description=f"""Introduction yourself and describe your current mood and any significant events from the week affecting their mental state.
""",
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
# Credentials ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
corpus_id = os.environ['VECTARA_CORPUS_ID']
customer_id = os.environ['VECTARA_CUSTOMER_ID']
api_key = os.environ['VECTARA_API_KEY']
# Get Data +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
def get_post_headers() -> dict:
"""Returns headers that should be attached to each post request."""
return {
"x-api-key": api_key,
"customer-id": customer_id,
"Content-Type": "application/json",
}
def query_vectara(query: str, filter_str="", lambda_val=0.0) -> str:
corpus_key = {
"customerId": customer_id,
"corpusId": corpus_id,
"lexicalInterpolationConfig": {"lambda": lambda_val},
}
if filter_str:
corpus_key["metadataFilter"] = filter_str
data = {
"query": [
{
"query": query,
"start": 0,
"numResults": 10,
"contextConfig": {
"sentencesBefore": 2,
"sentencesAfter": 2
},
"corpusKey": [corpus_key],
"summary": [
{
"responseLang": "eng",
"maxSummarizedResults": 5,
"summarizerPromptName": "vectara-summary-ext-v1.2.0"
},
]
}
]
}
response = requests.post(
"https://api.vectara.io/v1/query",
headers=get_post_headers(),
data=json.dumps(data),
timeout=130,
)
if response.status_code != 200:
st.error(f"Query failed (code {response.status_code}, reason {response.reason}, details {response.text})")
return ""
result = response.json()
answer = result["responseSet"][0]["summary"][0]["text"]
return re.sub(r'\[\d+(,\d+){0,5}\]', '', answer)
# 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")
with tab2:
st.header("Data Query")
st.link_button("Query & Summarize Data", "https://chat.openai.com/g/g-9tWqg4gRY-explore-summarize-medical-data")
with tab3:
st.header("HHEM-Victara Query Tuner")
# User inputs
query = st.text_area("Enter your text for query tuning", "", height=75)
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")
|