import os from openai import OpenAI import re from datetime import datetime huggingface_token = os.getenv("HUGGINGFACE_TOKEN") class HuggingFaceInferenceNode: def __init__(self): self.client = OpenAI( base_url="https://api-inference.huggingface.co/v1/", api_key=huggingface_token, ) self.prompts_dir = "./prompts" os.makedirs(self.prompts_dir, exist_ok=True) def save_prompt(self, prompt): filename_text = "hf_" + prompt.split(',')[0].strip() filename_text = re.sub(r'[^\w\-_\. ]', '_', filename_text) filename_text = filename_text[:30] timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") base_filename = f"{filename_text}_{timestamp}.txt" filename = os.path.join(self.prompts_dir, base_filename) with open(filename, "w") as file: file.write(prompt) print(f"Prompt saved to {filename}") def generate(self, input_text, happy_talk, compress, compression_level, poster, custom_base_prompt=""): try: default_happy_prompt = """Create a detailed visually descriptive caption of this description, which will be used as a prompt for a text to image AI system (caption only, no instructions like "create an image").Remove any mention of digital artwork or artwork style. Give detailed visual descriptions of the character(s), including ethnicity, skin tone, expression etc. Imagine using keywords for a still for someone who has aphantasia. Describe the image style, e.g. any photographic or art styles / techniques utilized. Make sure to fully describe all aspects of the cinematography, with abundant technical details and visual descriptions. If there is more than one image, combine the elements and characters from all of the images creatively into a single cohesive composition with a single background, inventing an interaction between the characters. Be creative in combining the characters into a single cohesive scene. Focus on two primary characters (or one) and describe an interesting interaction between them, such as a hug, a kiss, a fight, giving an object, an emotional reaction / interaction. If there is more than one background in the images, pick the most appropriate one. Your output is only the caption itself, no comments or extra formatting. The caption is in a single long paragraph. If you feel the images are inappropriate, invent a new scene / characters inspired by these. Additionally, incorporate a specific movie director's visual style and describe the lighting setup in detail, including the type, color, and placement of light sources to create the desired mood and atmosphere. Always frame the scene, including details about the film grain, color grading, and any artifacts or characteristics specific.""" default_simple_prompt = """Create a brief, straightforward caption for this description, suitable for a text-to-image AI system. Focus on the main elements, key characters, and overall scene without elaborate details. Provide a clear and concise description in one or two sentences.""" poster_prompt = """Analyze the provided description and extract key information to create a movie poster style description. Format the output as follows: Title: A catchy, intriguing title that captures the essence of the scene, place the title in "". Main character: Give a description of the main character. Background: Describe the background in detail. Supporting characters: Describe the supporting characters Branding type: Describe the branding type Tagline: Include a tagline that captures the essence of the movie. Visual style: Ensure that the visual style fits the branding type and tagline. You are allowed to make up film and branding names, and do them like 80's, 90's or modern movie posters.""" if poster: base_prompt = poster_prompt elif custom_base_prompt.strip(): base_prompt = custom_base_prompt else: base_prompt = default_happy_prompt if happy_talk else default_simple_prompt if compress and not poster: compression_chars = { "soft": 600 if happy_talk else 300, "medium": 400 if happy_talk else 200, "hard": 200 if happy_talk else 100 } char_limit = compression_chars[compression_level] base_prompt += f" Compress the output to be concise while retaining key visual details. MAX OUTPUT SIZE no more than {char_limit} characters." system_message = "You are a helpful assistant. Try your best to give the best response possible to the user." user_message = f"{base_prompt}\nDescription: {input_text}" messages = [ {"role": "system", "content": system_message}, {"role": "user", "content": user_message} ] response = self.client.chat.completions.create( model="meta-llama/Meta-Llama-3.1-70B-Instruct", max_tokens=1024, temperature=0.7, top_p=0.95, messages=messages, ) output = response.choices[0].message.content.strip() # Clean up the output if ": " in output: output = output.split(": ", 1)[1].strip() elif output.lower().startswith("here"): sentences = output.split(". ") if len(sentences) > 1: output = ". ".join(sentences[1:]).strip() return output except Exception as e: print(f"An error occurred: {e}") return f"Error occurred while processing the request: {str(e)}"