返回提交历史
Modified
g4f/Provider/DDG.py
+1
-3
Modified
g4f/Provider/hf/HuggingFaceAPI.py
+24
-19
XFEstudio/gpt4free
Cache pipline tag in HuggingFaceAPI
19c318e3
代码差异
2 个文件
+25
-22
@@ -173,12 +173,10 @@ class DDG(AsyncGeneratorProvider, ProviderModelMixin):
173
173
n: c.value
174
174
for n, c in session.cookie_jar.filter_cookies(cls.url).items()
175
175
}
176
yield conversation
176
177
177
178
if reason is not None:
178
179
yield FinishReason(reason)
179
180
if return_conversation:
181
yield conversation
182
180
183
181
except asyncio.TimeoutError as e:
184
182
raise TimeoutError(f"Request timed out: {str(e)}")
@@ -22,6 +22,8 @@ class HuggingFaceAPI(OpenaiTemplate):
22
22
vision_models = vision_models
23
23
model_aliases = model_aliases
24
24
25
pipeline_tag: dict[str, str] = {}
26
25
27
@classmethod
26
28
def get_models(cls, **kwargs):
27
29
if not cls.models:
@@ -32,6 +34,20 @@ class HuggingFaceAPI(OpenaiTemplate):
32
34
cls.models.append(model)
33
35
return cls.models
34
36
37
@classmethod
38
async def get_pipline_tag(cls, model: str, api_key: str = None):
39
if model in cls.pipeline_tag:
40
return cls.pipeline_tag[model]
41
async with StreamSession(
42
timeout=30,
43
headers=cls.get_headers(False, api_key),
44
) as session:
45
async with session.get(f"https://huggingface.co/api/models/{model}") as response:
46
await raise_for_status(response)
47
model_data = await response.json()
48
cls.pipeline_tag[model] = model_data.get("pipeline_tag")
49
return cls.pipeline_tag[model]
50
35
51
@classmethod
36
52
async def create_async_generator(
37
53
cls,
@@ -44,25 +60,14 @@ class HuggingFaceAPI(OpenaiTemplate):
44
60
images: ImagesType = None,
45
61
**kwargs
46
62
):
47
if api_base is None:
48
model_name = model
49
if model in cls.model_aliases:
50
model_name = cls.model_aliases[model]
51
api_base = f"https://api-inference.huggingface.co/models/{model_name}/v1"
52
async with StreamSession(
53
timeout=30,
54
headers=cls.get_headers(False, api_key),
55
) as session:
56
async with session.get(f"https://huggingface.co/api/models/{model}") as response:
57
if response.status == 404:
58
raise ModelNotSupportedError(f"Model is not supported: {model} in: {cls.__name__}")
59
await raise_for_status(response)
60
model_data = await response.json()
61
pipeline_tag = model_data.get("pipeline_tag")
62
if images is None and pipeline_tag not in ("text-generation", "image-text-to-text"):
63
raise ModelNotSupportedError(f"Model is not supported: {model} in: {cls.__name__} pipeline_tag: {pipeline_tag}")
64
elif pipeline_tag != "image-text-to-text":
65
raise ModelNotSupportedError(f"Model does not support images: {model} in: {cls.__name__} pipeline_tag: {pipeline_tag}")
63
if model in cls.model_aliases:
64
model = cls.model_aliases[model]
65
api_base = f"https://api-inference.huggingface.co/models/{model}/v1"
66
pipeline_tag = await cls.get_pipline_tag(model, api_key)
67
if images is None and pipeline_tag not in ("text-generation", "image-text-to-text"):
68
raise ModelNotSupportedError(f"Model is not supported: {model} in: {cls.__name__} pipeline_tag: {pipeline_tag}")
69
elif pipeline_tag != "image-text-to-text":
70
raise ModelNotSupportedError(f"Model does not support images: {model} in: {cls.__name__} pipeline_tag: {pipeline_tag}")
66
71
start = calculate_lenght(messages)
67
72
if start > max_inputs_lenght:
68
73
if len(messages) > 6: