Whitepaper
Docs
Sign In
Function
Function
pipe
v0.1.0
Grok_Image_Generator
Function ID
grok_image_generator
Creator
@jeeva420
Downloads
51+
A manifold function to integrate X_AI's GROK-2-IMAGE models into Open WebUI
Get
README
No README available
Function Code
Show
""" title: XAI GROK 2 IMAGE Manifold description: A manifold function to integrate X_AI's GROK-2-IMAGE models into Open WebUI. Get your API Key & Endpoint from https://console.x.ai. author: Jeeva version: 0.1.0 license: MIT requirements: pydantic environment_variables: X_AI_API_KEY, X_AI_ENDPOINT """ import os from typing import Iterator, List, Union from open_webui.utils.misc import get_last_user_message from pydantic import BaseModel, Field import json from openai import OpenAI # Thanks bro https://openwebui.com/f/bgeneto/dall_e/ class Pipe: """X_AI GROK-2-IMAGE pipeline""" class Valves(BaseModel): X_AI_ENDPOINT: str = Field( default="https://api.x.ai/v1", description="X_AI API Endpoint", ) X_AI_API_KEY: str = Field(default="", description="Your X_AI API key") NUM_IMAGES: int = Field(default=1, description="Number of images to generate") def __init__(self): self.type = "manifold" self.id = "GROK_3_IMAGE" self.name = "GROK-3-IMAGE" self.valves = self.Valves( X_AI_API_KEY=os.getenv("X_AI_API_KEY", ""), X_AI_ENDPOINT=os.getenv("X_AI_ENDPOINT", "https://api.x.ai/v1"), ) self.client = OpenAI( base_url=self.valves.X_AI_ENDPOINT, api_key=self.valves.X_AI_API_KEY, ) def get_openai_assistants(self) -> List[dict]: """Get the available ImageGen models from X_AI OpenAI""" if self.valves.X_AI_API_KEY: self.client = OpenAI( base_url=self.valves.X_AI_ENDPOINT, api_key=self.valves.X_AI_API_KEY, ) return [{"id": "grok-2-image", "name": ""}] return [] def pipes(self) -> List[dict]: return self.get_openai_assistants() def pipe(self, body: dict) -> Union[str, Iterator[str]]: if not self.valves.X_AI_API_KEY: return "Error: X_AI_API_KEY is not set" self.client = OpenAI( base_url=self.valves.X_AI_ENDPOINT, api_key=self.valves.X_AI_API_KEY, ) # Extract the user message (prompt) from the body user_message = get_last_user_message(body["messages"]) try: # Send the request to X_AI DALL-E 3 API response = self.client.images.generate( model="grok-2-image", # Grok Image Model ID prompt=user_message, n=self.valves.NUM_IMAGES, ) # Debugging: print raw response for inspection print("Raw API Response:", response) # Attempt to parse the response as JSON image_urls = json.loads(response.model_dump_json())["data"] message = "" for image in image_urls: if image["url"]: revised_prompt = image["revised_prompt"] message += f"Revised prompt by the Model: {revised_prompt}\n\n" message += "\n" yield message except json.JSONDecodeError as e: return f"Error parsing JSON response: {str(e)}. Raw response: {response}" except Exception as e: return f"An error occurred: {str(e)}. Raw response: {response}"