-
-
Notifications
You must be signed in to change notification settings - Fork 13.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2555 from hlohaus/10Jan
Add login url to HuggingFace in Web UI
- Loading branch information
Showing
14 changed files
with
207 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
from __future__ import annotations | ||
|
||
import json | ||
from aiohttp import ClientSession, FormData | ||
|
||
from ...typing import AsyncResult, Messages, ImagesType | ||
from ...requests import raise_for_status | ||
from ...errors import ResponseError | ||
from ..base_provider import AsyncGeneratorProvider, ProviderModelMixin | ||
from ..helper import format_prompt, get_random_string | ||
from ...image import to_bytes, is_accepted_format | ||
|
||
class Qwen_QVQ_72B(AsyncGeneratorProvider, ProviderModelMixin): | ||
url = "https://qwen-qvq-72b-preview.hf.space" | ||
api_endpoint = "/gradio_api/call/generate" | ||
|
||
working = True | ||
|
||
default_model = "Qwen/QwQ-32B-Preview" | ||
models = [default_model] | ||
|
||
@classmethod | ||
async def create_async_generator( | ||
cls, model: str, messages: Messages, | ||
images: ImagesType = None, | ||
api_key: str = None, | ||
proxy: str = None, | ||
**kwargs | ||
) -> AsyncResult: | ||
headers = { | ||
"Accept": "application/json", | ||
} | ||
if api_key is not None: | ||
headers["Authorization"] = f"Bearer {api_key}" | ||
async with ClientSession(headers=headers) as session: | ||
if images: | ||
data = FormData() | ||
data_bytes = to_bytes(images[0][0]) | ||
data.add_field("files", data_bytes, content_type=is_accepted_format(data_bytes), filename=images[0][1]) | ||
url = f"https://qwen-qvq-72b-preview.hf.space/gradio_api/upload?upload_id={get_random_string()}" | ||
async with session.post(url, data=data, proxy=proxy) as response: | ||
await raise_for_status(response) | ||
image = await response.json() | ||
data = {"data": [{"path": image[0]}, format_prompt(messages)]} | ||
else: | ||
data = {"data": [None, format_prompt(messages)]} | ||
async with session.post(f"{cls.url}{cls.api_endpoint}", json=data, proxy=proxy) as response: | ||
await raise_for_status(response) | ||
event_id = (await response.json()).get("event_id") | ||
async with session.get(f"{cls.url}{cls.api_endpoint}/{event_id}") as event_response: | ||
await raise_for_status(event_response) | ||
event = None | ||
text_position = 0 | ||
async for chunk in event_response.content: | ||
if chunk.startswith(b"event: "): | ||
event = chunk[7:].decode(errors="replace").strip() | ||
if chunk.startswith(b"data: "): | ||
if event == "error": | ||
raise ResponseError(f"GPU token limit exceeded: {chunk.decode(errors='replace')}") | ||
if event in ("complete", "generating"): | ||
try: | ||
data = json.loads(chunk[6:]) | ||
except (json.JSONDecodeError, KeyError, TypeError) as e: | ||
raise RuntimeError(f"Failed to read response: {chunk.decode(errors='replace')}", e) | ||
if event == "generating": | ||
if isinstance(data[0], str): | ||
yield data[0][text_position:] | ||
text_position = len(data[0]) | ||
else: | ||
break |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
from __future__ import annotations | ||
|
||
import json | ||
from aiohttp import ClientSession | ||
|
||
from ...typing import AsyncResult, Messages | ||
from ...image import ImageResponse, ImagePreview | ||
from ...errors import ResponseError | ||
from ..base_provider import AsyncGeneratorProvider, ProviderModelMixin | ||
|
||
class StableDiffusion35Large(AsyncGeneratorProvider, ProviderModelMixin): | ||
url = "https://stabilityai-stable-diffusion-3-5-large.hf.space" | ||
api_endpoint = "/gradio_api/call/infer" | ||
|
||
working = True | ||
|
||
default_model = 'stable-diffusion-3.5-large' | ||
models = [default_model] | ||
image_models = [default_model] | ||
|
||
@classmethod | ||
async def create_async_generator( | ||
cls, model: str, messages: Messages, | ||
prompt: str = None, | ||
negative_prompt: str = None, | ||
api_key: str = None, | ||
proxy: str = None, | ||
width: int = 1024, | ||
height: int = 1024, | ||
guidance_scale: float = 4.5, | ||
num_inference_steps: int = 50, | ||
seed: int = 0, | ||
randomize_seed: bool = True, | ||
**kwargs | ||
) -> AsyncResult: | ||
headers = { | ||
"Content-Type": "application/json", | ||
"Accept": "application/json", | ||
} | ||
if api_key is not None: | ||
headers["Authorization"] = f"Bearer {api_key}" | ||
async with ClientSession(headers=headers) as session: | ||
prompt = messages[-1]["content"] if prompt is None else prompt | ||
data = { | ||
"data": [prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps] | ||
} | ||
async with session.post(f"{cls.url}{cls.api_endpoint}", json=data, proxy=proxy) as response: | ||
response.raise_for_status() | ||
event_id = (await response.json()).get("event_id") | ||
async with session.get(f"{cls.url}{cls.api_endpoint}/{event_id}") as event_response: | ||
event_response.raise_for_status() | ||
event = None | ||
async for chunk in event_response.content: | ||
if chunk.startswith(b"event: "): | ||
event = chunk[7:].decode(errors="replace").strip() | ||
if chunk.startswith(b"data: "): | ||
if event == "error": | ||
raise ResponseError(f"GPU token limit exceeded: {chunk.decode(errors='replace')}") | ||
if event in ("complete", "generating"): | ||
try: | ||
data = json.loads(chunk[6:]) | ||
if data is None: | ||
continue | ||
url = data[0]["url"] | ||
except (json.JSONDecodeError, KeyError, TypeError) as e: | ||
raise RuntimeError(f"Failed to parse image URL: {chunk.decode(errors='replace')}", e) | ||
if event == "generating": | ||
yield ImagePreview(url, prompt) | ||
else: | ||
yield ImageResponse(url, prompt) | ||
break |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.