Update app.py
Browse files
app.py
CHANGED
@@ -28,7 +28,7 @@ from Bio import Entrez # Ensure BioPython is installed
|
|
28 |
from dotenv import load_dotenv
|
29 |
import requests
|
30 |
import openai # Updated for OpenAI SDK v1.0.0+
|
31 |
-
from openai import OpenAIError, RateLimitError, BadRequestError
|
32 |
|
33 |
# ---------------------- Load Environment Variables ---------------------------
|
34 |
load_dotenv()
|
@@ -53,13 +53,13 @@ if not OPENAI_API_KEY:
|
|
53 |
st.error("OpenAI API key must be set as an environment variable (OPENAI_API_KEY).")
|
54 |
st.stop()
|
55 |
|
56 |
-
#
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
|
64 |
# ---------------------- Load spaCy Model ---------------------------
|
65 |
try:
|
@@ -394,8 +394,9 @@ class DiagnosisSupport(ABC):
|
|
394 |
|
395 |
class SimpleDiagnosis(DiagnosisSupport):
|
396 |
"""Provides a simple diagnosis example, based on the Logistic regression model."""
|
397 |
-
def __init__(self):
|
398 |
self.model_trainer: LogisticRegressionTrainer = LogisticRegressionTrainer()
|
|
|
399 |
|
400 |
def diagnose(
|
401 |
self,
|
@@ -471,8 +472,9 @@ class MedicalKnowledgeBase(ABC):
|
|
471 |
|
472 |
class SimpleMedicalKnowledge(MedicalKnowledgeBase):
|
473 |
"""Enhanced Medical Knowledge Class using OpenAI GPT-4."""
|
474 |
-
def __init__(self, nlp_model):
|
475 |
self.nlp = nlp_model # Using the loaded spaCy model
|
|
|
476 |
|
477 |
def search_medical_info(self, query: str, pub_email: str = "") -> str:
|
478 |
"""
|
@@ -497,12 +499,12 @@ class SimpleMedicalKnowledge(MedicalKnowledgeBase):
|
|
497 |
"""
|
498 |
|
499 |
# Make the API request to OpenAI GPT-4
|
500 |
-
response =
|
501 |
-
model="gpt-4",
|
502 |
messages=[
|
503 |
{"role": "system", "content": "You are a helpful medical assistant."},
|
504 |
{"role": "user", "content": prompt}
|
505 |
],
|
|
|
506 |
max_tokens=500,
|
507 |
temperature=0.7,
|
508 |
)
|
@@ -780,13 +782,15 @@ def initialize_session_state():
|
|
780 |
if 'automated_reports' not in st.session_state:
|
781 |
st.session_state.automated_reports = AutomatedReports()
|
782 |
if 'diagnosis_support' not in st.session_state:
|
783 |
-
st.session_state.diagnosis_support = SimpleDiagnosis()
|
784 |
if 'treatment_recommendation' not in st.session_state:
|
785 |
st.session_state.treatment_recommendation = BasicTreatmentRecommendation()
|
786 |
if 'knowledge_base' not in st.session_state:
|
787 |
-
st.session_state.knowledge_base = SimpleMedicalKnowledge(nlp_model=nlp)
|
788 |
if 'pub_email' not in st.session_state:
|
789 |
st.session_state.pub_email = PUB_EMAIL # Load PUB_EMAIL from environment variables
|
|
|
|
|
790 |
|
791 |
def data_management_section():
|
792 |
"""Handles the data management section in the sidebar."""
|
|
|
28 |
from dotenv import load_dotenv
|
29 |
import requests
|
30 |
import openai # Updated for OpenAI SDK v1.0.0+
|
31 |
+
from openai import OpenAIError, RateLimitError, BadRequestError, OpenAI
|
32 |
|
33 |
# ---------------------- Load Environment Variables ---------------------------
|
34 |
load_dotenv()
|
|
|
53 |
st.error("OpenAI API key must be set as an environment variable (OPENAI_API_KEY).")
|
54 |
st.stop()
|
55 |
|
56 |
+
# Instantiate the OpenAI client
|
57 |
+
try:
|
58 |
+
client = OpenAI(api_key=OPENAI_API_KEY)
|
59 |
+
except Exception as e:
|
60 |
+
st.error(f"Failed to initialize OpenAI client: {e}")
|
61 |
+
logger.error(f"Failed to initialize OpenAI client: {e}")
|
62 |
+
st.stop()
|
63 |
|
64 |
# ---------------------- Load spaCy Model ---------------------------
|
65 |
try:
|
|
|
394 |
|
395 |
class SimpleDiagnosis(DiagnosisSupport):
|
396 |
"""Provides a simple diagnosis example, based on the Logistic regression model."""
|
397 |
+
def __init__(self, client: OpenAI):
|
398 |
self.model_trainer: LogisticRegressionTrainer = LogisticRegressionTrainer()
|
399 |
+
self.client = client # Using the OpenAI client
|
400 |
|
401 |
def diagnose(
|
402 |
self,
|
|
|
472 |
|
473 |
class SimpleMedicalKnowledge(MedicalKnowledgeBase):
|
474 |
"""Enhanced Medical Knowledge Class using OpenAI GPT-4."""
|
475 |
+
def __init__(self, nlp_model, client: OpenAI):
|
476 |
self.nlp = nlp_model # Using the loaded spaCy model
|
477 |
+
self.client = client # Using the OpenAI client
|
478 |
|
479 |
def search_medical_info(self, query: str, pub_email: str = "") -> str:
|
480 |
"""
|
|
|
499 |
"""
|
500 |
|
501 |
# Make the API request to OpenAI GPT-4
|
502 |
+
response = self.client.chat.completions.create(
|
|
|
503 |
messages=[
|
504 |
{"role": "system", "content": "You are a helpful medical assistant."},
|
505 |
{"role": "user", "content": prompt}
|
506 |
],
|
507 |
+
model="gpt-4",
|
508 |
max_tokens=500,
|
509 |
temperature=0.7,
|
510 |
)
|
|
|
782 |
if 'automated_reports' not in st.session_state:
|
783 |
st.session_state.automated_reports = AutomatedReports()
|
784 |
if 'diagnosis_support' not in st.session_state:
|
785 |
+
st.session_state.diagnosis_support = SimpleDiagnosis(client=st.session_state.get('openai_client'))
|
786 |
if 'treatment_recommendation' not in st.session_state:
|
787 |
st.session_state.treatment_recommendation = BasicTreatmentRecommendation()
|
788 |
if 'knowledge_base' not in st.session_state:
|
789 |
+
st.session_state.knowledge_base = SimpleMedicalKnowledge(nlp_model=nlp, client=st.session_state.get('openai_client'))
|
790 |
if 'pub_email' not in st.session_state:
|
791 |
st.session_state.pub_email = PUB_EMAIL # Load PUB_EMAIL from environment variables
|
792 |
+
if 'openai_client' not in st.session_state:
|
793 |
+
st.session_state.openai_client = client # Store the OpenAI client
|
794 |
|
795 |
def data_management_section():
|
796 |
"""Handles the data management section in the sidebar."""
|