gokaygokay commited on
Commit
4798f20
·
1 Parent(s): 6c1824a
Files changed (2) hide show
  1. app.py +26 -25
  2. requirements.txt +2 -1
app.py CHANGED
@@ -317,14 +317,16 @@ class PromptGenerator:
317
  return f"{prompt}, {caption}"
318
  return prompt
319
 
 
 
 
320
  class HuggingFaceInferenceNode:
321
  def __init__(self):
322
- self.clients = {
323
- "Mixtral": InferenceClient("NousResearch/Nous-Hermes-2-Mixtral-8x7B-DPO"),
324
- "Mistral": InferenceClient("mistralai/Mistral-7B-Instruct-v0.3"),
325
- "Llama 3": InferenceClient("meta-llama/Meta-Llama-3-8B-Instruct"),
326
- "Mistral-Nemo": InferenceClient("mistralai/Mistral-Nemo-Instruct-2407")
327
- }
328
  self.prompts_dir = "./prompts"
329
  os.makedirs(self.prompts_dir, exist_ok=True)
330
 
@@ -343,8 +345,6 @@ class HuggingFaceInferenceNode:
343
 
344
  def generate(self, model, input_text, happy_talk, compress, compression_level, poster, custom_base_prompt=""):
345
  try:
346
- client = self.clients[model]
347
-
348
  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."""
349
 
350
  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."""
@@ -375,23 +375,24 @@ You are allowed to make up film and branding names, and do them like 80's, 90's
375
  char_limit = compression_chars[compression_level]
376
  base_prompt += f" Compress the output to be concise while retaining key visual details. MAX OUTPUT SIZE no more than {char_limit} characters."
377
 
378
- messages = f"<|im_start|>system\nYou are a helpful assistant. Try your best to give best response possible to user.<|im_end|>"
379
- messages += f"\n<|im_start|>user\n{base_prompt}\nDescription: {input_text}<|im_end|>\n<|im_start|>assistant\n"
380
-
381
- stream = client.text_generation(messages, max_new_tokens=4000, do_sample=True, stream=True, details=True, return_full_text=False)
382
- output = ""
383
- for response in stream:
384
- if not response.token.text == "<|im_end|>":
385
- output += response.token.text
386
-
387
- # Remove specific tokens based on the model
388
- if model == "Llama 3":
389
- output = output.rstrip("<|eot_id|>")
390
- elif model == "Mistral":
391
- output = output.rstrip("</s>")
392
- elif model == "Mistral-Nemo":
393
- output = output.rstrip("<|im_end|></s>")
394
-
 
395
  # Clean up the output
396
  if ": " in output:
397
  output = output.split(": ", 1)[1].strip()
 
317
  return f"{prompt}, {caption}"
318
  return prompt
319
 
320
+ import os
321
+ from openai import OpenAI
322
+
323
  class HuggingFaceInferenceNode:
324
  def __init__(self):
325
+ self.ACCESS_TOKEN = os.getenv("HF_TOKEN")
326
+ self.client = OpenAI(
327
+ base_url="https://api-inference.huggingface.co/v1/",
328
+ api_key=self.ACCESS_TOKEN,
329
+ )
 
330
  self.prompts_dir = "./prompts"
331
  os.makedirs(self.prompts_dir, exist_ok=True)
332
 
 
345
 
346
  def generate(self, model, input_text, happy_talk, compress, compression_level, poster, custom_base_prompt=""):
347
  try:
 
 
348
  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."""
349
 
350
  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."""
 
375
  char_limit = compression_chars[compression_level]
376
  base_prompt += f" Compress the output to be concise while retaining key visual details. MAX OUTPUT SIZE no more than {char_limit} characters."
377
 
378
+ system_message = "You are a helpful assistant. Try your best to give the best response possible to the user."
379
+ user_message = f"{base_prompt}\nDescription: {input_text}"
380
+
381
+ messages = [
382
+ {"role": "system", "content": system_message},
383
+ {"role": "user", "content": user_message}
384
+ ]
385
+
386
+ response = self.client.chat.completions.create(
387
+ model="meta-llama/Meta-Llama-3.1-70B-Instruct",
388
+ max_tokens=1024,
389
+ temperature=0.7,
390
+ top_p=0.95,
391
+ messages=messages,
392
+ )
393
+
394
+ output = response.choices[0].message.content.strip()
395
+
396
  # Clean up the output
397
  if ": " in output:
398
  output = output.split(": ", 1)[1].strip()
requirements.txt CHANGED
@@ -1,3 +1,4 @@
1
  spaces
2
  transformers
3
- timm
 
 
1
  spaces
2
  transformers
3
+ timm
4
+ openai==1.37.0