Update app.py
Browse files
app.py
CHANGED
|
@@ -8,20 +8,30 @@ from huggingface_hub import InferenceClient
|
|
| 8 |
import subprocess
|
| 9 |
import torch
|
| 10 |
from PIL import Image
|
| 11 |
-
from transformers import AutoProcessor, AutoModelForCausalLM
|
| 12 |
-
import random
|
| 13 |
|
|
|
|
| 14 |
subprocess.run('pip install flash-attn --no-build-isolation', env={'FLASH_ATTENTION_SKIP_CUDA_BUILD': "TRUE"}, shell=True)
|
| 15 |
|
|
|
|
| 16 |
huggingface_token = os.getenv("HUGGINGFACE_TOKEN")
|
| 17 |
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
#
|
| 20 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 21 |
florence_model = AutoModelForCausalLM.from_pretrained('microsoft/Florence-2-base', trust_remote_code=True).to(device).eval()
|
| 22 |
florence_processor = AutoProcessor.from_pretrained('microsoft/Florence-2-base', trust_remote_code=True)
|
| 23 |
|
| 24 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
def florence_caption(image):
|
| 26 |
if not isinstance(image, Image.Image):
|
| 27 |
image = Image.fromarray(image)
|
|
@@ -42,13 +52,14 @@ def florence_caption(image):
|
|
| 42 |
image_size=(image.width, image.height)
|
| 43 |
)
|
| 44 |
return parsed_answer["<MORE_DETAILED_CAPTION>"]
|
| 45 |
-
|
| 46 |
-
#
|
| 47 |
def load_json_file(file_name):
|
| 48 |
file_path = os.path.join("data", file_name)
|
| 49 |
with open(file_path, "r") as file:
|
| 50 |
return json.load(file)
|
| 51 |
|
|
|
|
| 52 |
ARTFORM = load_json_file("artform.json")
|
| 53 |
PHOTO_TYPE = load_json_file("photo_type.json")
|
| 54 |
BODY_TYPES = load_json_file("body_types.json")
|
|
@@ -68,6 +79,7 @@ COMPOSITION = load_json_file("composition.json")
|
|
| 68 |
POSE = load_json_file("pose.json")
|
| 69 |
BACKGROUND = load_json_file("background.json")
|
| 70 |
|
|
|
|
| 71 |
class PromptGenerator:
|
| 72 |
def __init__(self, seed=None):
|
| 73 |
self.rng = random.Random(seed)
|
|
@@ -147,6 +159,7 @@ class PromptGenerator:
|
|
| 147 |
components = []
|
| 148 |
custom = kwargs.get("custom", "")
|
| 149 |
if custom:
|
|
|
|
| 150 |
components.append(custom)
|
| 151 |
is_photographer = kwargs.get("artform", "").lower() == "photography" or (
|
| 152 |
kwargs.get("artform", "").lower() == "random"
|
|
@@ -390,13 +403,19 @@ You are allowed to make up film and branding names, and do them like 80's, 90's
|
|
| 390 |
print(f"An error occurred: {e}")
|
| 391 |
return f"Error occurred while processing the request: {str(e)}"
|
| 392 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 393 |
def create_interface():
|
| 394 |
prompt_generator = PromptGenerator()
|
| 395 |
huggingface_node = HuggingFaceInferenceNode()
|
| 396 |
|
| 397 |
-
with gr.Blocks(theme="Nymbo/Nymbo_Theme") as demo:
|
| 398 |
|
| 399 |
-
gr.HTML("<h1>
|
| 400 |
|
| 401 |
with gr.Row():
|
| 402 |
with gr.Column(scale=2):
|
|
@@ -533,3 +552,4 @@ def create_interface():
|
|
| 533 |
if __name__ == "__main__":
|
| 534 |
demo = create_interface()
|
| 535 |
demo.launch()
|
|
|
|
|
|
| 8 |
import subprocess
|
| 9 |
import torch
|
| 10 |
from PIL import Image
|
| 11 |
+
from transformers import AutoProcessor, AutoModelForCausalLM, pipeline
|
|
|
|
| 12 |
|
| 13 |
+
# ์ค์น ๊ณผ์ ์ ์๋ต ๊ฐ๋ฅํ๋ฉฐ ํ์ํ ๊ฒฝ์ฐ์๋ง ์คํ
|
| 14 |
subprocess.run('pip install flash-attn --no-build-isolation', env={'FLASH_ATTENTION_SKIP_CUDA_BUILD': "TRUE"}, shell=True)
|
| 15 |
|
| 16 |
+
# Hugging Face ํ ํฐ ์ค์
|
| 17 |
huggingface_token = os.getenv("HUGGINGFACE_TOKEN")
|
| 18 |
|
| 19 |
+
# ๋ฒ์ญ ๋ชจ๋ธ ์ถ๊ฐ
|
| 20 |
+
translator = pipeline("translation", model="Helsinki-NLP/opus-mt-ko-en")
|
| 21 |
|
| 22 |
+
# Florence ๋ชจ๋ธ ์ด๊ธฐํ
|
| 23 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 24 |
florence_model = AutoModelForCausalLM.from_pretrained('microsoft/Florence-2-base', trust_remote_code=True).to(device).eval()
|
| 25 |
florence_processor = AutoProcessor.from_pretrained('microsoft/Florence-2-base', trust_remote_code=True)
|
| 26 |
|
| 27 |
+
# ํ๊ธ ํ๋กฌํํธ ๋ฒ์ญ ํจ์
|
| 28 |
+
def translate_prompt(prompt):
|
| 29 |
+
if any("\uAC00" <= char <= "\uD7A3" for char in prompt): # ํ๊ธ์ด ํฌํจ๋ ๊ฒฝ์ฐ
|
| 30 |
+
translated = translator(prompt, max_length=512)[0]['translation_text']
|
| 31 |
+
return translated
|
| 32 |
+
return prompt
|
| 33 |
+
|
| 34 |
+
# Florence ์บก์
ํจ์
|
| 35 |
def florence_caption(image):
|
| 36 |
if not isinstance(image, Image.Image):
|
| 37 |
image = Image.fromarray(image)
|
|
|
|
| 52 |
image_size=(image.width, image.height)
|
| 53 |
)
|
| 54 |
return parsed_answer["<MORE_DETAILED_CAPTION>"]
|
| 55 |
+
|
| 56 |
+
# JSON ํ์ผ ๋ก๋ ํจ์
|
| 57 |
def load_json_file(file_name):
|
| 58 |
file_path = os.path.join("data", file_name)
|
| 59 |
with open(file_path, "r") as file:
|
| 60 |
return json.load(file)
|
| 61 |
|
| 62 |
+
# JSON ๋ฐ์ดํฐ ๋ก๋
|
| 63 |
ARTFORM = load_json_file("artform.json")
|
| 64 |
PHOTO_TYPE = load_json_file("photo_type.json")
|
| 65 |
BODY_TYPES = load_json_file("body_types.json")
|
|
|
|
| 79 |
POSE = load_json_file("pose.json")
|
| 80 |
BACKGROUND = load_json_file("background.json")
|
| 81 |
|
| 82 |
+
# ํ๋กฌํํธ ์์ฑ ํด๋์ค
|
| 83 |
class PromptGenerator:
|
| 84 |
def __init__(self, seed=None):
|
| 85 |
self.rng = random.Random(seed)
|
|
|
|
| 159 |
components = []
|
| 160 |
custom = kwargs.get("custom", "")
|
| 161 |
if custom:
|
| 162 |
+
custom = translate_prompt(custom) # ๋ฒ์ญ ์ ์ฉ
|
| 163 |
components.append(custom)
|
| 164 |
is_photographer = kwargs.get("artform", "").lower() == "photography" or (
|
| 165 |
kwargs.get("artform", "").lower() == "random"
|
|
|
|
| 403 |
print(f"An error occurred: {e}")
|
| 404 |
return f"Error occurred while processing the request: {str(e)}"
|
| 405 |
|
| 406 |
+
css = """
|
| 407 |
+
footer {
|
| 408 |
+
visibility: hidden;
|
| 409 |
+
}
|
| 410 |
+
"""
|
| 411 |
+
|
| 412 |
def create_interface():
|
| 413 |
prompt_generator = PromptGenerator()
|
| 414 |
huggingface_node = HuggingFaceInferenceNode()
|
| 415 |
|
| 416 |
+
with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css) as demo:
|
| 417 |
|
| 418 |
+
gr.HTML("<h1>Flux Prompt Generator</h1>")
|
| 419 |
|
| 420 |
with gr.Row():
|
| 421 |
with gr.Column(scale=2):
|
|
|
|
| 552 |
if __name__ == "__main__":
|
| 553 |
demo = create_interface()
|
| 554 |
demo.launch()
|
| 555 |
+
|