mgbam commited on
Commit
80d6201
·
verified ·
1 Parent(s): 21af0f6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -13
app.py CHANGED
@@ -2,6 +2,8 @@ import os
2
  import json
3
  import base64
4
  import io
 
 
5
  from abc import ABC, abstractmethod
6
  from typing import Dict, List, Optional, Any
7
 
@@ -25,9 +27,8 @@ from Bio import Entrez # Ensure BioPython is installed
25
 
26
  from dotenv import load_dotenv
27
  import requests
28
- import openai # Updated for OpenAI SDK v1.8.0+
29
- import ast
30
- import logging
31
 
32
  # ---------------------- Load Environment Variables ---------------------------
33
  load_dotenv()
@@ -44,11 +45,7 @@ logger = logging.getLogger()
44
  # ---------------------- Streamlit Page Configuration ---------------------------
45
  st.set_page_config(page_title="AI Clinical Intelligence Hub", layout="wide")
46
 
47
- # ---------------------- Import OpenAIError ---------------------------
48
- from openai.error import OpenAIError
49
-
50
- # ---------------------- Initialize External Clients ---------------------------
51
- # Initialize OpenAI API details from environment variables
52
  OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
53
  PUB_EMAIL = os.getenv("PUB_EMAIL", "")
54
 
@@ -56,10 +53,12 @@ if not OPENAI_API_KEY:
56
  st.error("OpenAI API key must be set as an environment variable (OPENAI_API_KEY).")
57
  st.stop()
58
 
59
- # Set the OpenAI API key directly
60
- openai.api_key = OPENAI_API_KEY
 
 
61
 
62
- # Load spaCy model with error handling
63
  try:
64
  nlp = spacy.load("en_core_web_sm")
65
  except OSError:
@@ -495,7 +494,7 @@ class SimpleMedicalKnowledge(MedicalKnowledgeBase):
495
  """
496
 
497
  # Make the API request to OpenAI GPT-4
498
- response = openai.ChatCompletion.create(
499
  model="gpt-4",
500
  messages=[
501
  {"role": "system", "content": "You are a helpful medical assistant."},
@@ -516,7 +515,13 @@ class SimpleMedicalKnowledge(MedicalKnowledgeBase):
516
  # Format the response
517
  return f"**Based on your query:** {answer}\n\n**PubMed Abstract:**\n\n{pubmed_abstract}"
518
 
519
- except OpenAIError as e:
 
 
 
 
 
 
520
  logger.error(f"OpenAI API Error: {str(e)}")
521
  return f"OpenAI API Error: {str(e)}"
522
  except Exception as e:
 
2
  import json
3
  import base64
4
  import io
5
+ import ast
6
+ import logging
7
  from abc import ABC, abstractmethod
8
  from typing import Dict, List, Optional, Any
9
 
 
27
 
28
  from dotenv import load_dotenv
29
  import requests
30
+ import openai # Updated for OpenAI SDK v1.0.0+
31
+ from openai.error import APIError, RateLimitError, InvalidRequestError
 
32
 
33
  # ---------------------- Load Environment Variables ---------------------------
34
  load_dotenv()
 
45
  # ---------------------- Streamlit Page Configuration ---------------------------
46
  st.set_page_config(page_title="AI Clinical Intelligence Hub", layout="wide")
47
 
48
+ # ---------------------- Initialize OpenAI Client ---------------------------
 
 
 
 
49
  OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
50
  PUB_EMAIL = os.getenv("PUB_EMAIL", "")
51
 
 
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
+ client = openai.OpenAI(
58
+ api_key=OPENAI_API_KEY
59
+ )
60
 
61
+ # ---------------------- Load spaCy Model ---------------------------
62
  try:
63
  nlp = spacy.load("en_core_web_sm")
64
  except OSError:
 
494
  """
495
 
496
  # Make the API request to OpenAI GPT-4
497
+ response = client.chat.completions.create(
498
  model="gpt-4",
499
  messages=[
500
  {"role": "system", "content": "You are a helpful medical assistant."},
 
515
  # Format the response
516
  return f"**Based on your query:** {answer}\n\n**PubMed Abstract:**\n\n{pubmed_abstract}"
517
 
518
+ except RateLimitError as e:
519
+ logger.error(f"Rate Limit Exceeded: {str(e)}")
520
+ return "Rate limit exceeded. Please try again later."
521
+ except InvalidRequestError as e:
522
+ logger.error(f"Invalid Request: {str(e)}")
523
+ return f"Invalid request: {str(e)}"
524
+ except APIError as e:
525
  logger.error(f"OpenAI API Error: {str(e)}")
526
  return f"OpenAI API Error: {str(e)}"
527
  except Exception as e: