Whitepaper
Docs
Sign In
Tool
Tool
iGen
Tool ID
igen
Creator
@herawen
Downloads
143+
image generation w huggingface
Get
README
No README available
Tool Code
Show
[{"id":"da472c65-de60-4249-9c52-c98d3250b6df","userId":"aacbdcdc-f063-40ac-aced-e541919fe7e6","tool":{"id":"huggingface_image_generation","name":"Huggingface Image Generation","meta":{"description":"Generates images. Look at description below.","manifest":{"title":"Hugging Face Image Generation","author":"HugiSoft","author_url":"https://hugisoft.com","funding_url":"https://github.com/sponsors/hugisoft","version":"1.0","required_open_webui_version":"0.3.9","description":"|","License":"MIT"}},"content":"\"\"\"\ntitle: Hugging Face Image Generation\nauthor: HugiSoft\nauthor_url: https://hugisoft.com\nfunding_url: https://github.com/sponsors/hugisoft\nversion: 1.0\nrequired_open_webui_version: 0.3.9\n\ndescription: |\n The Hugging Face Image Generation tool is a powerful integration that leverages the Hugging Face API to generate high-quality images from text prompts. \n It uses the Stable Diffusion 3.5 Large Turbo model to create visually stunning images based on user input. The tool is designed to enhance user prompts \n with detailed and creative descriptions, ensuring the generated images are both accurate and visually appealing.\n\n Key Features:\n - **Text-to-Image Generation**: Converts text prompts into high-resolution images using Hugging Face's Stable Diffusion model.\n - **Prompt Enhancement**: Automatically enhances user prompts with detailed and creative descriptions for better image results.\n - **Multiple Formats**: Supports various image formats, including default, square, landscape, and portrait.\n - **Secure Image Storage**: Saves generated images to a specified directory and provides a URL for easy access.\n - **Error Handling**: Includes robust error handling for API failures, timeouts, and other issues.\n\n License: MIT\n\"\"\"\n\nimport requests\nimport os\nfrom typing import Dict, Any\nfrom pydantic import BaseModel, Field\nfrom datetime import datetime\n\n\nclass HFException(Exception):\n \"\"\"Base exception for HuggingFace API related errors.\"\"\"\n pass\n\n\nclass Tools:\n class Valves(BaseModel):\n HF_API_KEY: str = Field(\n default=None,\n description=\"HuggingFace API key for accessing the serverless endpoints\",\n )\n HF_API_URL: str = Field(\n default=\"https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-3.5-large-turbo\",\n description=\"HuggingFace API URL for accessing the serverless endpoint of a Text-to-Image Model.\",\n )\n IMAGE_SAVE_DIR: str = Field(\n default=\"/var/www/html/generated_images\",\n description=\"Directory to save generated images.\",\n )\n IMAGE_BASE_URL: str = Field(\n default=\"https://hugisoft.com/generated_images\",\n description=\"Base URL for accessing generated images.\",\n )\n\n def __init__(self):\n self.valves = self.Valves()\n # Ensure the image save directory exists\n os.makedirs(self.valves.IMAGE_SAVE_DIR, exist_ok=True)\n\n async def create_image(\n self,\n prompt: str,\n image_format: str = \"default\",\n __user__: dict = {},\n __event_emitter__=None,\n ) -> str:\n \"\"\"\n Creates visually stunning images with text prompts using text-to-image models, based on a prompt.\n If the user prompt is too general or lacking, embellish it to generate a better illustration.\n :param prompt: The prompt to generate the image.\n :param image_format: Format of the image (default, landscape, portrait).\n \"\"\"\n if not self.valves.HF_API_KEY:\n return \"HuggingFace API key is not set in the Valves.\"\n\n try:\n formats = {\n \"default\": (1024, 1024),\n \"square\": (1024, 1024),\n \"landscape\": (1024, 768),\n \"landscape_large\": (1440, 1024),\n \"portrait\": (768, 1024),\n \"portrait_large\": (1024, 1440),\n }\n\n if image_format not in formats:\n raise ValueError(\n f\"Invalid format. Must be one of: {', '.join(formats.keys())}\"\n )\n\n width, height = formats[image_format]\n\n headers = {\"Authorization\": f\"Bearer {self.valves.HF_API_KEY}\"}\n payload = {\n \"inputs\": prompt,\n \"parameters\": {\"width\": width, \"height\": height},\n }\n\n response = requests.post(\n self.valves.HF_API_URL,\n headers=headers,\n json=payload,\n timeout=(10, 600),\n )\n\n if response.status_code != 200:\n raise HFException(\n f\"API request failed with status code {response.status_code}: {response.text}\"\n )\n\n # Save the image to a file\n timestamp = datetime.now().strftime(\"%Y%m%d_%H%M%S\")\n image_filename = f\"generated_image_{timestamp}.png\"\n image_path = os.path.join(self.valves.IMAGE_SAVE_DIR, image_filename)\n\n with open(image_path, \"wb\") as f:\n f.write(response.content)\n\n # Generate the full image URL\n image_url = f\"{self.valves.IMAGE_BASE_URL}/{image_filename}\"\n\n # Return the fully formatted image for displaying\n return f\"\"\n\n except Exception as e:\n return f\"An error occurred: {str(e)}\""},"downloads":52,"upvotes":0,"downvotes":0,"updatedAt":1735399917,"createdAt":1735394453,"user":{"id":"aacbdcdc-f063-40ac-aced-e541919fe7e6","username":"blygf","name":"","profileImageUrl":"https://www.gravatar.com/avatar/398b5c8c59286d308bb567fcb3bfcec99c9a330af4354fbaf4977dcec24108f5?d=mp","createdAt":1735385595}}]