XFEstudio/gpt4free
fix: replace format_image_prompt with format_media_prompt across multiple files
- Updated imports to use format_media_prompt in g4f/Provider/ARTA.py, PollinationsAI.py, PollinationsImage.py, Websim.py, audio/OpenAIFM.py, hf_space/BlackForestLabs_Flux1Dev.py, hf_space/DeepseekAI_JanusPro7b.py, hf_space/G4F.py, hf_space/Microsoft_Phi_4_Multimodal.py, hf_space/StabilityAI_SD35Large.py, needs_auth/BingCreateImages.py, needs_auth/BlackboxPro.py, needs_auth/DeepInfra.py, needs_auth/Gemini.py, needs_auth/MicrosoftDesigner.py, needs_auth/OpenaiChat.py, needs_auth/hf/HuggingChat.py, needs_auth/hf/HuggingFaceInference.py, needs_auth/hf/HuggingFaceMedia.py, not_working/AllenAI.py, template/OpenaiTemplate.py, api.py, and gui/server/api.py - Replaced calls to format_image_prompt with format_media_prompt in relevant locations - Changed media prompt handling in various providers to ensure consistent usage of format_media_prompt - Modified the __aenter__ and __aexit__ methods of requests/aiohttp.py to properly manage ClientSession lifecycle
f96ea67f
代码差异
@@ -13,7 +13,7 @@ from ..providers.response import ImageResponse, Reasoning
from ..errors import ResponseError, ModelNotFoundError
from ..cookies import get_cookies_dir
from .base_provider import AsyncGeneratorProvider, ProviderModelMixin
from .helper import format_image_prompt
from .helper import format_media_prompt
from .. import debug
class ARTA(AsyncGeneratorProvider, ProviderModelMixin):
@@ -177,7 +177,7 @@ class ARTA(AsyncGeneratorProvider, ProviderModelMixin):
**kwargs
) -> AsyncResult:
model = cls.get_model(model)
prompt = format_image_prompt(messages, prompt)
prompt = format_media_prompt(messages, prompt)
# Generate a random seed if not provided
if seed is None:
@@ -9,7 +9,7 @@ from urllib.parse import quote_plus
from typing import Optional
from aiohttp import ClientSession
from .helper import filter_none, format_image_prompt
from .helper import filter_none, format_media_prompt
from .base_provider import AsyncGeneratorProvider, ProviderModelMixin
from ..typing import AsyncResult, Messages, MediaListType
from ..image import is_data_an_audio
@@ -132,6 +132,7 @@ class PollinationsAI(AsyncGeneratorProvider, ProviderModelMixin):
### Image Models ###
"sdxl-turbo": "turbo",
"gpt-image": "gptimage",
"dall-e-3": "gptimage",
"flux-pro": "flux",
"flux-dev": "flux",
"flux-schnell": "flux"
@@ -292,7 +293,7 @@ class PollinationsAI(AsyncGeneratorProvider, ProviderModelMixin):
if model in cls.image_models:
async for chunk in cls._generate_image(
model=model,
prompt=format_image_prompt(messages, prompt),
prompt=format_media_prompt(messages, prompt),
proxy=proxy,
aspect_ratio=aspect_ratio,
width=width,
@@ -524,7 +525,7 @@ class PollinationsAI(AsyncGeneratorProvider, ProviderModelMixin):
debug.error("Error generating title and followups")
debug.error(e)
elif response.headers["content-type"].startswith("application/json"):
prompt = format_image_prompt(messages)
prompt = format_media_prompt(messages)
result = await response.json()
if result.get("model"):
yield ProviderInfo(**cls.get_dict(), model=result.get("model"))
@@ -2,7 +2,7 @@ from __future__ import annotations
from typing import Optional
from .helper import format_image_prompt
from .helper import format_media_prompt
from ..typing import AsyncResult, Messages
from ..constants import STATIC_URL
from .PollinationsAI import PollinationsAI
@@ -54,7 +54,7 @@ class PollinationsImage(PollinationsAI):
cls.get_models()
async for chunk in cls._generate_image(
model=model,
prompt=format_image_prompt(messages, prompt),
prompt=format_media_prompt(messages, prompt),
proxy=proxy,
aspect_ratio=aspect_ratio,
width=width,
@@ -11,7 +11,7 @@ from .base_provider import AsyncGeneratorProvider, ProviderModelMixin
from ..requests.raise_for_status import raise_for_status
from ..errors import ResponseStatusError
from ..providers.response import ImageResponse
from .helper import format_prompt, format_image_prompt
from .helper import format_prompt, format_media_prompt
class Websim(AsyncGeneratorProvider, ProviderModelMixin):
@@ -110,7 +110,7 @@ class Websim(AsyncGeneratorProvider, ProviderModelMixin):
proxy: str = None,
**kwargs
) -> AsyncResult:
used_prompt = format_image_prompt(messages, prompt)
used_prompt = format_media_prompt(messages, prompt)
async with ClientSession(headers=headers) as session:
data = {
@@ -4,7 +4,7 @@ from aiohttp import ClientSession
from ...typing import AsyncResult, Messages
from ..base_provider import AsyncGeneratorProvider, ProviderModelMixin
from ..helper import get_last_user_message, get_system_prompt
from ..helper import format_media_prompt, get_system_prompt
from ...image.copy_images import save_response_media
from ...requests.raise_for_status import raise_for_status
from ...requests.aiohttp import get_connector
@@ -15,14 +15,13 @@ class OpenAIFM(AsyncGeneratorProvider, ProviderModelMixin):
url = "https://www.openai.fm"
api_endpoint = "https://www.openai.fm/api/generate"
working = True
default_model = 'gpt-4o-mini-tts'
default_audio_model = default_model
default_voice = 'coral'
voices = ['alloy', 'ash', 'ballad', default_voice, 'echo', 'fable', 'onyx', 'nova', 'sage', 'shimmer', 'verse']
audio_models = {default_audio_model: voices}
default_model = 'coral'
voices = ['alloy', 'ash', 'ballad', default_model, 'echo', 'fable', 'onyx', 'nova', 'sage', 'shimmer', 'verse']
audio_models = {"gpt-4o-mini-tts": voices}
model_aliases = {"gpt-4o-mini-tts": default_model}
models = voices
friendly = """Affect/personality: A cheerful guide
Tone: Friendly, clear, and reassuring, creating a calm atmosphere and making the listener feel confident and comfortable.
@@ -99,16 +98,17 @@ Emotion: Restrained enthusiasm for discoveries and findings, conveying intellect
audio: dict = {},
**kwargs
) -> AsyncResult:
# Retrieve parameters from the audio dictionary
voice = audio.get("voice", kwargs.get("voice", cls.default_voice))
instructions = audio.get("instructions", kwargs.get("instructions", get_system_prompt(messages) or cls.friendly))
model = cls.get_model(model)
voice = audio.get("voice", kwargs.get("voice", model))
default_instructions = get_system_prompt(messages) or cls.friendly
instructions = audio.get("instructions", kwargs.get("instructions", default_instructions))
headers = {
**DEFAULT_HEADERS,
"referer": f"{cls.url}/"
}
text = get_last_user_message(messages, prompt)
prompt = format_media_prompt(messages, prompt)
params = {
"input": text,
"input": prompt,
"prompt": instructions,
"voice": voice
}
@@ -118,5 +118,5 @@ Emotion: Restrained enthusiasm for discoveries and findings, conveying intellect
params=params
) as response:
await raise_for_status(response)
async for chunk in save_response_media(response, text, [model, voice]):
async for chunk in save_response_media(response, prompt, [model, voice]):
yield chunk
@@ -9,7 +9,7 @@ from ...requests import StreamSession
from ...image import use_aspect_ratio
from ...errors import ResponseError
from ..base_provider import AsyncGeneratorProvider, ProviderModelMixin
from ..helper import format_image_prompt
from ..helper import format_media_prompt
from .DeepseekAI_JanusPro7b import get_zerogpu_token
from .raise_for_status import raise_for_status
@@ -70,7 +70,7 @@ class BlackForestLabs_Flux1Dev(AsyncGeneratorProvider, ProviderModelMixin):
**kwargs
) -> AsyncResult:
async with StreamSession(impersonate="chrome", proxy=proxy) as session:
prompt = format_image_prompt(messages, prompt)
prompt = format_media_prompt(messages, prompt)
data = use_aspect_ratio({"width": width, "height": height}, aspect_ratio)
data = [prompt, seed, randomize_seed, data.get("width"), data.get("height"), guidance_scale, num_inference_steps]
conversation = JsonConversation(zerogpu_token=api_key, zerogpu_uuid=zerogpu_uuid, session_hash=uuid.uuid4().hex)
@@ -9,7 +9,7 @@ import urllib.parse
from ...typing import AsyncResult, Messages, Cookies, MediaListType
from ..base_provider import AsyncGeneratorProvider, ProviderModelMixin
from ..helper import format_prompt, format_image_prompt
from ..helper import format_prompt, format_media_prompt
from ...providers.response import JsonConversation, ImageResponse, Reasoning
from ...requests.aiohttp import StreamSession, StreamResponse, FormData
from ...requests.raise_for_status import raise_for_status
@@ -85,7 +85,7 @@ class DeepseekAI_JanusPro7b(AsyncGeneratorProvider, ProviderModelMixin):
if model == cls.default_image_model or prompt is not None:
method = "image"
prompt = format_prompt(messages) if prompt is None and conversation is None else prompt
prompt = format_image_prompt(messages, prompt)
prompt = format_media_prompt(messages, prompt)
if seed is None:
seed = random.randint(1000, 999999)
@@ -7,7 +7,7 @@ import asyncio
from ...typing import AsyncResult, Messages
from ...providers.response import ImageResponse, Reasoning, JsonConversation
from ..helper import format_image_prompt, get_random_string
from ..helper import format_media_prompt, get_random_string
from .DeepseekAI_JanusPro7b import DeepseekAI_JanusPro7b, get_zerogpu_token
from .BlackForestLabs_Flux1Dev import BlackForestLabs_Flux1Dev
from .raise_for_status import raise_for_status
@@ -80,7 +80,7 @@ class G4F(DeepseekAI_JanusPro7b):
width = max(32, width - (width % 8))
height = max(32, height - (height % 8))
if prompt is None:
prompt = format_image_prompt(messages)
prompt = format_media_prompt(messages)
if seed is None:
seed = random.randint(9999, 2**32 - 1)
@@ -5,7 +5,7 @@ import uuid
from ...typing import AsyncResult, Messages, Cookies, MediaListType
from ..base_provider import AsyncGeneratorProvider, ProviderModelMixin
from ..helper import format_prompt, format_image_prompt
from ..helper import format_prompt, format_media_prompt
from ...providers.response import JsonConversation
from ...requests.aiohttp import StreamSession, StreamResponse, FormData
from ...requests.raise_for_status import raise_for_status
@@ -104,7 +104,7 @@ class Microsoft_Phi_4_Multimodal(AsyncGeneratorProvider, ProviderModelMixin):
**kwargs
) -> AsyncResult:
prompt = format_prompt(messages) if prompt is None and conversation is None else prompt
prompt = format_image_prompt(messages, prompt)
prompt = format_media_prompt(messages, prompt)
session_hash = uuid.uuid4().hex if conversation is None else getattr(conversation, "session_hash", uuid.uuid4().hex)
async with StreamSession(proxy=proxy, impersonate="chrome") as session:
@@ -8,7 +8,7 @@ from ...providers.response import ImageResponse, ImagePreview
from ...image import use_aspect_ratio
from ...errors import ResponseError
from ..base_provider import AsyncGeneratorProvider, ProviderModelMixin
from ..helper import format_image_prompt
from ..helper import format_media_prompt
class StabilityAI_SD35Large(AsyncGeneratorProvider, ProviderModelMixin):
label = "StabilityAI SD-3.5-Large"
@@ -46,7 +46,7 @@ class StabilityAI_SD35Large(AsyncGeneratorProvider, ProviderModelMixin):
if api_key is not None:
headers["Authorization"] = f"Bearer {api_key}"
async with ClientSession(headers=headers) as session:
prompt = format_image_prompt(messages, prompt)
prompt = format_media_prompt(messages, prompt)
data = use_aspect_ratio({"width": width, "height": height}, aspect_ratio)
data = {
"data": [prompt, negative_prompt, seed, randomize_seed, data.get("width"), data.get("height"), guidance_scale, num_inference_steps]
@@ -6,7 +6,7 @@ from ...errors import MissingAuthError
from ...typing import AsyncResult, Messages, Cookies
from ..base_provider import AsyncGeneratorProvider, ProviderModelMixin
from .bing.create_images import create_images, create_session
from ..helper import format_image_prompt
from ..helper import format_media_prompt
class BingCreateImages(AsyncGeneratorProvider, ProviderModelMixin):
label = "Microsoft Designer in Bing"
@@ -36,7 +36,7 @@ class BingCreateImages(AsyncGeneratorProvider, ProviderModelMixin):
**kwargs
) -> AsyncResult:
session = BingCreateImages(cookies, proxy, api_key)
yield await session.generate(format_image_prompt(messages, prompt))
yield await session.generate(format_media_prompt(messages, prompt))
async def generate(self, prompt: str) -> ImageResponse:
"""
@@ -16,7 +16,7 @@ from ..base_provider import AsyncGeneratorProvider, ProviderModelMixin
from ..openai.har_file import get_har_files
from ...image import to_data_uri
from ...cookies import get_cookies_dir
from ..helper import format_image_prompt, render_messages
from ..helper import format_media_prompt, render_messages
from ...providers.response import JsonConversation, ImageResponse
from ...tools.media import merge_media
from ...errors import RateLimitError, NoValidHarFileError
@@ -1343,7 +1343,7 @@ class BlackboxPro(AsyncGeneratorProvider, ProviderModelMixin):
image_url_match = re.search(r'!\[.*?\]\((.*?)\)', full_response_text)
if image_url_match:
image_url = image_url_match.group(1)
yield ImageResponse(urls=[image_url], alt=format_image_prompt(messages, prompt))
yield ImageResponse(urls=[image_url], alt=format_media_prompt(messages, prompt))
return
# Handle conversation history once, in one place