pangram-sdk 0.1.2__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.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Max Spero
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,60 @@
1
+ Metadata-Version: 2.1
2
+ Name: pangram-sdk
3
+ Version: 0.1.2
4
+ Summary:
5
+ Author: Max Spero
6
+ Author-email: max@pangramlabs.com
7
+ Requires-Python: >=3.7,<4.0
8
+ Classifier: Programming Language :: Python :: 3
9
+ Classifier: Programming Language :: Python :: 3.7
10
+ Classifier: Programming Language :: Python :: 3.8
11
+ Classifier: Programming Language :: Python :: 3.9
12
+ Classifier: Programming Language :: Python :: 3.10
13
+ Classifier: Programming Language :: Python :: 3.11
14
+ Classifier: Programming Language :: Python :: 3.12
15
+ Requires-Dist: requests (>=2.31.0,<3.0.0)
16
+ Description-Content-Type: text/markdown
17
+
18
+ # Pangram Labs Python Package
19
+
20
+ ### Installation
21
+ ```
22
+ pip install pangram-sdk
23
+ ```
24
+
25
+ ### Add your API key
26
+ Either export it as a variable.
27
+ ```
28
+ export PANGRAM_API_KEY=<your API key>
29
+ ```
30
+ Or pass it in to the constructor:
31
+ ```
32
+ my_api_key = '' # Fill this in with your API key.
33
+ classifier = PangramText(api_key=my_api_key)
34
+ ```
35
+
36
+ ### Make a request
37
+ ```
38
+ from pangram import PangramText
39
+
40
+ classifier = PangramText()
41
+ result = classifier.predict(text)
42
+ # Score in range [0, 1] where 0 is human-written and 1 is AI-generated.
43
+ score = result['ai_likelihood']
44
+ ```
45
+
46
+ ### Make a batch request
47
+ ```
48
+ from pangram import PangramText
49
+
50
+ text_batch = ["abc", "def"]
51
+
52
+ classifier = PangramText()
53
+ results = classifier.batch_predict(text_batch)
54
+ for result in results:
55
+ text = result['text']
56
+ score = result['ai_likelihood']
57
+ ```
58
+
59
+ Questions? Email me at max@pangramlabs.com!
60
+
@@ -0,0 +1,42 @@
1
+ # Pangram Labs Python Package
2
+
3
+ ### Installation
4
+ ```
5
+ pip install pangram-sdk
6
+ ```
7
+
8
+ ### Add your API key
9
+ Either export it as a variable.
10
+ ```
11
+ export PANGRAM_API_KEY=<your API key>
12
+ ```
13
+ Or pass it in to the constructor:
14
+ ```
15
+ my_api_key = '' # Fill this in with your API key.
16
+ classifier = PangramText(api_key=my_api_key)
17
+ ```
18
+
19
+ ### Make a request
20
+ ```
21
+ from pangram import PangramText
22
+
23
+ classifier = PangramText()
24
+ result = classifier.predict(text)
25
+ # Score in range [0, 1] where 0 is human-written and 1 is AI-generated.
26
+ score = result['ai_likelihood']
27
+ ```
28
+
29
+ ### Make a batch request
30
+ ```
31
+ from pangram import PangramText
32
+
33
+ text_batch = ["abc", "def"]
34
+
35
+ classifier = PangramText()
36
+ results = classifier.batch_predict(text_batch)
37
+ for result in results:
38
+ text = result['text']
39
+ score = result['ai_likelihood']
40
+ ```
41
+
42
+ Questions? Email me at max@pangramlabs.com!
@@ -0,0 +1,9 @@
1
+ """Defile all variables to be accessible from `import pangram`."""
2
+ __version__ = "0.1.2"
3
+ __author__ = "Max Spero"
4
+ __email__ = "max@pangramlabs.com"
5
+ __license__ = "MIT"
6
+
7
+ from pangram.text_classifier import PangramText
8
+
9
+ __all__ = ["text_classifier", "PangramText"]
@@ -0,0 +1,76 @@
1
+ import requests
2
+ import os
3
+ from typing import List
4
+
5
+ SOURCE_VERSION = "python_sdk_0.1.0"
6
+
7
+ API_ENDPOINT = 'https://text.api.pangramlabs.com'
8
+ BATCH_API_ENDPOINT = 'https://text-batch.api.pangramlabs.com'
9
+ MAX_BATCH_SIZE = 32
10
+
11
+ class PangramText:
12
+ def __init__(self, api_key: str = None, max_batch_size = MAX_BATCH_SIZE) -> None:
13
+ """
14
+ A classifier for text inputs using the Pangram Labs API.
15
+
16
+ This class provides synchronous and asynchronous methods to classify text by making API requests.
17
+
18
+ :param api_key: Your API key for the Pangram Labs. If not provided, the environment variable PANGRAM_API_KEY will be used.
19
+ :type api_key: str, optional
20
+ :raises ValueError: If the API key is not provided and not set in the environment.
21
+ """
22
+ if api_key is None:
23
+ self.api_key = os.getenv('PANGRAM_API_KEY')
24
+ if self.api_key is None:
25
+ raise ValueError("API key is required. Set the environment variable PANGRAM_API_KEY or pass it as an argument.")
26
+ else:
27
+ self.api_key = api_key
28
+ self.max_batch_size = max_batch_size
29
+
30
+
31
+ def predict(self, text: str):
32
+ """
33
+ Classify text as AI- or human-written.
34
+
35
+ Sends a request to the Pangram Text API and returns the classification result.
36
+
37
+ :param text: The text to be classified.
38
+ :type text: str
39
+ :return: The classification result from the API.
40
+ :rtype: dict
41
+ """
42
+ headers = {
43
+ 'Content-Type': 'application/json',
44
+ 'x-api-key': self.api_key,
45
+ }
46
+ input_json = {
47
+ "text": text,
48
+ "source": SOURCE_VERSION,
49
+ }
50
+ response = requests.post(API_ENDPOINT, json=input_json, headers=headers, timeout=90)
51
+ return response.json()
52
+
53
+
54
+ def batch_predict(self, text_batch: List[str]):
55
+ """
56
+ Classify a batch of text as AI- or human-written.
57
+
58
+ This method sends a batch of text to the Pangram Text API and returns the classification results.
59
+
60
+ :param text_batch: A list of strings to be classified.
61
+ :type text_batch: List[str]
62
+ :return: A list of classification results from the API for each text in the batch.
63
+ :rtype: List[dict]
64
+ """
65
+ if len(text_batch) > self.max_batch_size:
66
+ raise ValueError(f"Maximum batch size is {self.max_batch_size}.")
67
+ headers = {
68
+ 'Content-Type': 'application/json',
69
+ 'x-api-key': self.api_key,
70
+ }
71
+ input_json = {
72
+ "text": text_batch,
73
+ "source": SOURCE_VERSION,
74
+ }
75
+ response = requests.post(BATCH_API_ENDPOINT, json=input_json, headers=headers, timeout=90)
76
+ return response.json()["responses"]
@@ -0,0 +1,21 @@
1
+ [tool.poetry]
2
+ name = "pangram-sdk"
3
+ version = "0.1.2"
4
+ description = ""
5
+ authors = ["Max Spero <max@pangramlabs.com>"]
6
+ readme = "README.md"
7
+ packages = [
8
+ { include = "pangram" }
9
+ ]
10
+
11
+ [tool.poetry.dependencies]
12
+ python = "^3.7"
13
+ requests = "^2.31.0"
14
+
15
+ [tool.poetry.dev-dependencies]
16
+ sphinx = "^3.0"
17
+ sphinxcontrib-httpdomain = "*"
18
+
19
+ [build-system]
20
+ requires = ["poetry-core"]
21
+ build-backend = "poetry.core.masonry.api"