Distopia22 commited on
Commit
c481ed9
Β·
1 Parent(s): d1f5b4b

Fix: Update groq library version and add error handling

Browse files
how_to_run.md DELETED
@@ -1,117 +0,0 @@
1
- <<<<<<< HEAD
2
- # ICD-CPT Model
3
-
4
- This project implements a FastAPI application that utilizes the Groq API and a language model (Llama 3.3 70b Versatile) to provide CPT and ICD coding based on provider notes. The application is designed to return structured JSON responses that include the predicted codes along with explanations for each code.
5
-
6
- ## Project Structure
7
-
8
- - **src/**: Contains the main application code.
9
- - **main.py**: Entry point for the FastAPI application.
10
- - **api/**: Contains API route definitions.
11
- - **routes.py**: Defines the endpoints for the application.
12
- - **services/**: Contains service logic for interacting with external APIs.
13
- - **groq_service.py**: Handles requests to the Groq API and processes responses.
14
- - **models/**: Contains data models for requests and responses.
15
- - **request_models.py**: Defines request models for incoming data.
16
- - **response_models.py**: Defines response models for outgoing data.
17
- - **config/**: Contains configuration settings for the application.
18
- - **settings.py**: Configuration for API keys and model IDs.
19
- - **utils/**: Contains utility functions and prompt templates.
20
- - **prompts.py**: Defines prompt templates for querying the model.
21
-
22
- - **tests/**: Contains unit tests for the application.
23
- - **test_api.py**: Tests for API endpoints.
24
-
25
- - **requirements.txt**: Lists the dependencies required for the project.
26
-
27
- - **.env.example**: Template for environment variables.
28
-
29
- - **.gitignore**: Specifies files to be ignored by Git.
30
-
31
- - **Dockerfile**: Instructions for building a Docker image for the application.
32
-
33
- ## Setup Instructions
34
-
35
- 1. **Clone the Repository**:
36
- Clone the repository to your local machine.
37
-
38
- 2. **Create a Virtual Environment**:
39
- Create a virtual environment to manage dependencies.
40
- ```
41
- python -m venv venv
42
- ```
43
-
44
- 3. **Activate the Virtual Environment**:
45
- Activate the virtual environment.
46
- - On Windows:
47
- ```
48
- venv\Scripts\activate
49
- ```
50
- - On macOS/Linux:
51
- ```
52
- source venv/bin/activate
53
- ```
54
-
55
- 4. **Install Dependencies**:
56
- Install the required dependencies using pip.
57
- ```
58
- pip install -r requirements.txt
59
- ```
60
-
61
- 5. **Set Up Environment Variables**:
62
- Copy `.env.example` to `.env` and fill in the necessary values, including the Groq API key and model ID.
63
-
64
- 6. **Run the Application**:
65
- Start the FastAPI application.
66
- ```
67
- uvicorn src.main:app --reload
68
- ```
69
-
70
- ## Usage
71
-
72
- - **Endpoint**: `/api/coding`
73
- - **Method**: `POST`
74
- - **Request Body**:
75
- ```json
76
- {
77
- "provider_notes": "Your provider notes here."
78
- }
79
- ```
80
-
81
- - **Response**:
82
- ```json
83
- {
84
- "icd_codes": [
85
- {
86
- "code": "ICD_CODE_1",
87
- "explanation": "Explanation for ICD_CODE_1"
88
- }
89
- ],
90
- "cpt_codes": [
91
- {
92
- "code": "CPT_CODE_1",
93
- "explanation": "Explanation for CPT_CODE_1"
94
- }
95
- ]
96
- }
97
- ```
98
-
99
- ## Deployment
100
-
101
- For deployment, you can use Docker to containerize the application. Follow the instructions in the Dockerfile to build and run the application in a containerized environment.
102
-
103
- ## License
104
-
105
- This project is licensed under the MIT License. See the LICENSE file for more details.
106
- =======
107
- ---
108
- title: Icd Cpt Coding Api
109
- emoji: πŸ“ˆ
110
- colorFrom: indigo
111
- colorTo: indigo
112
- sdk: docker
113
- pinned: false
114
- ---
115
-
116
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
117
- >>>>>>> cc72a56032827788eed3105a0ef90e037552e5a7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
requirements.txt CHANGED
@@ -1,5 +1,6 @@
1
  fastapi==0.104.1
2
  uvicorn[standard]==0.24.0
3
- groq==0.4.1
4
  pydantic==2.5.0
5
- python-dotenv==1.0.0
 
 
1
  fastapi==0.104.1
2
  uvicorn[standard]==0.24.0
3
+ groq==0.11.0
4
  pydantic==2.5.0
5
+ python-dotenv==1.0.0
6
+ httpx==0.27.0
src/config/settings.py CHANGED
@@ -5,13 +5,19 @@ from dotenv import load_dotenv
5
  load_dotenv()
6
 
7
  class Settings:
8
- GROQ_API_KEY: str = os.getenv("GROQ_API_KEY")
9
- MODEL_ID: str = os.getenv("MODEL_ID")
10
-
11
  def __init__(self):
 
 
 
 
12
  if not self.GROQ_API_KEY:
13
- raise ValueError("GROQ_API_KEY not found in environment variables")
14
- if not self.MODEL_ID:
15
- raise ValueError("MODEL_ID not found in environment variables")
 
 
 
 
 
16
 
17
  settings = Settings()
 
5
  load_dotenv()
6
 
7
  class Settings:
 
 
 
8
  def __init__(self):
9
+ self.GROQ_API_KEY = os.getenv("GROQ_API_KEY")
10
+ self.MODEL_ID = os.getenv("MODEL_ID", "llama-3.3-70b-versatile")
11
+
12
+ # Validate required environment variables
13
  if not self.GROQ_API_KEY:
14
+ raise ValueError(
15
+ "GROQ_API_KEY not found in environment variables. "
16
+ "Please set it in Hugging Face Space Settings -> Repository secrets"
17
+ )
18
+
19
+ print(f"βœ… Settings loaded successfully")
20
+ print(f"βœ… Model ID: {self.MODEL_ID}")
21
+ print(f"βœ… API Key configured: {self.GROQ_API_KEY[:10]}...")
22
 
23
  settings = Settings()
src/services/groq_service.py CHANGED
@@ -11,14 +11,21 @@ from utils.prompts import SYSTEM_PROMPT, create_user_prompt
11
 
12
  class GroqService:
13
  def __init__(self):
14
- self.client = Groq(api_key=settings.GROQ_API_KEY)
15
- self.model_id = settings.MODEL_ID
 
 
 
 
 
 
16
 
17
  async def analyze_provider_notes(self, provider_notes: str) -> dict:
18
  """
19
  Analyze provider notes and return ICD and CPT codes with explanations
20
  """
21
  try:
 
22
  # Create the chat completion with system and user prompts
23
  chat_completion = self.client.chat.completions.create(
24
  messages=[
@@ -38,13 +45,17 @@ class GroqService:
38
 
39
  # Extract and parse the response
40
  response_content = chat_completion.choices[0].message.content
 
41
  parsed_response = json.loads(response_content)
42
 
43
  return parsed_response
44
 
45
  except json.JSONDecodeError as e:
 
46
  raise ValueError(f"Failed to parse JSON response from model: {str(e)}")
47
  except Exception as e:
 
48
  raise Exception(f"Error calling Groq API: {str(e)}")
49
 
 
50
  groq_service = GroqService()
 
11
 
12
  class GroqService:
13
  def __init__(self):
14
+ try:
15
+ print(f"πŸ”§ Initializing Groq client...")
16
+ self.client = Groq(api_key=settings.GROQ_API_KEY)
17
+ self.model_id = settings.MODEL_ID
18
+ print(f"βœ… Groq client initialized successfully with model: {self.model_id}")
19
+ except Exception as e:
20
+ print(f"❌ Failed to initialize Groq client: {str(e)}")
21
+ raise
22
 
23
  async def analyze_provider_notes(self, provider_notes: str) -> dict:
24
  """
25
  Analyze provider notes and return ICD and CPT codes with explanations
26
  """
27
  try:
28
+ print(f"πŸ“ Analyzing provider notes...")
29
  # Create the chat completion with system and user prompts
30
  chat_completion = self.client.chat.completions.create(
31
  messages=[
 
45
 
46
  # Extract and parse the response
47
  response_content = chat_completion.choices[0].message.content
48
+ print(f"βœ… Received response from Groq API")
49
  parsed_response = json.loads(response_content)
50
 
51
  return parsed_response
52
 
53
  except json.JSONDecodeError as e:
54
+ print(f"❌ JSON parsing error: {str(e)}")
55
  raise ValueError(f"Failed to parse JSON response from model: {str(e)}")
56
  except Exception as e:
57
+ print(f"❌ Groq API error: {str(e)}")
58
  raise Exception(f"Error calling Groq API: {str(e)}")
59
 
60
+ # Don't initialize here - let it be initialized when imported
61
  groq_service = GroqService()