castleguard-sdk 0.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- castleguard_sdk-0.1/PKG-INFO +126 -0
- castleguard_sdk-0.1/README.md +113 -0
- castleguard_sdk-0.1/castleguard_sdk/__init__.py +1 -0
- castleguard_sdk-0.1/castleguard_sdk/castleguard.py +417 -0
- castleguard_sdk-0.1/castleguard_sdk.egg-info/PKG-INFO +126 -0
- castleguard_sdk-0.1/castleguard_sdk.egg-info/SOURCES.txt +9 -0
- castleguard_sdk-0.1/castleguard_sdk.egg-info/dependency_links.txt +1 -0
- castleguard_sdk-0.1/castleguard_sdk.egg-info/requires.txt +1 -0
- castleguard_sdk-0.1/castleguard_sdk.egg-info/top_level.txt +1 -0
- castleguard_sdk-0.1/setup.cfg +4 -0
- castleguard_sdk-0.1/setup.py +16 -0
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: castleguard_sdk
|
|
3
|
+
Version: 0.1
|
|
4
|
+
Summary: A Python SDK for interacting with CastleGuard APIs
|
|
5
|
+
Home-page: UNKNOWN
|
|
6
|
+
Author: Ravi Ramsaran
|
|
7
|
+
Author-email: ravi.ramsaran@nextria.ca
|
|
8
|
+
License: UNKNOWN
|
|
9
|
+
Platform: UNKNOWN
|
|
10
|
+
Description-Content-Type: text/markdown
|
|
11
|
+
|
|
12
|
+
# CastleGuard Python SDK
|
|
13
|
+
|
|
14
|
+
The CastleGuard Python SDK provides a convenient interface to interact with CastleGuard's API. This SDK allows you to authenticate, log messages, interact with the chatbot, translate text, perform named entity recognition (NER), transcribe audio files, manage document collections, and more.
|
|
15
|
+
|
|
16
|
+
## Features
|
|
17
|
+
|
|
18
|
+
- **Authentication**: Easily authenticate and retrieve an access token to interact with CastleGuard APIs.
|
|
19
|
+
- **Logging**: Send log messages to CastleGuard's logging endpoint.
|
|
20
|
+
- **Chatbot Integration**: Interact with CastleGuard's chatbot to generate responses based on input prompts, with or without document collections.
|
|
21
|
+
- **Translation**: Translate text between languages, supporting English and French.
|
|
22
|
+
- **Named Entity Recognition (NER)**: Perform NER on text to extract important entities.
|
|
23
|
+
- **Transcription**: Upload and transcribe audio files, and download SRT subtitles.
|
|
24
|
+
- **Collections**: Manage document collections by creating, uploading, and retrieving the status of files.
|
|
25
|
+
|
|
26
|
+
## Installation
|
|
27
|
+
|
|
28
|
+
Install the package via pip:
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
pip install castleguard
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Usage
|
|
35
|
+
|
|
36
|
+
### Initialization
|
|
37
|
+
|
|
38
|
+
To begin, initialize the `CastleGuard` class with your API credentials:
|
|
39
|
+
|
|
40
|
+
```python
|
|
41
|
+
from castleguard import CastleGuard
|
|
42
|
+
|
|
43
|
+
cg = CastleGuard(base_url='https://your-castleguard-url', username='your-username', password='your-password')
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
### Authentication
|
|
47
|
+
|
|
48
|
+
The class automatically handles authentication. The access token is retrieved upon initialization and used for subsequent API requests.
|
|
49
|
+
|
|
50
|
+
### Logging
|
|
51
|
+
|
|
52
|
+
Log a message with a specific log level (default is 1):
|
|
53
|
+
|
|
54
|
+
```python
|
|
55
|
+
cg.log("This is a log message", logLevel=2)
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
### Chatbot Interaction
|
|
59
|
+
|
|
60
|
+
Send a prompt to the chatbot and get a response:
|
|
61
|
+
|
|
62
|
+
```python
|
|
63
|
+
response, chat_id = cg.chat("What is the weather today?")
|
|
64
|
+
print(response)
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
You can also chat within the context of a document collection:
|
|
68
|
+
|
|
69
|
+
```python
|
|
70
|
+
response, chat_id = cg.chat_with_collection("Summarize the document", collection_id="your-collection-id")
|
|
71
|
+
print(response)
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
### Text Translation
|
|
75
|
+
|
|
76
|
+
Translate text from English to French (or specify different languages):
|
|
77
|
+
|
|
78
|
+
```python
|
|
79
|
+
translated_text = cg.translate_text("Hello, how are you?", source_lang="en", target_lang="fr")
|
|
80
|
+
print(translated_text)
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
### Named Entity Recognition (NER)
|
|
84
|
+
|
|
85
|
+
Perform NER on text to extract entities:
|
|
86
|
+
|
|
87
|
+
```python
|
|
88
|
+
entities = cg.named_entity_recognition("John Doe works at Google in Mountain View.")
|
|
89
|
+
print(entities)
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
### Transcription
|
|
93
|
+
|
|
94
|
+
Upload an audio file and transcribe it:
|
|
95
|
+
|
|
96
|
+
```python
|
|
97
|
+
document_id = cg.transcribe("/path/to/audiofile.mp3")
|
|
98
|
+
srt_text = cg.download_srt(document_id)
|
|
99
|
+
print(srt_text)
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
### Document Collections
|
|
103
|
+
|
|
104
|
+
Create a new document collection:
|
|
105
|
+
|
|
106
|
+
```python
|
|
107
|
+
collection_id = cg.create_collection(name="My Collection", description="A collection of legal documents")
|
|
108
|
+
print(f"Collection created with ID: {collection_id}")
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
Upload a document to the collection:
|
|
112
|
+
|
|
113
|
+
```python
|
|
114
|
+
upload_success = cg.upload_to_collection(collection_id, "/path/to/document.pdf")
|
|
115
|
+
print(f"Upload successful: {upload_success}")
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
## Contributing
|
|
119
|
+
|
|
120
|
+
Contributions are welcome! Please fork the repository and submit a pull request.
|
|
121
|
+
|
|
122
|
+
## License
|
|
123
|
+
|
|
124
|
+
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
|
|
125
|
+
|
|
126
|
+
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
# CastleGuard Python SDK
|
|
2
|
+
|
|
3
|
+
The CastleGuard Python SDK provides a convenient interface to interact with CastleGuard's API. This SDK allows you to authenticate, log messages, interact with the chatbot, translate text, perform named entity recognition (NER), transcribe audio files, manage document collections, and more.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **Authentication**: Easily authenticate and retrieve an access token to interact with CastleGuard APIs.
|
|
8
|
+
- **Logging**: Send log messages to CastleGuard's logging endpoint.
|
|
9
|
+
- **Chatbot Integration**: Interact with CastleGuard's chatbot to generate responses based on input prompts, with or without document collections.
|
|
10
|
+
- **Translation**: Translate text between languages, supporting English and French.
|
|
11
|
+
- **Named Entity Recognition (NER)**: Perform NER on text to extract important entities.
|
|
12
|
+
- **Transcription**: Upload and transcribe audio files, and download SRT subtitles.
|
|
13
|
+
- **Collections**: Manage document collections by creating, uploading, and retrieving the status of files.
|
|
14
|
+
|
|
15
|
+
## Installation
|
|
16
|
+
|
|
17
|
+
Install the package via pip:
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
pip install castleguard
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Usage
|
|
24
|
+
|
|
25
|
+
### Initialization
|
|
26
|
+
|
|
27
|
+
To begin, initialize the `CastleGuard` class with your API credentials:
|
|
28
|
+
|
|
29
|
+
```python
|
|
30
|
+
from castleguard import CastleGuard
|
|
31
|
+
|
|
32
|
+
cg = CastleGuard(base_url='https://your-castleguard-url', username='your-username', password='your-password')
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
### Authentication
|
|
36
|
+
|
|
37
|
+
The class automatically handles authentication. The access token is retrieved upon initialization and used for subsequent API requests.
|
|
38
|
+
|
|
39
|
+
### Logging
|
|
40
|
+
|
|
41
|
+
Log a message with a specific log level (default is 1):
|
|
42
|
+
|
|
43
|
+
```python
|
|
44
|
+
cg.log("This is a log message", logLevel=2)
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
### Chatbot Interaction
|
|
48
|
+
|
|
49
|
+
Send a prompt to the chatbot and get a response:
|
|
50
|
+
|
|
51
|
+
```python
|
|
52
|
+
response, chat_id = cg.chat("What is the weather today?")
|
|
53
|
+
print(response)
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
You can also chat within the context of a document collection:
|
|
57
|
+
|
|
58
|
+
```python
|
|
59
|
+
response, chat_id = cg.chat_with_collection("Summarize the document", collection_id="your-collection-id")
|
|
60
|
+
print(response)
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
### Text Translation
|
|
64
|
+
|
|
65
|
+
Translate text from English to French (or specify different languages):
|
|
66
|
+
|
|
67
|
+
```python
|
|
68
|
+
translated_text = cg.translate_text("Hello, how are you?", source_lang="en", target_lang="fr")
|
|
69
|
+
print(translated_text)
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
### Named Entity Recognition (NER)
|
|
73
|
+
|
|
74
|
+
Perform NER on text to extract entities:
|
|
75
|
+
|
|
76
|
+
```python
|
|
77
|
+
entities = cg.named_entity_recognition("John Doe works at Google in Mountain View.")
|
|
78
|
+
print(entities)
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
### Transcription
|
|
82
|
+
|
|
83
|
+
Upload an audio file and transcribe it:
|
|
84
|
+
|
|
85
|
+
```python
|
|
86
|
+
document_id = cg.transcribe("/path/to/audiofile.mp3")
|
|
87
|
+
srt_text = cg.download_srt(document_id)
|
|
88
|
+
print(srt_text)
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
### Document Collections
|
|
92
|
+
|
|
93
|
+
Create a new document collection:
|
|
94
|
+
|
|
95
|
+
```python
|
|
96
|
+
collection_id = cg.create_collection(name="My Collection", description="A collection of legal documents")
|
|
97
|
+
print(f"Collection created with ID: {collection_id}")
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
Upload a document to the collection:
|
|
101
|
+
|
|
102
|
+
```python
|
|
103
|
+
upload_success = cg.upload_to_collection(collection_id, "/path/to/document.pdf")
|
|
104
|
+
print(f"Upload successful: {upload_success}")
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
## Contributing
|
|
108
|
+
|
|
109
|
+
Contributions are welcome! Please fork the repository and submit a pull request.
|
|
110
|
+
|
|
111
|
+
## License
|
|
112
|
+
|
|
113
|
+
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
from .castleguard import CastleGuard
|
|
@@ -0,0 +1,417 @@
|
|
|
1
|
+
import requests
|
|
2
|
+
import json
|
|
3
|
+
import time
|
|
4
|
+
from datetime import datetime
|
|
5
|
+
|
|
6
|
+
class CastleGuard:
|
|
7
|
+
def __init__(self, base_url, username, password):
|
|
8
|
+
"""
|
|
9
|
+
Initialize the CastleGuard class with base credentials.
|
|
10
|
+
|
|
11
|
+
:param base_url: Base URL for the CastleGuard API.
|
|
12
|
+
:param username: Username for authentication.
|
|
13
|
+
:param password: Password for authentication.
|
|
14
|
+
"""
|
|
15
|
+
self.base_url = base_url
|
|
16
|
+
self.username = username
|
|
17
|
+
self.password = password
|
|
18
|
+
self.token = None
|
|
19
|
+
self.authenticate()
|
|
20
|
+
|
|
21
|
+
def authenticate(self):
|
|
22
|
+
"""
|
|
23
|
+
Authenticates the user and retrieves the access token.
|
|
24
|
+
|
|
25
|
+
:return: Access token or None if authentication fails.
|
|
26
|
+
"""
|
|
27
|
+
url = f'{self.base_url}/auth/token'
|
|
28
|
+
payload = {
|
|
29
|
+
"account": self.username,
|
|
30
|
+
"password": self.password
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
try:
|
|
34
|
+
response = requests.post(url, json=payload)
|
|
35
|
+
if response.status_code == 200:
|
|
36
|
+
self.token = response.json().get('token')
|
|
37
|
+
if self.token:
|
|
38
|
+
print("Authentication successful.")
|
|
39
|
+
else:
|
|
40
|
+
print("Failed to retrieve token from authentication response.")
|
|
41
|
+
else:
|
|
42
|
+
print(f"Failed to authenticate: {response.status_code} - {response.text}")
|
|
43
|
+
except requests.RequestException as e:
|
|
44
|
+
print(f"Authentication request failed: {e}")
|
|
45
|
+
self.token = None
|
|
46
|
+
|
|
47
|
+
def log(self, message, logLevel=1):
|
|
48
|
+
"""
|
|
49
|
+
Sends log messages to the CastleGuard logging endpoint as URL parameters.
|
|
50
|
+
|
|
51
|
+
:param message: Log message.
|
|
52
|
+
:param logLevel: Log level (0-6).
|
|
53
|
+
"""
|
|
54
|
+
if not self.token:
|
|
55
|
+
print("Cannot log message because authentication failed.")
|
|
56
|
+
return
|
|
57
|
+
|
|
58
|
+
url = f'{self.base_url}/logger'
|
|
59
|
+
headers = {
|
|
60
|
+
'Authorization': f'Bearer {self.token}'
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
params = {
|
|
64
|
+
'message': message,
|
|
65
|
+
'logLevel': logLevel
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
try:
|
|
69
|
+
response = requests.post(url, headers=headers, params=params)
|
|
70
|
+
if response.status_code == 200:
|
|
71
|
+
print(f"Logging: {message}")
|
|
72
|
+
else:
|
|
73
|
+
print(f"Failed to send log: {response.status_code} - {response.text}")
|
|
74
|
+
except requests.RequestException as e:
|
|
75
|
+
print(f"Log request failed: {e}")
|
|
76
|
+
|
|
77
|
+
def chat(self, prompt, chat_id=None):
|
|
78
|
+
"""
|
|
79
|
+
Interacts with the chat endpoint to generate a response from the model.
|
|
80
|
+
|
|
81
|
+
:param prompt: The input prompt to send to the model.
|
|
82
|
+
:param chat_id: Optional chat session ID.
|
|
83
|
+
:return: Chatbot response or 'Unknown' if the request fails.
|
|
84
|
+
"""
|
|
85
|
+
chat_url = f'{self.base_url}/chat-completion/chat'
|
|
86
|
+
headers = {
|
|
87
|
+
'Authorization': f'Bearer {self.token}',
|
|
88
|
+
'Content-Type': 'application/json'
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
# Reuse the same Chat ID for the same session, or create a new one
|
|
92
|
+
if not chat_id:
|
|
93
|
+
params = {
|
|
94
|
+
"displayName": "Chat " + datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
|
95
|
+
}
|
|
96
|
+
chat_response = requests.post(chat_url, headers=headers, params=params)
|
|
97
|
+
if chat_response.status_code == 200:
|
|
98
|
+
chat_id = json.loads(chat_response.text).get('id')
|
|
99
|
+
else:
|
|
100
|
+
self.log("Failed to create chat session", logLevel=3)
|
|
101
|
+
return "Unknown", None
|
|
102
|
+
|
|
103
|
+
# Post a message to the chat
|
|
104
|
+
|
|
105
|
+
message_url = f'{self.base_url}/chat-completion/completions'
|
|
106
|
+
message_payload = {
|
|
107
|
+
"chatId": chat_id,
|
|
108
|
+
"prompt": prompt,
|
|
109
|
+
"model": "default", # replace with actual model if needed
|
|
110
|
+
"bestOf": 0,
|
|
111
|
+
"echo": True,
|
|
112
|
+
"frequencyPenalty": 0,
|
|
113
|
+
"logitBias": {},
|
|
114
|
+
"logprobs": 0,
|
|
115
|
+
"maxTokens": 0,
|
|
116
|
+
"n": 0,
|
|
117
|
+
"presencePenalty": 0,
|
|
118
|
+
"seed": 0,
|
|
119
|
+
"stop": True,
|
|
120
|
+
"stream": True,
|
|
121
|
+
"streamOptions": "string",
|
|
122
|
+
"suffix": "string",
|
|
123
|
+
"temperature": 0,
|
|
124
|
+
"topP": 0,
|
|
125
|
+
"user": "string"
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
message_response = requests.post(message_url, json=message_payload, headers=headers)
|
|
129
|
+
if message_response.status_code == 200:
|
|
130
|
+
response_dict = json.loads(message_response.text)
|
|
131
|
+
bot_message = response_dict.get('botMessage', {}).get('chatMessage')
|
|
132
|
+
return bot_message, chat_id
|
|
133
|
+
else:
|
|
134
|
+
self.log(f"Failed to get response for prompt: {prompt}", logLevel=3)
|
|
135
|
+
return "Unknown", chat_id
|
|
136
|
+
|
|
137
|
+
def chat_with_collection(self, prompt, collection_id=None):
|
|
138
|
+
"""
|
|
139
|
+
Interacts with the chat endpoint to generate a response from the model.
|
|
140
|
+
|
|
141
|
+
:param prompt: The input prompt to send to the model.
|
|
142
|
+
:param chat_id: Optional chat session ID.
|
|
143
|
+
:return: Chatbot response or 'Unknown' if the request fails.
|
|
144
|
+
"""
|
|
145
|
+
chat_url = f'{self.base_url}/chat-completion/chat'
|
|
146
|
+
headers = {
|
|
147
|
+
'Authorization': f'Bearer {self.token}',
|
|
148
|
+
'Content-Type': 'application/json'
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
chat_id = None
|
|
152
|
+
params = {
|
|
153
|
+
"displayName": "Chat " + datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
|
|
154
|
+
"collectionId": collection_id
|
|
155
|
+
}
|
|
156
|
+
chat_response = requests.post(chat_url, headers=headers, params=params)
|
|
157
|
+
if chat_response.status_code == 200:
|
|
158
|
+
chat_id = json.loads(chat_response.text).get('id')
|
|
159
|
+
# Post a message to the chat
|
|
160
|
+
message_url = f'{self.base_url}/chat-completion/completions'
|
|
161
|
+
message_payload = {
|
|
162
|
+
"chatId": chat_id,
|
|
163
|
+
"prompt": prompt,
|
|
164
|
+
"model": "default", # replace with actual model if needed
|
|
165
|
+
"bestOf": 0,
|
|
166
|
+
"echo": True,
|
|
167
|
+
"frequencyPenalty": 0,
|
|
168
|
+
"logitBias": {},
|
|
169
|
+
"logprobs": 0,
|
|
170
|
+
"maxTokens": 0,
|
|
171
|
+
"n": 0,
|
|
172
|
+
"presencePenalty": 0,
|
|
173
|
+
"seed": 0,
|
|
174
|
+
"stop": True,
|
|
175
|
+
"stream": True,
|
|
176
|
+
"streamOptions": "string",
|
|
177
|
+
"suffix": "string",
|
|
178
|
+
"temperature": 0,
|
|
179
|
+
"topP": 0,
|
|
180
|
+
"user": "string"
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
message_response = requests.post(message_url, json=message_payload, headers=headers)
|
|
184
|
+
if message_response.status_code == 200:
|
|
185
|
+
response_dict = json.loads(message_response.text)
|
|
186
|
+
bot_message = response_dict.get('botMessage')
|
|
187
|
+
chat_message = bot_message.get('chatMessage')
|
|
188
|
+
return chat_message, chat_id
|
|
189
|
+
else:
|
|
190
|
+
self.log(f"Failed to get response for prompt: {prompt}", logLevel=3)
|
|
191
|
+
return "Unknown", chat_id
|
|
192
|
+
else:
|
|
193
|
+
self.log("Failed to create chat session", logLevel=3)
|
|
194
|
+
return "Unknown", None
|
|
195
|
+
|
|
196
|
+
|
|
197
|
+
def translate_text(self, text, source_lang='en', target_lang='fr'):
|
|
198
|
+
"""
|
|
199
|
+
Translates text from one language to another.
|
|
200
|
+
|
|
201
|
+
:param text: The text to translate.
|
|
202
|
+
:param source_lang: Source language code.
|
|
203
|
+
:param target_lang: Target language code.
|
|
204
|
+
:return: Translated text or None if the request fails.
|
|
205
|
+
"""
|
|
206
|
+
url = f'{self.base_url}/translation/instant'
|
|
207
|
+
headers = {
|
|
208
|
+
'Authorization': f'Bearer {self.token}',
|
|
209
|
+
'Content-Type': 'application/json'
|
|
210
|
+
}
|
|
211
|
+
payload = {
|
|
212
|
+
"originalText": text,
|
|
213
|
+
"sourceLanguageCode": source_lang,
|
|
214
|
+
"targetLanguageCode": target_lang
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
response = requests.post(url, json=payload, headers=headers)
|
|
218
|
+
if response.status_code == 200:
|
|
219
|
+
translated_text = response.text
|
|
220
|
+
self.log(f"Translated text: {translated_text}", logLevel=1)
|
|
221
|
+
return translated_text
|
|
222
|
+
else:
|
|
223
|
+
self.log(f"Translation failed: {response.text}", logLevel=3)
|
|
224
|
+
return None
|
|
225
|
+
|
|
226
|
+
def named_entity_recognition(self, text):
|
|
227
|
+
"""
|
|
228
|
+
Performs named entity recognition (NER) on a given text.
|
|
229
|
+
|
|
230
|
+
:param text: The input text for NER.
|
|
231
|
+
:return: Extracted entities or None if the request fails.
|
|
232
|
+
"""
|
|
233
|
+
url = f'{self.base_url}/triton/ner'
|
|
234
|
+
headers = {
|
|
235
|
+
'Authorization': f'Bearer {self.token}',
|
|
236
|
+
'Content-Type': 'application/json'
|
|
237
|
+
}
|
|
238
|
+
payload = {
|
|
239
|
+
"inputText": text
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
response = requests.post(url, json=payload, headers=headers)
|
|
243
|
+
if response.status_code == 200:
|
|
244
|
+
ner_result = response.json()
|
|
245
|
+
self.log(f"NER result: {ner_result}", logLevel=1)
|
|
246
|
+
return ner_result
|
|
247
|
+
else:
|
|
248
|
+
self.log(f"NER extraction failed: {response.text}", logLevel=3)
|
|
249
|
+
return None
|
|
250
|
+
|
|
251
|
+
# Add the transcription functionalities
|
|
252
|
+
|
|
253
|
+
def transcribe(self, file_path):
|
|
254
|
+
"""
|
|
255
|
+
Uploads and transcribes an audio/file file.
|
|
256
|
+
|
|
257
|
+
:param file_path: Path to the file to transcribe.
|
|
258
|
+
:return: Document ID if successful, None otherwise.
|
|
259
|
+
"""
|
|
260
|
+
url = f'{self.base_url}/transcription/document'
|
|
261
|
+
headers = {
|
|
262
|
+
'Authorization': f'Bearer {self.token}'
|
|
263
|
+
}
|
|
264
|
+
files = {
|
|
265
|
+
'file': open(file_path, 'rb'),
|
|
266
|
+
'taskType': (None, 'Diarize'),
|
|
267
|
+
'diarizationConfig': (None, 'General')
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
response = requests.post(url, headers=headers, files=files)
|
|
271
|
+
if response.status_code == 200:
|
|
272
|
+
document_id = response.json().get('id')
|
|
273
|
+
self.log(f"File uploaded for transcription. Document ID: {document_id}", logLevel=1)
|
|
274
|
+
status_code = self.get_transcription_document_status_code(document_id)
|
|
275
|
+
if status_code and status_code == "success":
|
|
276
|
+
return document_id
|
|
277
|
+
return None
|
|
278
|
+
else:
|
|
279
|
+
self.log(f"Failed to upload file for transcription: {response.text}", logLevel=3)
|
|
280
|
+
return None
|
|
281
|
+
|
|
282
|
+
def get_transcription_document_status_code(self, document_id):
|
|
283
|
+
"""
|
|
284
|
+
Gets the status of a transcription document.
|
|
285
|
+
|
|
286
|
+
:param document_id: The ID of the transcription document.
|
|
287
|
+
:return: Status code if successful, None otherwise.
|
|
288
|
+
"""
|
|
289
|
+
url = f'{self.base_url}/transcription/document/{document_id}'
|
|
290
|
+
headers = {
|
|
291
|
+
'Authorization': f'Bearer {self.token}'
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
status = None
|
|
295
|
+
while status != 2:
|
|
296
|
+
response = requests.get(url, headers=headers)
|
|
297
|
+
if response.status_code == 200:
|
|
298
|
+
document = response.json()
|
|
299
|
+
job_detail = document.get('jobDetail', {})
|
|
300
|
+
status = job_detail.get('status', None)
|
|
301
|
+
if status == 2:
|
|
302
|
+
status_code = job_detail.get('statusCode', None)
|
|
303
|
+
self.log(f"Transcription job completed. Status Code: {status_code}", logLevel=1)
|
|
304
|
+
return status_code
|
|
305
|
+
else:
|
|
306
|
+
self.log(f"Failed to get the transcription document: {response.text}", logLevel=3)
|
|
307
|
+
return None
|
|
308
|
+
time.sleep(5)
|
|
309
|
+
|
|
310
|
+
def download_srt(self, document_id):
|
|
311
|
+
"""
|
|
312
|
+
Downloads the SRT file from a transcription document.
|
|
313
|
+
|
|
314
|
+
:param document_id: The ID of the transcription document.
|
|
315
|
+
:return: SRT text if successful, None otherwise.
|
|
316
|
+
"""
|
|
317
|
+
url = f'{self.base_url}/transcription/document/download/{document_id}?downloadDocumentOptions=SRT'
|
|
318
|
+
headers = {
|
|
319
|
+
'Authorization': f'Bearer {self.token}'
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
response = requests.get(url, headers=headers)
|
|
323
|
+
if response.status_code == 200:
|
|
324
|
+
srt_text = response.text
|
|
325
|
+
self.log(f"SRT downloaded successfully.", logLevel=1)
|
|
326
|
+
return srt_text
|
|
327
|
+
else:
|
|
328
|
+
self.log(f"Failed to download SRT: {response.text}", logLevel=3)
|
|
329
|
+
return None
|
|
330
|
+
|
|
331
|
+
# Add the collection functionalities
|
|
332
|
+
|
|
333
|
+
def create_collection(self, name, description):
|
|
334
|
+
"""
|
|
335
|
+
Creates a new collection.
|
|
336
|
+
|
|
337
|
+
:param name: Collection name.
|
|
338
|
+
:param description: Collection description.
|
|
339
|
+
:return: Collection ID if successful, None otherwise.
|
|
340
|
+
"""
|
|
341
|
+
url = f'{self.base_url}/collections/collection'
|
|
342
|
+
headers = {
|
|
343
|
+
'Authorization': f'Bearer {self.token}',
|
|
344
|
+
'Content-Type': 'application/json'
|
|
345
|
+
}
|
|
346
|
+
params = {
|
|
347
|
+
"displayName": name,
|
|
348
|
+
"description": description
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
response = requests.post(url, headers=headers, params=params)
|
|
352
|
+
if response.status_code == 200:
|
|
353
|
+
collection_id = response.json().get('id')
|
|
354
|
+
self.log(f"Collection created successfully. Collection ID: {collection_id}", logLevel=1)
|
|
355
|
+
return collection_id
|
|
356
|
+
else:
|
|
357
|
+
self.log(f"Failed to create collection: {response.text}", logLevel=3)
|
|
358
|
+
return None
|
|
359
|
+
|
|
360
|
+
def upload_to_collection(self, collection_id, file_path):
|
|
361
|
+
"""
|
|
362
|
+
Uploads a file to a collection.
|
|
363
|
+
|
|
364
|
+
:param collection_id: The ID of the collection.
|
|
365
|
+
:param file_path: Path to the file to upload.
|
|
366
|
+
:return: True if successful, False otherwise.
|
|
367
|
+
"""
|
|
368
|
+
url = f'{self.base_url}/collections/collection/document/{collection_id}'
|
|
369
|
+
headers = {
|
|
370
|
+
'Authorization': f'Bearer {self.token}'
|
|
371
|
+
}
|
|
372
|
+
files = {
|
|
373
|
+
'file': open(file_path, 'rb')
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
response = requests.post(url, headers=headers, files=files)
|
|
377
|
+
if response.status_code == 200:
|
|
378
|
+
document_id = response.json().get('id')
|
|
379
|
+
if document_id:
|
|
380
|
+
status_code = self.get_collection_document_status_code(collection_id, document_id)
|
|
381
|
+
if status_code and status_code.lower() == "success":
|
|
382
|
+
self.log(f"File uploaded to collection {collection_id} successfully.", logLevel=1)
|
|
383
|
+
return True
|
|
384
|
+
else:
|
|
385
|
+
self.log(f"Failed to upload file to collection: {response.text}", logLevel=3)
|
|
386
|
+
return False
|
|
387
|
+
|
|
388
|
+
def get_collection_document_status_code(self, collection_id, document_id):
|
|
389
|
+
"""
|
|
390
|
+
Gets the status of a document in a collection.
|
|
391
|
+
|
|
392
|
+
:param collection_id: The ID of the collection.
|
|
393
|
+
:param document_id: The ID of the document.
|
|
394
|
+
:return: Status code if successful, None otherwise.
|
|
395
|
+
"""
|
|
396
|
+
url = f'{self.base_url}/collections/collection/documents/{collection_id}'
|
|
397
|
+
headers = {
|
|
398
|
+
'Authorization': f'Bearer {self.token}'
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
status = None
|
|
402
|
+
while status != 2:
|
|
403
|
+
response = requests.get(url, headers=headers)
|
|
404
|
+
if response.status_code == 200:
|
|
405
|
+
documents = response.json()
|
|
406
|
+
for document in documents:
|
|
407
|
+
if document.get('id') == document_id:
|
|
408
|
+
job_detail = document.get('jobDetail', {})
|
|
409
|
+
status = job_detail.get('status', None)
|
|
410
|
+
if status == 2:
|
|
411
|
+
status_code = job_detail.get('statusCode', None)
|
|
412
|
+
self.log(f"Collection document processing completed. Status Code: {status_code}", logLevel=1)
|
|
413
|
+
return status_code
|
|
414
|
+
else:
|
|
415
|
+
self.log(f"Failed to get collection document status: {response.text}", logLevel=3)
|
|
416
|
+
return None
|
|
417
|
+
time.sleep(5)
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: castleguard-sdk
|
|
3
|
+
Version: 0.1
|
|
4
|
+
Summary: A Python SDK for interacting with CastleGuard APIs
|
|
5
|
+
Home-page: UNKNOWN
|
|
6
|
+
Author: Ravi Ramsaran
|
|
7
|
+
Author-email: ravi.ramsaran@nextria.ca
|
|
8
|
+
License: UNKNOWN
|
|
9
|
+
Platform: UNKNOWN
|
|
10
|
+
Description-Content-Type: text/markdown
|
|
11
|
+
|
|
12
|
+
# CastleGuard Python SDK
|
|
13
|
+
|
|
14
|
+
The CastleGuard Python SDK provides a convenient interface to interact with CastleGuard's API. This SDK allows you to authenticate, log messages, interact with the chatbot, translate text, perform named entity recognition (NER), transcribe audio files, manage document collections, and more.
|
|
15
|
+
|
|
16
|
+
## Features
|
|
17
|
+
|
|
18
|
+
- **Authentication**: Easily authenticate and retrieve an access token to interact with CastleGuard APIs.
|
|
19
|
+
- **Logging**: Send log messages to CastleGuard's logging endpoint.
|
|
20
|
+
- **Chatbot Integration**: Interact with CastleGuard's chatbot to generate responses based on input prompts, with or without document collections.
|
|
21
|
+
- **Translation**: Translate text between languages, supporting English and French.
|
|
22
|
+
- **Named Entity Recognition (NER)**: Perform NER on text to extract important entities.
|
|
23
|
+
- **Transcription**: Upload and transcribe audio files, and download SRT subtitles.
|
|
24
|
+
- **Collections**: Manage document collections by creating, uploading, and retrieving the status of files.
|
|
25
|
+
|
|
26
|
+
## Installation
|
|
27
|
+
|
|
28
|
+
Install the package via pip:
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
pip install castleguard
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Usage
|
|
35
|
+
|
|
36
|
+
### Initialization
|
|
37
|
+
|
|
38
|
+
To begin, initialize the `CastleGuard` class with your API credentials:
|
|
39
|
+
|
|
40
|
+
```python
|
|
41
|
+
from castleguard import CastleGuard
|
|
42
|
+
|
|
43
|
+
cg = CastleGuard(base_url='https://your-castleguard-url', username='your-username', password='your-password')
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
### Authentication
|
|
47
|
+
|
|
48
|
+
The class automatically handles authentication. The access token is retrieved upon initialization and used for subsequent API requests.
|
|
49
|
+
|
|
50
|
+
### Logging
|
|
51
|
+
|
|
52
|
+
Log a message with a specific log level (default is 1):
|
|
53
|
+
|
|
54
|
+
```python
|
|
55
|
+
cg.log("This is a log message", logLevel=2)
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
### Chatbot Interaction
|
|
59
|
+
|
|
60
|
+
Send a prompt to the chatbot and get a response:
|
|
61
|
+
|
|
62
|
+
```python
|
|
63
|
+
response, chat_id = cg.chat("What is the weather today?")
|
|
64
|
+
print(response)
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
You can also chat within the context of a document collection:
|
|
68
|
+
|
|
69
|
+
```python
|
|
70
|
+
response, chat_id = cg.chat_with_collection("Summarize the document", collection_id="your-collection-id")
|
|
71
|
+
print(response)
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
### Text Translation
|
|
75
|
+
|
|
76
|
+
Translate text from English to French (or specify different languages):
|
|
77
|
+
|
|
78
|
+
```python
|
|
79
|
+
translated_text = cg.translate_text("Hello, how are you?", source_lang="en", target_lang="fr")
|
|
80
|
+
print(translated_text)
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
### Named Entity Recognition (NER)
|
|
84
|
+
|
|
85
|
+
Perform NER on text to extract entities:
|
|
86
|
+
|
|
87
|
+
```python
|
|
88
|
+
entities = cg.named_entity_recognition("John Doe works at Google in Mountain View.")
|
|
89
|
+
print(entities)
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
### Transcription
|
|
93
|
+
|
|
94
|
+
Upload an audio file and transcribe it:
|
|
95
|
+
|
|
96
|
+
```python
|
|
97
|
+
document_id = cg.transcribe("/path/to/audiofile.mp3")
|
|
98
|
+
srt_text = cg.download_srt(document_id)
|
|
99
|
+
print(srt_text)
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
### Document Collections
|
|
103
|
+
|
|
104
|
+
Create a new document collection:
|
|
105
|
+
|
|
106
|
+
```python
|
|
107
|
+
collection_id = cg.create_collection(name="My Collection", description="A collection of legal documents")
|
|
108
|
+
print(f"Collection created with ID: {collection_id}")
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
Upload a document to the collection:
|
|
112
|
+
|
|
113
|
+
```python
|
|
114
|
+
upload_success = cg.upload_to_collection(collection_id, "/path/to/document.pdf")
|
|
115
|
+
print(f"Upload successful: {upload_success}")
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
## Contributing
|
|
119
|
+
|
|
120
|
+
Contributions are welcome! Please fork the repository and submit a pull request.
|
|
121
|
+
|
|
122
|
+
## License
|
|
123
|
+
|
|
124
|
+
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
|
|
125
|
+
|
|
126
|
+
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
README.md
|
|
2
|
+
setup.py
|
|
3
|
+
castleguard_sdk/__init__.py
|
|
4
|
+
castleguard_sdk/castleguard.py
|
|
5
|
+
castleguard_sdk.egg-info/PKG-INFO
|
|
6
|
+
castleguard_sdk.egg-info/SOURCES.txt
|
|
7
|
+
castleguard_sdk.egg-info/dependency_links.txt
|
|
8
|
+
castleguard_sdk.egg-info/requires.txt
|
|
9
|
+
castleguard_sdk.egg-info/top_level.txt
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
requests
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
castleguard_sdk
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
from setuptools import setup, find_packages
|
|
2
|
+
|
|
3
|
+
setup(
|
|
4
|
+
name='castleguard_sdk',
|
|
5
|
+
version='0.1',
|
|
6
|
+
packages=find_packages(),
|
|
7
|
+
install_requires=[
|
|
8
|
+
'requests'
|
|
9
|
+
],
|
|
10
|
+
author='Ravi Ramsaran',
|
|
11
|
+
author_email='ravi.ramsaran@nextria.ca',
|
|
12
|
+
description='A Python SDK for interacting with CastleGuard APIs',
|
|
13
|
+
long_description=open('README.md').read(),
|
|
14
|
+
long_description_content_type='text/markdown'
|
|
15
|
+
)
|
|
16
|
+
|