Spaces:
Sleeping
Sleeping
import os | |
import gradio as gr | |
import random | |
import time | |
import logging | |
import google.generativeai as genai | |
import torch | |
import numpy as np | |
from diffusers import DiffusionPipeline | |
from transformers import pipeline as hf_pipeline | |
############################################################################## | |
# 1) ZeroGPU 환경 처리 + device, dtype 설정 | |
############################################################################## | |
############################################################################## | |
# 1) ZeroGPU 환경 처리 + device, dtype 설정 | |
############################################################################## | |
# ZeroGPU 초기화 시도 | |
try: | |
import zerogpu | |
zerogpu.init() | |
print("ZeroGPU initialized successfully") | |
device = "cuda" if torch.cuda.is_available() else "cpu" | |
except ImportError: | |
# ZeroGPU가 설치되지 않은 경우 | |
print("ZeroGPU package not installed, continuing without it") | |
if os.getenv("ZERO_GPU"): | |
print("ZeroGPU environment variable is set but zerogpu package is not installed.") | |
device = "cuda" if torch.cuda.is_available() else "cpu" | |
except Exception as e: | |
# ZeroGPU 초기화 중 다른 오류가 발생한 경우 | |
print(f"Error initializing ZeroGPU: {e}") | |
print("Continuing without ZeroGPU") | |
device = "cuda" if torch.cuda.is_available() else "cpu" | |
# GPU일 때만 bfloat16, 그 외에는 float32 | |
dtype = torch.bfloat16 if device == "cuda" else torch.float32 | |
print(f"Using device: {device}, dtype: {dtype}") | |
############################################################################## | |
# 2) 모델 로드: 번역 모델, DiffusionPipeline | |
############################################################################## | |
try: | |
translator = hf_pipeline( | |
"translation", | |
model="Helsinki-NLP/opus-mt-ko-en", | |
device=0 if device == "cuda" else -1 | |
) | |
pipe = DiffusionPipeline.from_pretrained( | |
"black-forest-labs/FLUX.1-schnell", | |
torch_dtype=dtype | |
).to(device) | |
print("Models loaded successfully") | |
except Exception as e: | |
print(f"Error loading models: {e}") | |
# 모델 로드 에러 처리를 위한 더미 함수들 | |
def dummy_translator(text): | |
return [{'translation_text': text}] | |
class DummyPipe: | |
def __call__(self, **kwargs): | |
from PIL import Image | |
import numpy as np | |
dummy_img = Image.fromarray(np.zeros((512, 512, 3), dtype=np.uint8)) | |
class DummyResult: | |
def __init__(self, img): | |
self.images = [img] | |
return DummyResult(dummy_img) | |
translator = dummy_translator | |
pipe = DummyPipe() | |
MAX_SEED = np.iinfo(np.int32).max | |
MAX_IMAGE_SIZE = 2048 | |
############################################################################## | |
# 한국어 감지 함수 | |
############################################################################## | |
def contains_korean(text): | |
for char in text: | |
if ord('가') <= ord(char) <= ord('힣'): | |
return True | |
return False | |
############################################################################## | |
# 이미지 생성 함수 | |
############################################################################## | |
def generate_design_image(prompt, seed=42, randomize_seed=True, width=1024, height=1024, num_inference_steps=4): | |
""" | |
생성된 확장 아이디어 텍스트(prompt)를 입력받아, | |
필요시 한국어를 영어로 번역한 후 DiffusionPipeline으로 이미지를 생성합니다. | |
""" | |
original_prompt = prompt | |
translated = False | |
# 한국어가 포함되어 있으면 영어로 번역 | |
if contains_korean(prompt): | |
translation = translator(prompt) | |
prompt = translation[0]['translation_text'] | |
translated = True | |
# 랜덤 시드 설정 | |
if randomize_seed: | |
seed = random.randint(0, MAX_SEED) | |
generator = torch.Generator(device=device).manual_seed(seed) | |
image = pipe( | |
prompt=prompt, | |
width=width, | |
height=height, | |
num_inference_steps=num_inference_steps, | |
generator=generator, | |
guidance_scale=0.0 | |
).images[0] | |
return image | |
############################################################################## | |
# 로깅 설정 | |
############################################################################## | |
logging.basicConfig( | |
level=logging.INFO, | |
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', | |
handlers=[ | |
logging.FileHandler("api_debug.log"), | |
logging.StreamHandler() | |
] | |
) | |
logger = logging.getLogger("idea_generator") | |
############################################################################## | |
# Gemini API 키 | |
############################################################################## | |
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY") | |
genai.configure(api_key=GEMINI_API_KEY) | |
############################################################################## | |
# 선택적 변형 선택 함수 | |
############################################################################## | |
def choose_alternative(transformation): | |
if "/" not in transformation: | |
return transformation | |
parts = transformation.split("/") | |
if len(parts) != 2: | |
return random.choice([part.strip() for part in parts]) | |
left = parts[0].strip() | |
right = parts[1].strip() | |
if " " in left: | |
tokens = left.split(" ", 1) | |
prefix = tokens[0] | |
if not right.startswith(prefix): | |
option1 = left | |
option2 = prefix + " " + right | |
else: | |
option1 = left | |
option2 = right | |
return random.choice([option1, option2]) | |
else: | |
return random.choice([left, right]) | |
############################################################################## | |
# 물리적 변화 카테고리 사전 (총 15개) | |
############################################################################## | |
physical_transformation_categories = { | |
"공간 이동": [ | |
"앞/뒤 이동", "좌/우 이동", "위/아래 이동", "세로축 회전(고개 끄덕임)", | |
"가로축 회전(고개 젓기)", "길이축 회전(옆으로 기울임)", "원 운동", "나선형 이동", | |
"관성에 의한 미끄러짐", "회전축 변화", "불규칙 회전", "흔들림 운동", "포물선 이동", | |
"무중력 부유", "수면 위 부유", "점프/도약", "슬라이딩", "롤링", "자유 낙하", | |
"왕복 운동", "탄성 튕김", "관통", "회피 움직임", "지그재그 이동", "스윙 운동" | |
], | |
"크기와 형태 변화": [ | |
"부피 늘어남/줄어듦", "길이 늘어남/줄어듦", "너비 늘어남/줄어듦", "높이 늘어남/줄어듦", | |
"밀도 변화", "무게 증가/감소", "모양 변형", "상태 변화", "불균등 변형", | |
"복잡한 형태 변형", "비틀림/꼬임", "불균일한 확장/축소", "모서리 둥글게/날카롭게", | |
"깨짐/갈라짐", "여러 조각 나눠짐", "물 저항", "먼지 저항", "찌그러짐/복원", | |
"접힘/펼쳐짐", "압착/팽창", "늘어남/수축", "구겨짐/평평해짐", "뭉개짐/단단해짐", | |
"말림/펴짐", "꺾임/구부러짐" | |
], | |
"표면 및 외관 변화": [ | |
"색상 변화", "질감 변화", "투명/불투명 변화", "반짝임/무광 변화", | |
"빛 반사 정도 변화", "무늬 변화", "각도에 따른 색상 변화", "빛에 따른 색상 변화", | |
"온도에 따른 색상 변화", "홀로그램 효과", "표면 각도별 빛 반사", "표면 모양 변형", | |
"초미세 표면 구조 변화", "자가 세정 효과", "얼룩/패턴 생성", "흐림/선명함 변화", | |
"광택/윤기 변화", "색조/채도 변화", "발광/형광", "빛 산란 효과", | |
"빛 흡수 변화", "반투명 효과", "그림자 효과 변화", "자외선 반응 변화", | |
"야광 효과" | |
], | |
"물질의 상태 변화": [ | |
"고체/액체/기체 전환", "결정화/용해", "산화/부식", "딱딱해짐/부드러워짐", | |
"특수 상태 전환", "무정형/결정형 전환", "성분 분리", "미세 입자 형성/분해", | |
"젤 형성/풀어짐", "준안정 상태 변화", "분자 자가 정렬/분해", "상태변화 지연 현상", | |
"녹음", "굳음", "증발/응축", "승화/증착", "침전/부유", "분산/응집", | |
"건조/습윤", "팽윤/수축", "동결/해동", "풍화/침식", "충전/방전", | |
"결합/분리", "발효/부패" | |
], | |
"열 관련 변화": [ | |
"온도 상승/하강", "열에 의한 팽창/수축", "열 전달/차단", "압력 상승/하강", | |
"열 변화에 따른 자화", "무질서도 변화", "열전기 현상", "자기장에 의한 열 변화", | |
"상태변화 중 열 저장/방출", "열 스트레스 발생/해소", "급격한 온도 변화 영향", | |
"복사열에 의한 냉각/가열", "발열/흡열", "열 분포 변화", "열 반사/흡수", | |
"냉각 응축", "열 활성화", "열 변색", "열 팽창 계수 변화", "열 안정성 변화", | |
"내열성/내한성", "자기발열", "열적 평형/불균형", "열적 변형", "열 분산/집중" | |
], | |
"움직임 특성 변화": [ | |
"가속/감속", "일정 속도 유지", "진동/진동 감소", "부딪힘/튕김", | |
"회전 속도 증가/감소", "회전 방향 변화", "불규칙 움직임", "멈췄다 미끄러지는 현상", | |
"공진/반공진", "유체 속 저항/양력 변화", "움직임 저항 변화", "복합 진동 움직임", | |
"특수 유체 속 움직임", "회전-이동 연계 움직임", "관성 정지", "충격 흡수", | |
"충격 전달", "운동량 보존", "마찰력 변화", "관성 탈출", "불안정 균형", | |
"동적 안정성", "흔들림 감쇠", "경로 예측성", "회피 움직임" | |
], | |
"구조적 변화": [ | |
"부품 추가/제거", "조립/분해", "접기/펴기", "변형/원상복구", "최적 구조 변화", | |
"자가 재배열", "자연 패턴 형성/소멸", "규칙적 패턴 변화", "모듈식 변형", | |
"복잡성 증가 구조", "원래 모양 기억 효과", "시간에 따른 형태 변화", "부분 제거", | |
"부분 교체", "결합", "분리", "분할/통합", "중첩/겹침", "내부 구조 변화", | |
"외부 구조 변화", "중심축 이동", "균형점 변화", "계층 구조 변화", "지지 구조 변화", | |
"응력 분산 구조", "충격 흡수 구조", "그리드/매트릭스 구조 변화", "상호 연결성 변화" | |
], | |
"전기 및 자기 변화": [ | |
"자성 생성/소멸", "전하량 증가/감소", "전기장 생성/소멸", "자기장 생성/소멸", | |
"초전도 상태 전환", "강유전체 특성 변화", "양자 상태 변화", "플라즈마 상태 형성/소멸", | |
"스핀파 전달", "빛에 의한 전기 발생", "압력에 의한 전기 발생", "자기장 속 전류 변화", | |
"전기 저항 변화", "전기 전도성 변화", "정전기 발생/방전", "전자기 유도", | |
"전자기파 방출/흡수", "전기 용량 변화", "자기 이력 현상", "전기적 분극", | |
"전자 흐름 방향 변화", "전기적 공명", "전기적 차폐/노출", "자기 차폐/노출", | |
"자기장 방향 정렬" | |
], | |
"화학적 변화": [ | |
"표면 코팅 변화", "물질 성분 변화", "화학 반응 변화", "촉매 작용 시작/중단", | |
"빛에 의한 화학 반응", "전기에 의한 화학 반응", "단분자막 형성", "분자 수준 계산 변화", | |
"자연 모방 표면 변화", "환경 반응형 물질 변화", "주기적 화학 반응", "산화", "환원", | |
"고분자화", "물 분해", "화합", "방사선 영향", "산-염기 반응", "중화 반응", | |
"이온화", "화학적 흡착/탈착", "촉매 효율 변화", "효소 활성 변화", "발색 반응", | |
"pH 변화", "화학적 평형 이동", "결합 형성/분해", "용해도 변화" | |
], | |
"시간 관련 변화": [ | |
"노화/풍화", "마모/부식", "색 바램/변색", "손상/회복", "수명 주기 변화", | |
"사용자 상호작용에 따른 적응", "학습 기반 형태 최적화", "시간에 따른 물성 변화", | |
"집단 기억 효과", "문화적 의미 변화", "지연 반응", "이전 상태 의존 변화", | |
"점진적 시간 변화", "진화적 변화", "주기적 재생", "계절 변화 적응", | |
"생체리듬 변화", "생애 주기 단계", "성장/퇴화", "자기 복구/재생", | |
"자연 순환 적응", "지속성/일시성", "기억 효과", "지연된 작용", "누적 효과" | |
], | |
"빛과 시각 효과": [ | |
"발광/소등", "빛 투과/차단", "빛 산란/집중", "색상 스펙트럼 변화", "빛 회절", | |
"빛 간섭", "홀로그램 생성", "레이저 효과", "빛 편광", "형광/인광", | |
"자외선/적외선 발광", "광학적 착시", "빛 굴절", "그림자 생성/제거", | |
"색수차 효과", "무지개 효과", "글로우 효과", "플래시 효과", "조명 패턴", | |
"빔 효과", "광 필터 효과", "빛의 방향성 변화", "투영 효과", "빛 감지/반응", | |
"광도 변화" | |
], | |
"소리와 진동 효과": [ | |
"소리 발생/소멸", "소리 높낮이 변화", "소리 크기 변화", "음색 변화", | |
"공명/반공명", "음향 진동", "초음파/저음파 발생", "음향 집중/분산", | |
"음향 반사/흡수", "음향 도플러 효과", "음파 간섭", "음향 공진", | |
"진동 패턴 변화", "타악 효과", "음향 피드백", "음향 차폐/증폭", | |
"소리 지향성", "음향 왜곡", "비트 생성", "하모닉스 생성", "주파수 변조", | |
"음향 충격파", "음향 필터링" | |
], | |
"생물학적 변화": [ | |
"생장/위축", "세포 분열/사멸", "생물 발광", "신진대사 변화", "면역 반응", | |
"호르몬 분비", "신경 반응", "유전적 발현", "적응/진화", "생체리듬 변화", | |
"재생/치유", "노화/성숙", "생체 모방 변화", "바이오필름 형성", "생물학적 분해", | |
"효소 활성화/비활성화", "생물학적 신호 전달", "스트레스 반응", "체온 조절", | |
"생물학적 시계 변화", "세포외 기질 변화", "생체 역학적 반응", "세포 운동성", | |
"세포 극성 변화", "영양 상태 변화" | |
], | |
"환경 상호작용": [ | |
"온도 반응", "습도 반응", "기압 반응", "중력 반응", "자기장 반응", | |
"빛 반응", "소리 반응", "화학 물질 감지", "기계적 자극 감지", "전기 자극 반응", | |
"방사선 반응", "진동 감지", "pH 반응", "용매 반응", "기체 교환", | |
"환경 오염 반응", "날씨 반응", "계절 변화 반응", "일주기 반응", "생태계 상호작용", | |
"공생/경쟁 반응", "포식/피식 관계", "군집 형성", "영역 설정", "이주/정착 패턴" | |
], | |
"센서 기능": [ | |
"시각 센서/감지", "청각 센서/감지", "촉각 센서/감지", "미각 센서/감지", "후각 센서/감지", | |
"온도 센서/감지", "습도 센서/감지", "압력 센서/감지", "가속도 센서/감지", "회전 센서/감지", | |
"근접 센서/감지", "위치 센서/감지", "운동 센서/감지", "가스 센서/감지", "적외선 센서/감지", | |
"자외선 센서/감지", "방사선 센서/감지", "자기장 센서/감지", "전기장 센서/감지", "화학물질 센서/감지", | |
"생체신호 센서/감지", "진동 센서/감지", "소음 센서/감지", "빛 세기 센서/감지", "빛 파장 센서/감지", | |
"기울기 센서/감지", "pH 센서/감지", "전류 센서/감지", "전압 센서/감지", "이미지 센서/감지", | |
"거리 센서/감지", "깊이 센서/감지", "중력 센서/감지", "속도 센서/감지", "흐름 센서/감지", | |
"수위 센서/감지", "탁도 센서/감지", "염도 센서/감지", "금속 감지", "압전 센서/감지", | |
"광전 센서/감지", "열전대 센서/감지", "홀 효과 센서/감지", "초음파 센서/감지", "레이더 센서/감지", | |
"라이다 센서/감지", "터치 센서/감지", "제스처 센서/감지", "심박 센서/감지", "혈압 센서/감지" | |
] | |
} | |
############################################################################## | |
# Gemini API 호출 함수 | |
############################################################################## | |
def query_gemini_api(prompt): | |
try: | |
model = genai.GenerativeModel('gemini-2.0-flash-thinking-exp-01-21') | |
response = model.generate_content(prompt) | |
try: | |
if hasattr(response, 'text'): | |
return response.text | |
if hasattr(response, 'candidates') and response.candidates: | |
candidate = response.candidates[0] | |
if hasattr(candidate, 'content'): | |
content = candidate.content | |
if hasattr(content, 'parts') and content.parts: | |
if len(content.parts) > 0: | |
return content.parts[0].text | |
if hasattr(response, 'parts') and response.parts: | |
if len(response.parts) > 0: | |
return response.parts[0].text | |
return "Unable to generate a response. API response structure is different than expected." | |
except Exception as inner_e: | |
logger.error(f"Error processing response: {inner_e}") | |
return f"An error occurred while processing the response: {str(inner_e)}" | |
except Exception as e: | |
logger.error(f"Error calling Gemini API: {e}") | |
if "API key not valid" in str(e): | |
return "API key is not valid. Please check your GEMINI_API_KEY environment variable." | |
return f"An error occurred while calling the API: {str(e)}" | |
############################################################################## | |
# 설명 확장 함수 (LLM 이용) | |
############################################################################## | |
def enhance_with_llm(base_description, obj_name, category): | |
prompt = f""" | |
다음은 '{obj_name}'의 '{category}' 관련 간단한 설명입니다: | |
"{base_description}" | |
위 내용을 보다 구체화하여, | |
1) 창의적인 모델/컨셉/형상의 변화에 대한 이해, | |
2) 혁신 포인트와 기능성 등을 중심으로 | |
3~4문장의 아이디어로 확장해 주세요. | |
""" | |
return query_gemini_api(prompt) | |
############################################################################## | |
# 각 객체수(1, 2, 3)에 따른 변형 아이디어 생성 | |
############################################################################## | |
def generate_single_object_transformation_for_category(obj, selected_category): | |
transformations = physical_transformation_categories.get(selected_category) | |
if not transformations: | |
return {} | |
transformation = choose_alternative(random.choice(transformations)) | |
base_description = f"{obj}이(가) {transformation} 현상을 보인다" | |
return {selected_category: {"base": base_description, "enhanced": None}} | |
def generate_two_objects_interaction_for_category(obj1, obj2, selected_category): | |
transformations = physical_transformation_categories.get(selected_category) | |
if not transformations: | |
return {} | |
transformation = choose_alternative(random.choice(transformations)) | |
template = random.choice([ | |
"{obj1}이(가) {obj2}에 결합하여 {change}가 발생했다", | |
"{obj1}과(와) {obj2}이(가) 충돌하면서 {change}가 일어났다" | |
]) | |
base_description = template.format(obj1=obj1, obj2=obj2, change=transformation) | |
return {selected_category: {"base": base_description, "enhanced": None}} | |
def generate_three_objects_interaction_for_category(obj1, obj2, obj3, selected_category): | |
transformations = physical_transformation_categories.get(selected_category) | |
if not transformations: | |
return {} | |
transformation = choose_alternative(random.choice(transformations)) | |
template = random.choice([ | |
"{obj1}, {obj2}, {obj3}이(가) 삼각형 구조로 결합하여 {change}가 발생했다", | |
"{obj1}이(가) {obj2}와(과) {obj3} 사이에서 매개체 역할을 하며 {change}를 촉진했다" | |
]) | |
base_description = template.format(obj1=obj1, obj2=obj2, obj3=obj3, change=transformation) | |
return {selected_category: {"base": base_description, "enhanced": None}} | |
############################################################################## | |
# 생성된 기본 설명을 LLM을 통해 확장 (각 카테고리별) | |
############################################################################## | |
def enhance_descriptions(results, objects): | |
obj_name = " 및 ".join([obj for obj in objects if obj]) | |
for category, result in results.items(): | |
result["enhanced"] = enhance_with_llm(result["base"], obj_name, category) | |
return results | |
############################################################################## | |
# 사용자 입력(최대 3개 키워드) + 선택 카테고리 → 변화 아이디어 생성 | |
############################################################################## | |
def generate_transformations(text1, text2, text3, selected_category): | |
if text2 and text3: | |
results = generate_three_objects_interaction_for_category(text1, text2, text3, selected_category) | |
objects = [text1, text2, text3] | |
elif text2: | |
results = generate_two_objects_interaction_for_category(text1, text2, selected_category) | |
objects = [text1, text2] | |
else: | |
results = generate_single_object_transformation_for_category(text1, selected_category) | |
objects = [text1] | |
return enhance_descriptions(results, objects) | |
############################################################################## | |
# 결과 포맷팅 | |
############################################################################## | |
def format_results(results): | |
formatted = "" | |
for category, result in results.items(): | |
formatted += f"## {category}\n**기본 아이디어**: {result['base']}\n\n**확장된 아이디어**: {result['enhanced']}\n\n---\n\n" | |
return formatted | |
############################################################################## | |
# Gradio UI에서 호출될 함수 (텍스트 아이디어만 생성) | |
############################################################################## | |
def process_inputs(text1, text2, text3, selected_category, progress=gr.Progress()): | |
text1 = text1.strip() if text1 else None | |
text2 = text2.strip() if text2 else None | |
text3 = text3.strip() if text3 else None | |
if not text1: | |
return "오류: 최소 하나의 키워드를 입력해주세요." | |
progress(0.05, desc="아이디어 생성 준비 중...") | |
time.sleep(0.3) | |
progress(0.1, desc="창의적인 아이디어 생성 시작...") | |
# 카테고리에 해당하는 아이디어 생성 | |
results = generate_transformations(text1, text2, text3, selected_category) | |
progress(0.8, desc="결과 포맷팅 중...") | |
formatted = format_results(results) | |
progress(1.0, desc="완료!") | |
return formatted | |
############################################################################## | |
# 아이디어와 이미지를 함께 생성하는 최종 함수 | |
############################################################################## | |
def process_all(text1, text2, text3, selected_category, progress=gr.Progress()): | |
idea_result = process_inputs(text1, text2, text3, selected_category, progress) | |
image_result = generate_design_image( | |
idea_result, | |
seed=42, | |
randomize_seed=True, | |
width=1024, | |
height=1024, | |
num_inference_steps=4 | |
) | |
return idea_result, image_result | |
############################################################################## | |
# API 키 경고 메시지 | |
############################################################################## | |
def get_warning_message(): | |
if not GEMINI_API_KEY: | |
return "⚠️ 환경 변수 GEMINI_API_KEY가 설정되지 않았습니다. Gemini API 키를 설정하세요." | |
return "" | |
############################################################################## | |
# Gradio UI | |
############################################################################## | |
with gr.Blocks( | |
title="키워드 기반 창의적 변화 아이디어 및 디자인 생성기", | |
theme=gr.themes.Soft(primary_hue="teal", secondary_hue="slate", neutral_hue="neutral") | |
) as demo: | |
gr.HTML(""" | |
<style> | |
body { | |
background: linear-gradient(135deg, #e0eafc, #cfdef3); | |
font-family: 'Arial', sans-serif; | |
} | |
.gradio-container { | |
padding: 20px; | |
} | |
h1, h2 { | |
text-align: center; | |
} | |
h1 { | |
color: #333; | |
} | |
h2 { | |
color: #555; | |
} | |
.output { | |
background-color: #ffffff; | |
padding: 15px; | |
border-radius: 8px; | |
} | |
.gr-button { | |
background-color: #4CAF50; | |
color: white; | |
border: none; | |
border-radius: 4px; | |
padding: 8px 16px; | |
} | |
.progress-message { | |
color: #2196F3; | |
font-weight: bold; | |
margin-top: 10px; | |
} | |
</style> | |
""") | |
gr.Markdown("# 🚀 키워드 기반 창의적 변화 아이디어 및 디자인 생성기") | |
gr.Markdown("입력한 **키워드**(최대 3개)와 **카테고리**를 바탕으로, 창의적인 모델/컨셉/형상 변화 아이디어를 생성하고, 해당 확장 아이디어를 프롬프트로 하여 디자인 이미지를 생성합니다.") | |
warning = gr.Markdown(get_warning_message()) | |
with gr.Row(): | |
with gr.Column(scale=1): | |
text_input1 = gr.Textbox(label="키워드 1 (필수)", placeholder="예: 스마트폰") | |
text_input2 = gr.Textbox(label="키워드 2 (선택)", placeholder="예: 인공지능") | |
text_input3 = gr.Textbox(label="키워드 3 (선택)", placeholder="예: 헬스케어") | |
category_dropdown = gr.Dropdown( | |
label="카테고리 선택", | |
choices=list(physical_transformation_categories.keys()), | |
value=list(physical_transformation_categories.keys())[0], | |
info="출력할 카테고리를 선택하세요." | |
) | |
status_msg = gr.Markdown("💡 '아이디어 생성하기' 버튼을 클릭하면 선택한 카테고리에 해당하는 아이디어와 디자인 이미지가 생성됩니다.") | |
processing_indicator = gr.HTML(""" | |
<div style="display: flex; justify-content: center; align-items: center; margin: 10px 0;"> | |
<div style="border: 5px solid #f3f3f3; border-top: 5px solid #3498db; border-radius: 50%; width: 30px; height: 30px; animation: spin 2s linear infinite;"></div> | |
<p style="margin-left: 10px; font-weight: bold; color: #3498db;">처리 중입니다...</p> | |
</div> | |
<style> | |
@keyframes spin { | |
0% { transform: rotate(0deg); } | |
100% { transform: rotate(360deg); } | |
} | |
</style> | |
""", visible=False) | |
submit_button = gr.Button("아이디어 생성하기", variant="primary") | |
with gr.Column(scale=2): | |
idea_output = gr.Markdown(label="아이디어 결과") | |
generated_image = gr.Image(label="생성된 디자인 이미지", type="pil") | |
# 예제 | |
gr.Examples( | |
examples=[ | |
["스마트폰", "", "", list(physical_transformation_categories.keys())[0]], | |
["자동차", "", "", list(physical_transformation_categories.keys())[0]], | |
["자동차", "인공지능", "", list(physical_transformation_categories.keys())[0]], | |
["드론", "인공지능", "", list(physical_transformation_categories.keys())[0]], | |
["운동화", "웨어러블", "건강", list(physical_transformation_categories.keys())[0]], | |
], | |
inputs=[text_input1, text_input2, text_input3, category_dropdown], | |
) | |
# 처리중 아이콘 보이기 | |
def show_processing_indicator(): | |
return gr.update(visible=True) | |
# 처리중 아이콘 숨기기 | |
def hide_processing_indicator(): | |
return gr.update(visible=False) | |
# 버튼 클릭 시 처리 로직 | |
submit_button.click( | |
fn=show_processing_indicator, | |
inputs=None, | |
outputs=processing_indicator | |
).then( | |
fn=process_all, | |
inputs=[text_input1, text_input2, text_input3, category_dropdown], | |
outputs=[idea_output, generated_image] | |
).then( | |
fn=hide_processing_indicator, | |
inputs=None, | |
outputs=processing_indicator | |
) | |
# 메인 실행 | |
if __name__ == "__main__": | |
demo.launch(debug=True) | |