Spaces:
Running
on
Zero
Running
on
Zero
gokaygokay
commited on
Commit
•
fb29fff
1
Parent(s):
707a6ed
global prompt type
Browse files- huggingface_inference_node.py +30 -19
- ui_components.py +4 -3
huggingface_inference_node.py
CHANGED
@@ -1,13 +1,15 @@
|
|
1 |
import os
|
2 |
-
from openai import OpenAI
|
3 |
-
import anthropic
|
4 |
-
from groq import Groq
|
5 |
import re
|
6 |
from datetime import datetime
|
7 |
|
|
|
|
|
|
|
|
|
8 |
huggingface_token = os.getenv("HUGGINGFACE_TOKEN")
|
9 |
groq_api_key = os.getenv("GROQ_API_KEY")
|
10 |
|
|
|
11 |
class LLMInferenceNode:
|
12 |
def __init__(self):
|
13 |
self.huggingface_client = OpenAI(
|
@@ -16,7 +18,19 @@ class LLMInferenceNode:
|
|
16 |
)
|
17 |
self.groq_client = Groq(api_key=groq_api_key)
|
18 |
|
19 |
-
def generate(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
try:
|
21 |
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."""
|
22 |
|
@@ -47,7 +61,7 @@ You are allowed to make up film and branding names, and do them like 80's, 90's
|
|
47 |
"only_objects": only_objects_prompt,
|
48 |
"no_figure": no_figure_prompt,
|
49 |
"landscape": landscape_prompt,
|
50 |
-
"fantasy": fantasy_prompt
|
51 |
}
|
52 |
|
53 |
# Update this part to handle the prompt_type correctly
|
@@ -60,13 +74,15 @@ You are allowed to make up film and branding names, and do them like 80's, 90's
|
|
60 |
print("Using custom base prompt")
|
61 |
else:
|
62 |
base_prompt = default_happy_prompt
|
63 |
-
print(
|
|
|
|
|
64 |
|
65 |
if compress and not poster:
|
66 |
compression_chars = {
|
67 |
"soft": 600 if happy_talk else 300,
|
68 |
"medium": 400 if happy_talk else 200,
|
69 |
-
"hard": 200 if happy_talk else 100
|
70 |
}
|
71 |
char_limit = compression_chars[compression_level]
|
72 |
base_prompt += f" Compress the output to be concise while retaining key visual details. MAX OUTPUT SIZE no more than {char_limit} characters."
|
@@ -82,7 +98,7 @@ You are allowed to make up film and branding names, and do them like 80's, 90's
|
|
82 |
top_p=0.95,
|
83 |
messages=[
|
84 |
{"role": "system", "content": system_message},
|
85 |
-
{"role": "user", "content": user_message}
|
86 |
],
|
87 |
)
|
88 |
output = response.choices[0].message.content.strip()
|
@@ -95,7 +111,7 @@ You are allowed to make up film and branding names, and do them like 80's, 90's
|
|
95 |
temperature=0.7,
|
96 |
messages=[
|
97 |
{"role": "system", "content": system_message},
|
98 |
-
{"role": "user", "content": user_message}
|
99 |
],
|
100 |
)
|
101 |
output = response.choices[0].message.content.strip()
|
@@ -110,14 +126,9 @@ You are allowed to make up film and branding names, and do them like 80's, 90's
|
|
110 |
messages=[
|
111 |
{
|
112 |
"role": "user",
|
113 |
-
"content": [
|
114 |
-
{
|
115 |
-
"type": "text",
|
116 |
-
"text": user_message
|
117 |
-
}
|
118 |
-
]
|
119 |
}
|
120 |
-
]
|
121 |
)
|
122 |
output = response.content[0].text
|
123 |
|
@@ -128,7 +139,7 @@ You are allowed to make up film and branding names, and do them like 80's, 90's
|
|
128 |
temperature=0.7,
|
129 |
messages=[
|
130 |
{"role": "system", "content": system_message},
|
131 |
-
{"role": "user", "content": user_message}
|
132 |
],
|
133 |
)
|
134 |
output = response.choices[0].message.content.strip()
|
@@ -143,9 +154,9 @@ You are allowed to make up film and branding names, and do them like 80's, 90's
|
|
143 |
sentences = output.split(". ")
|
144 |
if len(sentences) > 1:
|
145 |
output = ". ".join(sentences[1:]).strip()
|
146 |
-
|
147 |
return output
|
148 |
|
149 |
except Exception as e:
|
150 |
print(f"An error occurred: {e}")
|
151 |
-
return f"Error occurred while processing the request: {str(e)}"
|
|
|
1 |
import os
|
|
|
|
|
|
|
2 |
import re
|
3 |
from datetime import datetime
|
4 |
|
5 |
+
import anthropic
|
6 |
+
from groq import Groq
|
7 |
+
from openai import OpenAI
|
8 |
+
|
9 |
huggingface_token = os.getenv("HUGGINGFACE_TOKEN")
|
10 |
groq_api_key = os.getenv("GROQ_API_KEY")
|
11 |
|
12 |
+
|
13 |
class LLMInferenceNode:
|
14 |
def __init__(self):
|
15 |
self.huggingface_client = OpenAI(
|
|
|
18 |
)
|
19 |
self.groq_client = Groq(api_key=groq_api_key)
|
20 |
|
21 |
+
def generate(
|
22 |
+
self,
|
23 |
+
input_text,
|
24 |
+
happy_talk,
|
25 |
+
compress,
|
26 |
+
compression_level,
|
27 |
+
poster,
|
28 |
+
prompt_type,
|
29 |
+
custom_base_prompt="",
|
30 |
+
provider="Hugging Face",
|
31 |
+
api_key=None,
|
32 |
+
model=None,
|
33 |
+
):
|
34 |
try:
|
35 |
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."""
|
36 |
|
|
|
61 |
"only_objects": only_objects_prompt,
|
62 |
"no_figure": no_figure_prompt,
|
63 |
"landscape": landscape_prompt,
|
64 |
+
"fantasy": fantasy_prompt,
|
65 |
}
|
66 |
|
67 |
# Update this part to handle the prompt_type correctly
|
|
|
74 |
print("Using custom base prompt")
|
75 |
else:
|
76 |
base_prompt = default_happy_prompt
|
77 |
+
print(
|
78 |
+
f"Warning: Unknown or empty prompt type '{prompt_type}'. Using default happy prompt."
|
79 |
+
)
|
80 |
|
81 |
if compress and not poster:
|
82 |
compression_chars = {
|
83 |
"soft": 600 if happy_talk else 300,
|
84 |
"medium": 400 if happy_talk else 200,
|
85 |
+
"hard": 200 if happy_talk else 100,
|
86 |
}
|
87 |
char_limit = compression_chars[compression_level]
|
88 |
base_prompt += f" Compress the output to be concise while retaining key visual details. MAX OUTPUT SIZE no more than {char_limit} characters."
|
|
|
98 |
top_p=0.95,
|
99 |
messages=[
|
100 |
{"role": "system", "content": system_message},
|
101 |
+
{"role": "user", "content": user_message},
|
102 |
],
|
103 |
)
|
104 |
output = response.choices[0].message.content.strip()
|
|
|
111 |
temperature=0.7,
|
112 |
messages=[
|
113 |
{"role": "system", "content": system_message},
|
114 |
+
{"role": "user", "content": user_message},
|
115 |
],
|
116 |
)
|
117 |
output = response.choices[0].message.content.strip()
|
|
|
126 |
messages=[
|
127 |
{
|
128 |
"role": "user",
|
129 |
+
"content": [{"type": "text", "text": user_message}],
|
|
|
|
|
|
|
|
|
|
|
130 |
}
|
131 |
+
],
|
132 |
)
|
133 |
output = response.content[0].text
|
134 |
|
|
|
139 |
temperature=0.7,
|
140 |
messages=[
|
141 |
{"role": "system", "content": system_message},
|
142 |
+
{"role": "user", "content": user_message},
|
143 |
],
|
144 |
)
|
145 |
output = response.choices[0].message.content.strip()
|
|
|
154 |
sentences = output.split(". ")
|
155 |
if len(sentences) > 1:
|
156 |
output = ". ".join(sentences[1:]).strip()
|
157 |
+
|
158 |
return output
|
159 |
|
160 |
except Exception as e:
|
161 |
print(f"An error occurred: {e}")
|
162 |
+
return f"Error occurred while processing the request: {str(e)}"
|
ui_components.py
CHANGED
@@ -215,13 +215,14 @@ def create_interface():
|
|
215 |
|
216 |
def generate_text_with_llm(output, happy_talk, compress, compression_level, custom_base_prompt, provider, api_key, model):
|
217 |
global selected_prompt_type
|
218 |
-
|
219 |
-
|
|
|
220 |
|
221 |
generate_text_button.click(
|
222 |
generate_text_with_llm,
|
223 |
inputs=[output, happy_talk, compress, compression_level, custom_base_prompt, llm_provider, api_key, model],
|
224 |
-
outputs=text_output,
|
225 |
api_name="generate_text"
|
226 |
)
|
227 |
|
|
|
215 |
|
216 |
def generate_text_with_llm(output, happy_talk, compress, compression_level, custom_base_prompt, provider, api_key, model):
|
217 |
global selected_prompt_type
|
218 |
+
result = llm_node.generate(output, happy_talk, compress, compression_level, False, selected_prompt_type, custom_base_prompt, provider, api_key, model)
|
219 |
+
selected_prompt_type = "happy" # Reset to "happy" after generation
|
220 |
+
return result, "happy" # Return the result and the new prompt type value
|
221 |
|
222 |
generate_text_button.click(
|
223 |
generate_text_with_llm,
|
224 |
inputs=[output, happy_talk, compress, compression_level, custom_base_prompt, llm_provider, api_key, model],
|
225 |
+
outputs=[text_output, prompt_type],
|
226 |
api_name="generate_text"
|
227 |
)
|
228 |
|