返回提交历史
Modified
g4f/Provider/audio/EdgeTTS.py
+9
-12
Modified
g4f/api/stubs.py
+3
-6
XFEstudio/gpt4free
refactor: update audio parameter handling in EdgeTTS and stubs
- Remove the unused `language`, `locale`, and `extra_parameters` parameters from the `EdgeTTS` function signature in `g4f/Provider/audio/EdgeTTS.py`. - Update voice selection logic to check for `"locale"` and `"language"` keys in the `audio` dictionary, defaulting to `cls.default_locale` when neither is provided, and modify the error message accordingly. - Refactor extraction of extra parameters by building a dict from the `audio` dictionary for keys `"rate"`, `"volume"`, and `"pitch"`. - In `g4f/api/stubs.py`, remove the try/except block for importing `Annotated` and import `Messages` from `..typing` instead. - Add an optional `audio: Optional[dict] = None` field to the `ImageGenerationConfig` model.
1296b3f6
代码差异
2 个文件
+12
-18
@@ -38,10 +38,7 @@ class EdgeTTS(AsyncGeneratorProvider, ProviderModelMixin):
38
38
messages: Messages,
39
39
proxy: str = None,
40
40
prompt: str = None,
41
language: str = None,
42
locale: str = None,
43
41
audio: dict = {"voice": None, "format": "mp3"},
44
extra_parameters: list[str] = ["rate", "volume", "pitch"],
45
42
**kwargs
46
43
) -> AsyncResult:
47
44
prompt = format_image_prompt(messages, prompt)
@@ -50,17 +47,17 @@ class EdgeTTS(AsyncGeneratorProvider, ProviderModelMixin):
50
47
voice = audio.get("voice", model)
51
48
if not voice:
52
49
voices = await VoicesManager.create()
53
if locale is None:
54
if language is None:
55
voices = voices.find(Locale=cls.default_locale)
56
elif "-" in language:
57
voices = voices.find(Locale=language)
50
if "locale" in audio:
51
voices = voices.find(Locale=audio["locale"])
52
elif "language" in audio:
53
if "-" in audio["language"]:
54
voices = voices.find(Locale=audio["language"])
58
55
else:
59
voices = voices.find(Language=language)
56
voices = voices.find(Language=audio["language"])
60
57
else:
61
voices = voices.find(Locale=locale)
58
voices = voices.find(Locale=cls.default_locale)
62
59
if not voices:
63
raise ValueError(f"No voices found for language '{language}' and locale '{locale}'.")
60
raise ValueError(f"No voices found for language '{audio.get('language')}' and locale '{audio.get('locale')}'.")
64
61
voice = random.choice(voices)["Name"]
65
62
66
63
format = audio.get("format", "mp3")
@@ -68,7 +65,7 @@ class EdgeTTS(AsyncGeneratorProvider, ProviderModelMixin):
68
65
target_path = os.path.join(get_media_dir(), filename)
69
66
ensure_media_dir()
70
67
71
extra_parameters = {param: kwargs[param] for param in extra_parameters if param in kwargs}
68
extra_parameters = {param: audio[param] for param in ["rate", "volume", "pitch"] if param in audio}
72
69
communicate = edge_tts.Communicate(prompt, voice=voice, proxy=proxy, **extra_parameters)
73
70
74
71
await communicate.save(target_path)
@@ -2,12 +2,8 @@ from __future__ import annotations
2
2
3
3
from pydantic import BaseModel, Field, model_validator
4
4
from typing import Union, Optional
5
try:
6
from typing import Annotated
7
except ImportError:
8
class Annotated:
9
pass
10
from g4f.typing import Messages
5
6
from ..typing import Messages
11
7
12
8
class ChatCompletionsConfig(BaseModel):
13
9
messages: Messages = Field(examples=[[{"role": "system", "content": ""}, {"role": "user", "content": ""}]])
@@ -69,6 +65,7 @@ class ImageGenerationConfig(BaseModel):
69
65
n: Optional[int] = None
70
66
negative_prompt: Optional[str] = None
71
67
resolution: Optional[str] = None
68
audio: Optional[dict] = None
72
69
73
70
@model_validator(mode='before')
74
71
def parse_size(cls, values):