gokaygokay
commited on
Commit
•
e1089fb
1
Parent(s):
6dc7e5a
files
Browse files- app.py +134 -0
- llm_inference.py +150 -0
- requirements.txt +2 -0
app.py
ADDED
@@ -0,0 +1,134 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from llm_inference import LLMInferenceNode
|
3 |
+
import random
|
4 |
+
|
5 |
+
title = """<h1 align="center">Random Prompt Generator</h1>
|
6 |
+
<p><center>
|
7 |
+
<a href="https://x.com/gokayfem" target="_blank">[X gokaygokay]</a>
|
8 |
+
<a href="https://github.com/gokayfem" target="_blank">[Github gokayfem]</a>
|
9 |
+
<a href="https://github.com/dagthomas/comfyui_dagthomas" target="_blank">[comfyui_dagthomas]</a>
|
10 |
+
<p align="center">Generate random prompts using powerful LLMs from Hugging Face, Groq, and SambaNova.</p>
|
11 |
+
</center></p>
|
12 |
+
"""
|
13 |
+
|
14 |
+
# Global variable to store selected prompt type
|
15 |
+
selected_prompt_type = "Long" # Default value
|
16 |
+
|
17 |
+
def create_interface():
|
18 |
+
llm_node = LLMInferenceNode()
|
19 |
+
|
20 |
+
with gr.Blocks(theme='bethecloud/storj_theme') as demo:
|
21 |
+
|
22 |
+
gr.HTML(title)
|
23 |
+
|
24 |
+
with gr.Row():
|
25 |
+
with gr.Column(scale=2):
|
26 |
+
with gr.Accordion("Basic Settings"):
|
27 |
+
custom = gr.Textbox(label="Custom Input Prompt (optional)")
|
28 |
+
|
29 |
+
with gr.Accordion("Prompt Generation Options", open=False):
|
30 |
+
prompt_type = gr.Dropdown(
|
31 |
+
choices=["Long", "Short", "Medium", "Long"],
|
32 |
+
label="Prompt Type",
|
33 |
+
value="Long",
|
34 |
+
interactive=True
|
35 |
+
)
|
36 |
+
|
37 |
+
# Function to update the selected prompt type
|
38 |
+
def update_prompt_type(value):
|
39 |
+
global selected_prompt_type
|
40 |
+
selected_prompt_type = value
|
41 |
+
print(f"Updated prompt type: {selected_prompt_type}")
|
42 |
+
return value
|
43 |
+
|
44 |
+
# Connect the update_prompt_type function to the prompt_type dropdown
|
45 |
+
prompt_type.change(update_prompt_type, inputs=[prompt_type], outputs=[prompt_type])
|
46 |
+
|
47 |
+
with gr.Column(scale=2):
|
48 |
+
generate_button = gr.Button("Generate Prompt")
|
49 |
+
|
50 |
+
with gr.Accordion("Generated Prompt", open=True):
|
51 |
+
output = gr.Textbox(label="Generated Prompt", lines=4, show_copy_button=True)
|
52 |
+
text_output = gr.Textbox(label="LLM Generated Text", lines=10, show_copy_button=True)
|
53 |
+
|
54 |
+
with gr.Column(scale=2):
|
55 |
+
with gr.Accordion("""LLM Prompt Generation""", open=False):
|
56 |
+
long_talk = gr.Checkbox(label="Long Talk", value=True)
|
57 |
+
compress = gr.Checkbox(label="Compress", value=True)
|
58 |
+
compression_level = gr.Dropdown(
|
59 |
+
choices=["soft", "medium", "hard"],
|
60 |
+
label="Compression Level",
|
61 |
+
value="hard"
|
62 |
+
)
|
63 |
+
|
64 |
+
custom_base_prompt = gr.Textbox(label="Custom Base Prompt", lines=5)
|
65 |
+
|
66 |
+
# LLM Provider Selection
|
67 |
+
llm_provider = gr.Dropdown(
|
68 |
+
choices=["Hugging Face", "Groq", "SambaNova"],
|
69 |
+
label="LLM Provider",
|
70 |
+
value="Hugging Face"
|
71 |
+
)
|
72 |
+
api_key = gr.Textbox(label="API Key", type="password", visible=False)
|
73 |
+
model = gr.Dropdown(label="Model", choices=[], value="")
|
74 |
+
|
75 |
+
generate_text_button = gr.Button("Generate Prompt with LLM")
|
76 |
+
text_output = gr.Textbox(label="Generated Text", lines=10, show_copy_button=True)
|
77 |
+
|
78 |
+
# Initialize Models based on provider
|
79 |
+
def update_model_choices(provider):
|
80 |
+
provider_models = {
|
81 |
+
"Hugging Face": ["meta-llama/Meta-Llama-3.1-70B-Instruct", "another-model-hf"],
|
82 |
+
"Groq": ["llama-3.1-70b-versatile", "mixtral-8x7b-32768", "gemma2-9b-it"],
|
83 |
+
"SambaNova": ["Meta-Llama-3.1-70B-Instruct", "Meta-Llama-3.1-405B-Instruct", "Meta-Llama-3.1-8B-Instruct"],
|
84 |
+
}
|
85 |
+
models = provider_models.get(provider, [])
|
86 |
+
return gr.Dropdown.update(choices=models, value=models[0] if models else "")
|
87 |
+
|
88 |
+
def update_api_key_visibility(provider):
|
89 |
+
return gr.update(visible=False) # No API key required for selected providers
|
90 |
+
|
91 |
+
llm_provider.change(update_model_choices, inputs=[llm_provider], outputs=[model])
|
92 |
+
llm_provider.change(update_api_key_visibility, inputs=[llm_provider], outputs=[api_key])
|
93 |
+
|
94 |
+
# Generate Prompt Function
|
95 |
+
def generate_prompt(prompt_type, custom_input):
|
96 |
+
dynamic_seed = random.randint(0, 1000000)
|
97 |
+
result = llm_node.generate_prompt(dynamic_seed, prompt_type, custom_input)
|
98 |
+
return result
|
99 |
+
|
100 |
+
generate_button.click(
|
101 |
+
generate_prompt,
|
102 |
+
inputs=[prompt_type, custom],
|
103 |
+
outputs=[output]
|
104 |
+
)
|
105 |
+
|
106 |
+
# Generate Text with LLM
|
107 |
+
def generate_text_with_llm(output_prompt, long_talk, compress, compression_level, custom_base_prompt, provider, api_key, model_selected):
|
108 |
+
global selected_prompt_type
|
109 |
+
result = llm_node.generate(
|
110 |
+
input_text=output_prompt,
|
111 |
+
long_talk=long_talk,
|
112 |
+
compress=compress,
|
113 |
+
compression_level=compression_level,
|
114 |
+
prompt_type=selected_prompt_type,
|
115 |
+
custom_base_prompt=custom_base_prompt,
|
116 |
+
provider=provider,
|
117 |
+
api_key=api_key,
|
118 |
+
model=model_selected
|
119 |
+
)
|
120 |
+
selected_prompt_type = "Long"
|
121 |
+
return result
|
122 |
+
|
123 |
+
generate_text_button.click(
|
124 |
+
generate_text_with_llm,
|
125 |
+
inputs=[output, long_talk, compress, compression_level, custom_base_prompt, llm_provider, api_key, model],
|
126 |
+
outputs=[text_output],
|
127 |
+
api_name="generate_text"
|
128 |
+
)
|
129 |
+
|
130 |
+
return demo
|
131 |
+
|
132 |
+
if __name__ == "__main__":
|
133 |
+
demo = create_interface()
|
134 |
+
demo.launch()
|
llm_inference.py
ADDED
@@ -0,0 +1,150 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from groq import Groq
|
3 |
+
from openai import OpenAI
|
4 |
+
import anthropic
|
5 |
+
|
6 |
+
class LLMInferenceNode:
|
7 |
+
def __init__(self):
|
8 |
+
self.huggingface_token = os.getenv("HUGGINGFACE_TOKEN")
|
9 |
+
self.groq_api_key = os.getenv("GROQ_API_KEY")
|
10 |
+
self.sambanova_api_key = os.getenv("SAMBANOVA_API_KEY")
|
11 |
+
|
12 |
+
self.huggingface_client = OpenAI(
|
13 |
+
base_url="https://api-inference.huggingface.co/v1/",
|
14 |
+
api_key=self.huggingface_token,
|
15 |
+
)
|
16 |
+
self.groq_client = Groq(api_key=self.groq_api_key)
|
17 |
+
self.sambanova_client = OpenAI(
|
18 |
+
api_key=self.sambanova_api_key,
|
19 |
+
base_url="https://api.sambanova.ai/v1",
|
20 |
+
)
|
21 |
+
|
22 |
+
def generate(
|
23 |
+
self,
|
24 |
+
input_text,
|
25 |
+
long_talk,
|
26 |
+
compress,
|
27 |
+
compression_level,
|
28 |
+
poster,
|
29 |
+
prompt_type,
|
30 |
+
custom_base_prompt="",
|
31 |
+
provider="Hugging Face",
|
32 |
+
api_key=None,
|
33 |
+
model=None,
|
34 |
+
):
|
35 |
+
try:
|
36 |
+
# Define prompts
|
37 |
+
default_long_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."""
|
38 |
+
|
39 |
+
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. Your output is only the caption itself, no comments or extra formatting. The caption is in a single long paragraph."""
|
40 |
+
|
41 |
+
poster_prompt = """Analyze the provided description and extract key information to create a movie poster style description. Format the output as follows:
|
42 |
+
Title: A catchy, intriguing title that captures the essence of the scene, place the title in "".
|
43 |
+
Main character: Give a description of the main character.
|
44 |
+
Background: Describe the background in detail.
|
45 |
+
Supporting characters: Describe the supporting characters.
|
46 |
+
Branding type: Describe the branding type.
|
47 |
+
Tagline: Include a tagline that captures the essence of the movie.
|
48 |
+
Visual style: Ensure that the visual style fits the branding type and tagline.
|
49 |
+
You are allowed to make up film and branding names, and do them like 80's, 90's or modern movie posters. Your output is only the caption itself, no comments or extra formatting. The caption is in a single long paragraph."""
|
50 |
+
|
51 |
+
only_objects_prompt = """Create a highly detailed and visually rich description focusing solely on inanimate objects, without including any human or animal figures. Describe the objects' shapes, sizes, colors, textures, and materials in great detail. Pay attention to their arrangement, positioning, and how they interact with light and shadow. Include information about the setting or environment these objects are in, such as indoor/outdoor, time of day, weather conditions, and any atmospheric effects. Mention any unique features, patterns, or imperfections on the objects. Describe the overall composition, perspective, and any artistic techniques that might be employed to render these objects (e.g., photorealism, impressionistic style, etc.). Your description should paint a vivid picture that allows someone to imagine the scene without seeing it, focusing on the beauty, complexity, or significance of everyday objects. Your output is only the caption itself, no comments or extra formatting. The caption is in a single long paragraph."""
|
52 |
+
|
53 |
+
no_figure_prompt = """Generate a comprehensive and visually evocative description of a scene or landscape without including any human or animal figures. Focus on the environment, natural elements, and man-made structures if present. Describe the topography, vegetation, weather conditions, and time of day in great detail. Pay attention to colors, textures, and how light interacts with different elements of the scene. If there are buildings or other structures, describe their architecture, condition, and how they fit into the landscape. Include sensory details beyond just visual elements - mention sounds, smells, and the overall atmosphere or mood of the scene. Describe any notable features like bodies of water, geological formations, or sky phenomena. Consider the perspective from which the scene is viewed and how this affects the composition. Your description should transport the reader to this location, allowing them to vividly imagine the scene without any living subjects present. Your output is only the caption itself, no comments or extra formatting. The caption is in a single long paragraph."""
|
54 |
+
|
55 |
+
landscape_prompt = """Create an immersive and detailed description of a landscape, focusing on its natural beauty and geographical features. Begin with the overall topography - is it mountainous, coastal, forested, desert, or a combination? Describe the horizon and how land meets sky. Detail the vegetation, noting types of trees, flowers, or grass, and how they're distributed across the landscape. Include information about any water features - rivers, lakes, oceans - and how they interact with the land. Describe the sky, including cloud formations, color gradients, and any celestial bodies visible. Pay attention to the quality of light, time of day, and season, explaining how these factors affect the colors and shadows in the scene. Include details about weather conditions and how they impact the landscape. Mention any geological features like rock formations, cliffs, or unique land patterns. If there are any distant man-made elements, describe how they integrate with the natural setting. Your description should capture the grandeur and mood of the landscape, allowing the reader to feel as if they're standing within this awe-inspiring natural scene. Your output is only the caption itself, no comments or extra formatting. The caption is in a single long paragraph."""
|
56 |
+
|
57 |
+
fantasy_prompt = """Craft an extraordinarily detailed and imaginative description of a fantasy scene, blending elements of magic, otherworldly creatures, and fantastical environments. Begin by setting the overall tone - is this a dark and foreboding realm, a whimsical fairytale setting, or an epic high-fantasy world? Describe the landscape, including any impossible or magical geographical features like floating islands, crystal forests, or rivers of starlight. Detail the flora and fauna, focusing on fantastical plants and creatures that don't exist in our world. Include descriptions of any structures or ruins, emphasizing their otherworldly architecture and magical properties. Describe the sky and any celestial bodies, considering how they might differ from our reality. Include details about the presence of magic - how it manifests visually, its effects on the environment, and any magical phenomena occurring in the scene. If there are characters present, describe their appearance, focusing on non-human features, magical auras, or fantastical clothing and accessories. Pay attention to colors, textures, and light sources, especially those that couldn't exist in the real world. Your description should transport the reader to a realm of pure imagination, where the laws of physics and nature as we know them don't apply. Your output is only the caption itself, no comments or extra formatting. The caption is in a single long paragraph."""
|
58 |
+
|
59 |
+
prompt_types = {
|
60 |
+
"Long": default_long_prompt,
|
61 |
+
"Short": default_simple_prompt,
|
62 |
+
"Medium": poster_prompt,
|
63 |
+
"OnlyObjects": only_objects_prompt,
|
64 |
+
"NoFigure": no_figure_prompt,
|
65 |
+
"Landscape": landscape_prompt,
|
66 |
+
"Fantasy": fantasy_prompt,
|
67 |
+
}
|
68 |
+
|
69 |
+
# Determine the base prompt
|
70 |
+
print(f"Received prompt_type: '{prompt_type}'") # Debug print
|
71 |
+
if prompt_type and prompt_type.strip() and prompt_type in prompt_types:
|
72 |
+
base_prompt = prompt_types[prompt_type]
|
73 |
+
print(f"Using {prompt_type} prompt")
|
74 |
+
elif custom_base_prompt.strip():
|
75 |
+
base_prompt = custom_base_prompt
|
76 |
+
print("Using custom base prompt")
|
77 |
+
else:
|
78 |
+
base_prompt = default_long_prompt
|
79 |
+
print(
|
80 |
+
f"Warning: Unknown or empty prompt type '{prompt_type}'. Using default long prompt."
|
81 |
+
)
|
82 |
+
|
83 |
+
# Handle compression if applicable
|
84 |
+
if compress and not poster:
|
85 |
+
compression_chars = {
|
86 |
+
"soft": 600 if long_talk else 300,
|
87 |
+
"medium": 400 if long_talk else 200,
|
88 |
+
"hard": 200 if long_talk else 100,
|
89 |
+
}
|
90 |
+
char_limit = compression_chars.get(compression_level, 200)
|
91 |
+
base_prompt += f" Compress the output to be concise while retaining key visual details. MAX OUTPUT SIZE no more than {char_limit} characters."
|
92 |
+
|
93 |
+
# Construct messages for the LLM
|
94 |
+
system_message = "You are a helpful assistant. Try your best to give the best response possible to the user."
|
95 |
+
user_message = f"{base_prompt}\nDescription: {input_text}"
|
96 |
+
|
97 |
+
# Select the appropriate provider
|
98 |
+
if provider == "Hugging Face":
|
99 |
+
response = self.huggingface_client.chat.completions.create(
|
100 |
+
model=model or "meta-llama/Meta-Llama-3.1-70B-Instruct",
|
101 |
+
max_tokens=1024,
|
102 |
+
temperature=0.7,
|
103 |
+
top_p=0.95,
|
104 |
+
messages=[
|
105 |
+
{"role": "system", "content": system_message},
|
106 |
+
{"role": "user", "content": user_message},
|
107 |
+
],
|
108 |
+
)
|
109 |
+
output = response.choices[0].message.content.strip()
|
110 |
+
|
111 |
+
elif provider == "Groq":
|
112 |
+
response = self.groq_client.chat.completions.create(
|
113 |
+
model=model or "llama-3.1-70b-versatile",
|
114 |
+
max_tokens=1024,
|
115 |
+
temperature=0.7,
|
116 |
+
messages=[
|
117 |
+
{"role": "system", "content": system_message},
|
118 |
+
{"role": "user", "content": user_message},
|
119 |
+
],
|
120 |
+
)
|
121 |
+
output = response.choices[0].message.content.strip()
|
122 |
+
|
123 |
+
elif provider == "SambaNova":
|
124 |
+
response = self.sambanova_client.chat.completions.create(
|
125 |
+
model=model or "Meta-Llama-3.1-70B-Instruct",
|
126 |
+
max_tokens=1024,
|
127 |
+
temperature=0.7,
|
128 |
+
messages=[
|
129 |
+
{"role": "system", "content": system_message},
|
130 |
+
{"role": "user", "content": user_message},
|
131 |
+
],
|
132 |
+
)
|
133 |
+
output = response.choices[0].message.content.strip()
|
134 |
+
|
135 |
+
else:
|
136 |
+
raise ValueError(f"Unsupported provider: {provider}")
|
137 |
+
|
138 |
+
# Clean up the output if necessary
|
139 |
+
if ": " in output:
|
140 |
+
output = output.split(": ", 1)[1].strip()
|
141 |
+
elif output.lower().startswith("here"):
|
142 |
+
sentences = output.split(". ")
|
143 |
+
if len(sentences) > 1:
|
144 |
+
output = ". ".join(sentences[1:]).strip()
|
145 |
+
|
146 |
+
return output
|
147 |
+
|
148 |
+
except Exception as e:
|
149 |
+
print(f"An error occurred: {e}")
|
150 |
+
return f"Error occurred while processing the request: {str(e)}"
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
openai
|
2 |
+
groq
|