返回提交历史
Modified
g4f/Provider/needs_auth/GeminiPro.py
+2
-3
Modified
g4f/gui/server/backend_api.py
+7
-2
Modified
g4f/image/__init__.py
+17
-4
Modified
g4f/image/copy_images.py
+2
-1
XFEstudio/gpt4free
Add ip ban
744dfeb9
代码差异
4 个文件
+28
-10
@@ -3,7 +3,6 @@ from __future__ import annotations
3
3
import base64
4
4
import json
5
5
import requests
6
import random
7
6
from typing import Optional
8
7
from aiohttp import ClientSession, BaseConnector
9
8
@@ -109,10 +108,10 @@ class GeminiPro(AsyncGeneratorProvider, ProviderModelMixin):
109
108
]
110
109
if media is not None:
111
110
for media_data, filename in media:
112
image = to_bytes(image)
111
media_data = to_bytes(media_data)
113
112
contents[-1]["parts"].append({
114
113
"inline_data": {
115
"mime_type": is_data_an_media(image, filename),
114
"mime_type": is_data_an_media(media_data, filename),
116
115
"data": base64.b64encode(media_data).decode()
117
116
}
118
117
})
@@ -135,9 +135,14 @@ class Backend_Api(Api):
135
135
else:
136
136
json_data["provider"] = models.HuggingFace
137
137
if app.demo:
138
ip = request.headers.get("X-Forwarded-For", "")
139
ip_bans = Path(get_cookies_dir()) / ".ip_bans"
140
if ip_bans.exists():
141
ip_bans = ip_bans.read_text().splitlines()
142
if (ip and ip in ip_bans):
143
return "You are banned from using this service.", 403
138
144
user = request.headers.get("Cf-Ipcountry", "")
139
ip = request.headers.get("X-Forwarded-For", "").split(":")[-1]
140
json_data["user"] = request.headers.get("x_user", f"{user}:{ip}")
145
json_data["user"] = request.headers.get("x_user", f"{user}:{ip.split(':')[-1]}")
141
146
json_data["referer"] = request.headers.get("referer", "")
142
147
json_data["user-agent"] = request.headers.get("user-agent", "")
143
148
if not json_data.get("referer") or "python" in json_data.get("user-agent", "").lower():
@@ -7,6 +7,7 @@ import base64
7
7
from io import BytesIO
8
8
from pathlib import Path
9
9
from typing import Optional
10
from urllib.parse import urlparse
10
11
11
12
try:
12
13
from PIL import Image, ImageOps
@@ -16,6 +17,7 @@ except ImportError:
16
17
17
18
from ..typing import ImageType
18
19
from ..errors import MissingRequirementsError
20
from ..tools.files import get_bucket_dir
19
21
20
22
EXTENSIONS_MAP: dict[str, str] = {
21
23
# Image
@@ -241,15 +243,26 @@ def to_bytes(image: ImageType) -> bytes:
241
243
"""
242
244
if isinstance(image, bytes):
243
245
return image
244
elif isinstance(image, str) and image.startswith("data:"):
245
is_data_an_media(image)
246
return extract_data_uri(image)
246
elif isinstance(image, str):
247
if image.startswith("data:"):
248
is_data_uri_an_image(image)
249
return extract_data_uri(image)
250
elif image.startswith("http://") or image.startswith("https://"):
251
path: str = urlparse(image).path
252
if path.startswith("/files/"):
253
path = get_bucket_dir(path.split(path, "/")[1:])
254
if os.path.exists(path):
255
return Path(path).read_bytes()
256
else:
257
raise FileNotFoundError(f"File not found: {path}")
258
else:
259
raise ValueError("Invalid image format. Expected bytes, str, or PIL Image.")
247
260
elif isinstance(image, Image):
248
261
bytes_io = BytesIO()
249
262
image.save(bytes_io, image.format)
250
263
image.seek(0)
251
264
return bytes_io.getvalue()
252
elif isinstance(image, (str, os.PathLike)):
265
elif isinstance(image, os.PathLike):
253
266
return Path(image).read_bytes()
254
267
elif isinstance(image, Path):
255
268
return image.read_bytes()
@@ -70,7 +70,8 @@ async def save_response_media(response, prompt: str, tags: list[str]) -> AsyncIt
70
70
raise ValueError(f"Unsupported media type: {content_type}")
71
71
72
72
filename = get_filename(tags, prompt, f".{extension}", prompt)
73
filename = update_filename(response, filename)
73
if hasattr(response, "headers"):
74
filename = update_filename(response, filename)
74
75
target_path = os.path.join(get_media_dir(), filename)
75
76
ensure_media_dir()
76
77
with open(target_path, 'wb') as f: