Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import random
|
| 3 |
+
import gradio as gr
|
| 4 |
+
|
| 5 |
+
def generate_prompt():
|
| 6 |
+
generated_prompt =[]
|
| 7 |
+
for category, values in choices.items():
|
| 8 |
+
choice = values.get()
|
| 9 |
+
selected_choice = choice
|
| 10 |
+
prompt_part = generate_category_prompt(category, selected_choice)
|
| 11 |
+
if prompt_part:
|
| 12 |
+
generated_prompt.append(prompt_part)
|
| 13 |
+
return ", ".join(generated_prompt)
|
| 14 |
+
|
| 15 |
+
def generate_category_prompt(category, choice):
|
| 16 |
+
if choice == 'random':
|
| 17 |
+
word = random.choice(categories[category]['random'])
|
| 18 |
+
#elif choice == 'none'
|
| 19 |
+
#return None
|
| 20 |
+
#else:
|
| 21 |
+
#word = choice
|
| 22 |
+
#return word
|
| 23 |
+
def generate_prompt_interface():
|
| 24 |
+
generated_prompt = generate_prompt()
|
| 25 |
+
result_text.value = generated_promt
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
#Load categories from the JSON file
|
| 29 |
+
with open('categories.json', 'r') as file:
|
| 30 |
+
categories = json.load(file)
|
| 31 |
+
|
| 32 |
+
#Create interative choices with gradio
|
| 33 |
+
choices = {}
|
| 34 |
+
|
| 35 |
+
for category, options in categories.items():
|
| 36 |
+
choice = gr.Dropdown(list(["random", "none"] + options['random']), label=category, type="value")
|
| 37 |
+
choices[category] = choice
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
#Create result text
|
| 41 |
+
result_text = gr.Textbox(Generated Prompt())
|
| 42 |
+
|
| 43 |
+
iface = gr.Interface(
|
| 44 |
+
fn=None,
|
| 45 |
+
live=True,
|
| 46 |
+
inputs=[choice for choice in choices.values()],
|
| 47 |
+
outputs=[result_text],
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
iface.launch()
|