返回提交历史
Modified
g4f/Provider/hf/HuggingFaceMedia.py
+26
-18
XFEstudio/gpt4free
``` refactor: update headers, data handling, and response parsing in HuggingFaceMedia
- Add 'Prefer': 'wait' to the request headers. - Compute extra_data_image using use_aspect_ratio with height and width. - Introduce extra_data_video for text-to-video tasks (non-novita) with inference steps, resolution, and aspect ratio. - Construct the main request payload by merging prompt, dimensions, and the appropriate extra data (video or image). - Update the 'fal-ai' text-to-image branch to wrap image_size using use_aspect_ratio. - Modify JSON response handling to first check if the Content-Type header starts with "application/json". - Parse and return VideoResponse or ImageResponse based on the JSON result, raising an error for unexpected responses. ```
eda3f69d
代码差异
1 个文件
+26
-18
@@ -121,6 +121,7 @@ class HuggingFaceMedia(AsyncGeneratorProvider, ProviderModelMixin):
121
121
headers = {
122
122
'Accept-Encoding': 'gzip, deflate',
123
123
'Content-Type': 'application/json',
124
'Prefer': 'wait',
124
125
}
125
126
new_mapping = {
126
127
"hf-free" if key == "hf-inference" else key: value for key, value in provider_mapping.items()
@@ -142,28 +143,32 @@ class HuggingFaceMedia(AsyncGeneratorProvider, ProviderModelMixin):
142
143
143
144
if aspect_ratio is None:
144
145
aspect_ratio = "1:1" if task == "text-to-image" else "16:9"
146
extra_data_image = use_aspect_ratio({
147
**extra_data,
148
"height": height,
149
"width": width,
150
}, aspect_ratio)
151
extra_data_video = {}
145
152
if task == "text-to-video" and provider_key != "novita":
146
extra_data = {
153
extra_data_video = {
147
154
"num_inference_steps": 20,
148
155
"resolution": resolution,
149
156
"aspect_ratio": aspect_ratio,
150
157
**extra_data
151
158
}
152
else:
153
extra_data = use_aspect_ratio({
154
**extra_data,
155
"height": height,
156
"width": width,
157
}, aspect_ratio)
158
159
url = f"{api_base}/{provider_id}"
159
160
data = {
160
161
"prompt": prompt,
161
**extra_data
162
**{"width": width, "height": height},
163
**(extra_data_video if task == "text-to-video" else extra_data_image),
162
164
}
163
165
if provider_key == "fal-ai" and task == "text-to-image":
164
166
data = {
165
"image_size": extra_data,
166
**data
167
"image_size": use_aspect_ratio({
168
"height": height,
169
"width": width,
170
}, aspect_ratio),
171
**extra_data
167
172
}
168
173
elif provider_key == "novita":
169
174
url = f"{api_base}/v3/hf/{provider_id}"
@@ -179,7 +184,7 @@ class HuggingFaceMedia(AsyncGeneratorProvider, ProviderModelMixin):
179
184
"inputs": prompt,
180
185
"parameters": {
181
186
"seed": random.randint(0, 2**32),
182
**extra_data
187
**data
183
188
}
184
189
}
185
190
elif task == "text-to-image":
@@ -203,15 +208,18 @@ class HuggingFaceMedia(AsyncGeneratorProvider, ProviderModelMixin):
203
208
if response.status == 404:
204
209
raise ModelNotSupportedError(f"Model is not supported: {model}")
205
210
await raise_for_status(response)
211
if response.headers.get("Content-Type", "").startswith("application/json"):
212
result = await response.json()
213
if "video" in result:
214
return provider_info, VideoResponse(result.get("video").get("url", result.get("video").get("video_url")), prompt)
215
elif task == "text-to-image":
216
return provider_info, ImageResponse([item["url"] for item in result.get("images", result.get("data"))], prompt)
217
elif task == "text-to-video" and result.get("output") is not None:
218
return provider_info, VideoResponse(result["output"], prompt)
219
raise ValueError(f"Unexpected response: {result}")
206
220
async for chunk in save_response_media(response, prompt, [aspect_ratio, model]):
207
221
return provider_info, chunk
208
result = await response.json()
209
if "video" in result:
210
return provider_info, VideoResponse(result.get("video").get("url", result.get("video").get("video_url")), prompt)
211
elif task == "text-to-image":
212
return provider_info, ImageResponse([item["url"] for item in result.get("images", result.get("data"))], prompt)
213
elif task == "text-to-video":
214
return provider_info, VideoResponse(result["output"], prompt)
222
215
223
await raise_for_status(last_response)
216
224
217
225
background_tasks = set()