Skip to content

Commit c8b4259

Browse files
authored
Merge pull request #3 from Haste171/async
Add Async
2 parents 0ac8d8c + 58a71db commit c8b4259

File tree

4 files changed

+57
-6
lines changed

4 files changed

+57
-6
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.idea/

gptzero/api.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import os
2+
import aiohttp
23
import requests
34

45

6+
57
class GPTZeroAPI:
68
def __init__(self, api_key):
79
self.api_key = api_key
@@ -31,3 +33,35 @@ def file_predict(self, file_path):
3133
}
3234
response = requests.post(url, headers=headers, files=files)
3335
return response.json()
36+
37+
38+
class AsyncGPTZeroAPI:
39+
def __init__(self, api_key):
40+
self.api_key = api_key
41+
self.base_url = 'https://api.gptzero.me/v2/predict'
42+
43+
44+
async def text_predict(self, session: aiohttp.ClientSession, document):
45+
url = f'{self.base_url}/text'
46+
headers = {
47+
'accept': 'application/json',
48+
'X-Api-Key': self.api_key,
49+
'Content-Type': 'application/json'
50+
}
51+
data = {
52+
'document': document
53+
}
54+
async with session.get(url, headers=headers, json=data, ssl=False) as response:
55+
return await response.json()
56+
57+
async def file_predict(self, session: aiohttp.ClientSession, file_path):
58+
url = f'{self.base_url}/files'
59+
headers = {
60+
'accept': 'application/json',
61+
'X-Api-Key': self.api_key
62+
}
63+
files = {
64+
'files': (os.path.basename(file_path), open(file_path, 'rb'))
65+
}
66+
async with session.get(url, headers=headers, files=files, ssl=False) as response:
67+
return await response.json()

requirements.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
aiohttp==3.8.4
2+
aiosignal==1.3.1
3+
async-timeout==4.0.2
4+
attrs==22.2.0
5+
certifi==2022.12.7
6+
charset-normalizer==3.1.0
7+
frozenlist==1.3.3
8+
idna==3.4
9+
multidict==6.0.4
10+
urllib3==1.26.15
11+
yarl==1.8.2
12+
13+
requests~=2.28.2
14+
setuptools~=65.5.1

setup.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22

33
setup(
44
name='gptzero',
5-
version='0.1.1',
6-
description='Python wrapper for the GPTZero API',
7-
author='David Peterson',
8-
author_email='dapanon@proton.me',
9-
url='https://github.com/haste171/gptzero',
5+
version='0.1.2',
6+
description='Asynchronous Python wrapper for the GPTZero API',
7+
8+
author="David Peterson",
9+
author_email="dapanon@proton.me",
10+
url='https://github.com/Haste171/gptzero/',
1011
packages=find_packages(),
11-
install_requires=['requests'],
12+
install_requires=['aiohttp'],
1213
classifiers=[
1314
'Development Status :: 3 - Alpha',
1415
'Intended Audience :: Developers',
@@ -19,6 +20,7 @@
1920
'Programming Language :: Python :: 3.8',
2021
'Programming Language :: Python :: 3.9',
2122
'Programming Language :: Python :: 3.10',
23+
'Programming Language :: Python :: 3.11',
2224
],
2325
long_description=open('README.md').read(),
2426
long_description_content_type='text/markdown',

0 commit comments

Comments
 (0)