summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorkqlio67 <kqlio67@users.noreply.github.com>2024-09-07 00:16:11 +0200
committerkqlio67 <kqlio67@users.noreply.github.com>2024-09-07 00:16:11 +0200
commite69e71875f7b5e3d8b267ef5e1cbb2ec9d5aece8 (patch)
tree94850fac9cfe3a6ba3d304e2446cc801d3901a78
parentAdding a new provider Free2GPT (diff)
downloadgpt4free-e69e71875f7b5e3d8b267ef5e1cbb2ec9d5aece8.tar
gpt4free-e69e71875f7b5e3d8b267ef5e1cbb2ec9d5aece8.tar.gz
gpt4free-e69e71875f7b5e3d8b267ef5e1cbb2ec9d5aece8.tar.bz2
gpt4free-e69e71875f7b5e3d8b267ef5e1cbb2ec9d5aece8.tar.lz
gpt4free-e69e71875f7b5e3d8b267ef5e1cbb2ec9d5aece8.tar.xz
gpt4free-e69e71875f7b5e3d8b267ef5e1cbb2ec9d5aece8.tar.zst
gpt4free-e69e71875f7b5e3d8b267ef5e1cbb2ec9d5aece8.zip
-rw-r--r--g4f/Provider/MagickPen.py111
-rw-r--r--g4f/Provider/MagickPenAsk.py51
-rw-r--r--g4f/Provider/MagickPenChat.py50
-rw-r--r--g4f/Provider/__init__.py3
-rw-r--r--g4f/models.py11
5 files changed, 115 insertions, 111 deletions
diff --git a/g4f/Provider/MagickPen.py b/g4f/Provider/MagickPen.py
new file mode 100644
index 00000000..0f476eca
--- /dev/null
+++ b/g4f/Provider/MagickPen.py
@@ -0,0 +1,111 @@
+from __future__ import annotations
+
+import time
+import random
+import hashlib
+import re
+from aiohttp import ClientSession
+
+from ..typing import AsyncResult, Messages
+from .base_provider import AsyncGeneratorProvider, ProviderModelMixin
+from .helper import format_prompt
+
+class MagickPen(AsyncGeneratorProvider, ProviderModelMixin):
+ url = "https://magickpen.com"
+ api_endpoint = "https://api.magickpen.com/chat/free"
+ working = True
+ supports_gpt_4 = True
+ supports_stream = False
+
+ default_model = 'gpt-4o-mini'
+ models = ['gpt-4o-mini']
+
+ model_aliases = {}
+
+ @classmethod
+ def get_model(cls, model: str) -> str:
+ if model in cls.models:
+ return model
+ elif model in cls.model_aliases:
+ return cls.model_aliases[model]
+ else:
+ return cls.default_model
+
+ @classmethod
+ async def get_secrets(cls):
+ url = 'https://magickpen.com/_nuxt/02c76dc.js'
+ async with ClientSession() as session:
+ async with session.get(url) as response:
+ if response.status == 200:
+ text = await response.text()
+ x_api_secret_match = re.search(r'"X-API-Secret":"([^"]+)"', text)
+ secret_match = re.search(r'secret:\s*"([^"]+)"', text)
+
+ x_api_secret = x_api_secret_match.group(1) if x_api_secret_match else None
+ secret = secret_match.group(1) if secret_match else None
+
+ # Generate timestamp and nonce dynamically
+ timestamp = str(int(time.time() * 1000))
+ nonce = str(random.random())
+
+ # Generate signature
+ signature_parts = ["TGDBU9zCgM", timestamp, nonce]
+ signature_string = "".join(sorted(signature_parts))
+ signature = hashlib.md5(signature_string.encode()).hexdigest()
+
+ return {
+ 'X-API-Secret': x_api_secret,
+ 'signature': signature,
+ 'timestamp': timestamp,
+ 'nonce': nonce,
+ 'secret': secret
+ }
+ else:
+ print(f"Error while fetching the file: {response.status}")
+ return None
+
+ @classmethod
+ async def create_async_generator(
+ cls,
+ model: str,
+ messages: Messages,
+ proxy: str = None,
+ **kwargs
+ ) -> AsyncResult:
+ model = cls.get_model(model)
+
+ secrets = await cls.get_secrets()
+ if not secrets:
+ raise Exception("Failed to obtain necessary secrets")
+
+ headers = {
+ "accept": "application/json, text/plain, */*",
+ "accept-language": "en-US,en;q=0.9",
+ "cache-control": "no-cache",
+ "content-type": "application/json",
+ "nonce": secrets['nonce'],
+ "origin": "https://magickpen.com",
+ "pragma": "no-cache",
+ "priority": "u=1, i",
+ "referer": "https://magickpen.com/",
+ "sec-ch-ua": '"Chromium";v="127", "Not)A;Brand";v="99"',
+ "sec-ch-ua-mobile": "?0",
+ "sec-ch-ua-platform": '"Linux"',
+ "sec-fetch-dest": "empty",
+ "sec-fetch-mode": "cors",
+ "sec-fetch-site": "same-site",
+ "secret": secrets['secret'],
+ "signature": secrets['signature'],
+ "timestamp": secrets['timestamp'],
+ "user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36",
+ "x-api-secret": secrets['X-API-Secret']
+ }
+
+ async with ClientSession(headers=headers) as session:
+ data = {
+ "history": [{"role": "user", "content": format_prompt(messages)}]
+ }
+ async with session.post(cls.api_endpoint, json=data, proxy=proxy) as response:
+ response.raise_for_status()
+ result = await response.text()
+ yield result
diff --git a/g4f/Provider/MagickPenAsk.py b/g4f/Provider/MagickPenAsk.py
deleted file mode 100644
index 39f062f0..00000000
--- a/g4f/Provider/MagickPenAsk.py
+++ /dev/null
@@ -1,51 +0,0 @@
-from __future__ import annotations
-
-from aiohttp import ClientSession
-
-from ..typing import AsyncResult, Messages
-from .base_provider import AsyncGeneratorProvider, ProviderModelMixin
-from .helper import format_prompt
-
-
-class MagickPenAsk(AsyncGeneratorProvider, ProviderModelMixin):
- url = "https://magickpen.com/ask"
- api_endpoint = "https://api.magickpen.com/ask"
- working = True
- supports_gpt_4 = True
- default_model = "gpt-4o-mini"
-
- @classmethod
- async def create_async_generator(
- cls,
- model: str,
- messages: Messages,
- proxy: str = None,
- **kwargs
- ) -> AsyncResult:
- headers = {
- "accept": "application/json, text/plain, */*",
- "accept-language": "en-US,en;q=0.9",
- "content-type": "application/json",
- "dnt": "1",
- "origin": "https://magickpen.com",
- "priority": "u=1, i",
- "referer": "https://magickpen.com/",
- "sec-ch-ua": '"Not/A)Brand";v="8", "Chromium";v="126"',
- "sec-ch-ua-mobile": "?0",
- "sec-ch-ua-platform": '"Linux"',
- "sec-fetch-dest": "empty",
- "sec-fetch-mode": "cors",
- "sec-fetch-site": "same-site",
- "user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36",
- 'X-API-Secret': 'W252GY255JVYBS9NAM'
- }
- async with ClientSession(headers=headers) as session:
- data = {
- "query": format_prompt(messages),
- "plan": "Pay as you go"
- }
- async with session.post(f"{cls.api_endpoint}", json=data, proxy=proxy) as response:
- response.raise_for_status()
- async for chunk in response.content:
- if chunk:
- yield chunk.decode()
diff --git a/g4f/Provider/MagickPenChat.py b/g4f/Provider/MagickPenChat.py
deleted file mode 100644
index ade85c4b..00000000
--- a/g4f/Provider/MagickPenChat.py
+++ /dev/null
@@ -1,50 +0,0 @@
-from __future__ import annotations
-
-from aiohttp import ClientSession
-
-from ..typing import AsyncResult, Messages
-from .base_provider import AsyncGeneratorProvider, ProviderModelMixin
-from .helper import format_prompt
-
-
-class MagickPenChat(AsyncGeneratorProvider, ProviderModelMixin):
- url = "https://magickpen.com/chat"
- api_endpoint = "https://api.magickpen.com/chat/free"
- working = True
- supports_gpt_4 = True
- default_model = "gpt-4o-mini"
- @classmethod
- async def create_async_generator(
- cls,
- model: str,
- messages: Messages,
- proxy: str = None,
- **kwargs
- ) -> AsyncResult:
- headers = {
- "accept": "application/json, text/plain, */*",
- "accept-language": "en-US,en;q=0.9",
- "access-control-allow-origin": "*",
- "content-type": "application/json",
- "dnt": "1",
- "origin": "https://magickpen.com",
- "priority": "u=1, i",
- "referer": "https://magickpen.com/",
- "sec-ch-ua": '"Not/A)Brand";v="8", "Chromium";v="126"',
- "sec-ch-ua-mobile": "?0",
- "sec-ch-ua-platform": '"Linux"',
- "sec-fetch-dest": "empty",
- "sec-fetch-mode": "cors",
- "sec-fetch-site": "same-site",
- "user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36",
- 'X-Api-Secret': 'W252GY255JVYBS9NAM'
- }
- async with ClientSession(headers=headers) as session:
- data = {
- "history": [{"role": "user", "content": format_prompt(messages)}]
- }
- async with session.post(f"{cls.api_endpoint}", json=data, proxy=proxy) as response:
- response.raise_for_status()
- async for chunk in response.content:
- if chunk:
- yield chunk.decode()
diff --git a/g4f/Provider/__init__.py b/g4f/Provider/__init__.py
index a3126ddb..a9a815ea 100644
--- a/g4f/Provider/__init__.py
+++ b/g4f/Provider/__init__.py
@@ -43,8 +43,7 @@ from .Liaobots import Liaobots
from .LiteIcoding import LiteIcoding
from .Llama import Llama
from .Local import Local
-from .MagickPenAsk import MagickPenAsk
-from .MagickPenChat import MagickPenChat
+from .MagickPen import MagickPen
from .MetaAI import MetaAI
from .MetaAIAccount import MetaAIAccount
from .Nexra import Nexra
diff --git a/g4f/models.py b/g4f/models.py
index 7b42165b..9607658c 100644
--- a/g4f/models.py
+++ b/g4f/models.py
@@ -30,8 +30,7 @@ from .Provider import (
HuggingFace,
Koala,
Liaobots,
- MagickPenAsk,
- MagickPenChat,
+ MagickPen,
MetaAI,
Nexra,
OpenaiChat,
@@ -72,14 +71,9 @@ default = Model(
name = "",
base_provider = "",
best_provider = IterListProvider([
- ChatGot,
- Chatgpt4Online,
DDG,
FreeChatgpt,
- FreeNetfly,
HuggingChat,
- MagickPenAsk,
- MagickPenChat,
Pizzagpt,
ChatgptFree,
ReplicateHome,
@@ -127,7 +121,8 @@ gpt_4o_mini = Model(
name = 'gpt-4o-mini',
base_provider = 'OpenAI',
best_provider = IterListProvider([
- DDG, Liaobots, You, FreeNetfly, MagickPenAsk, MagickPenChat, Pizzagpt, ChatgptFree, AiChatOnline, CodeNews, OpenaiChat, Koala,
+ DDG, Liaobots, You, FreeNetfly, Pizzagpt, ChatgptFree, AiChatOnline, CodeNews,
+ MagickPen, OpenaiChat, Koala,
])
)