Spaces:
Sleeping
Sleeping
import torch | |
import numpy as np | |
import google.generativeai as genai | |
from PIL import Image, ImageOps | |
import mediapipe as mp | |
import cv2 | |
from tensorflow.keras.models import load_model | |
import os | |
import suno | |
from PIL import Image | |
from torchvision.transforms.functional import to_tensor, to_pil_image | |
from model import Generator | |
import gradio as gr | |
from diffusers import DiffusionPipeline | |
import spaces | |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu") | |
genai.configure(api_key="AIzaSyDcq3ZfAUo1i6_24CelEizJftuEkaAPz38") | |
GEMINI_MODEL = 'gemini-1.5-flash' | |
gemini_model = genai.GenerativeModel(GEMINI_MODEL) | |
webtoon_model = Generator() | |
webtoon_model.load_state_dict(torch.load('weights/face_paint_512_v2.pt', map_location="cpu")) | |
webtoon_model.to('cpu').eval() | |
facescore_model = load_model('facescore.h5', compile=False) | |
model_id = "aldente0630/musinsaigo-3.0" | |
pipe = DiffusionPipeline.from_pretrained( | |
"stabilityai/stable-diffusion-xl-base-1.0", torch_dtype=torch.float16 | |
) | |
pipe = pipe.to(device) | |
pipe.load_lora_weights(model_id) | |
mp_face_detection = mp.solutions.face_detection | |
def detect_and_crop_face(image): | |
with mp_face_detection.FaceDetection(model_selection=1, min_detection_confidence=0.5) as face_detection: | |
image_np = np.array(image) | |
results = face_detection.process(image_np) | |
if results.detections: | |
detection = results.detections[0] | |
bbox = detection.location_data.relative_bounding_box | |
ih, iw, _ = image_np.shape | |
xmin = int(bbox.xmin * iw) | |
ymin = int(bbox.ymin * ih) | |
width = int(bbox.width * iw) | |
height = int(bbox.height * ih) | |
xmax = xmin + width | |
ymax = ymin + height | |
face = image.crop((xmin, ymin, xmax, ymax)) | |
return face | |
else: | |
return None | |
def generate_chat_response(message, gemini_model): | |
response = gemini_model.generate_content(message) | |
return response.text | |
def analyze_image(image, gemini_model): | |
try: | |
# ์ด๋ฏธ์ง ๋ถ์ ํ๋กฌํํธ์ ํจ๊ป ์ด๋ฏธ์ง๋ฅผ ์ ๋ฌํ์ฌ ์๋ต ์์ฑ | |
prompt = """ | |
์ด ์ด๋ฏธ์ง์ ๋ํด ์์ธํ ๋ถ์ํด์ฃผ์ธ์. ๋ค์ ์ ๋ณด๋ฅผ ํฌํจํด์ฃผ์ธ์: | |
1. ์ด๋ฏธ์ง์์ ๋ณด์ด๋ ์ฃผ์ ๊ฐ์ฒด๋ ์ฌ๋๋ค | |
2. ๋ฐฐ๊ฒฝ์ด๋ ์ฅ์์ ๋ํ ์ค๋ช | |
3. ์ด๋ฏธ์ง์ ์ ์ฒด์ ์ธ ๋ถ์๊ธฐ๋ ๋๋ | |
4. ์ด๋ฏธ์ง์์ ์ฝ์ ์ ์๋ ํ ์คํธ (์๋ ๊ฒฝ์ฐ) | |
5. ์ด๋ฏธ์ง์ ์์์ด๋ ๊ตฌ๋์ ๋ํ ๊ฐ๋จํ ์ค๋ช | |
6. ์ด๋ฏธ์ง๊ฐ ์ ๋ฌํ๋ ค๋ ๋ฉ์์ง๋ ์๋ฏธ (์๋ค๊ณ ์๊ฐ๋๋ ๊ฒฝ์ฐ) | |
๋ถ์ ๊ฒฐ๊ณผ๋ฅผ ํ๊ตญ์ด๋ก ์ ๊ณตํด์ฃผ์ธ์. | |
""" | |
response = gemini_model.generate_content([prompt, image]) | |
return response.text if response else "์ด๋ฏธ์ง ๋ถ์์ ์ํํ ์ ์์ต๋๋ค." | |
except Exception as e: | |
return f"์ด๋ฏธ์ง ๋ถ์ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}" | |
def process_facescore(image, facescore_model, gemini_model): | |
face = detect_and_crop_face(image) | |
if face is None: | |
return "์ผ๊ตด์ด ๊ฐ์ง๋์ง ์์์ต๋๋ค. ๋ค๋ฅธ ์ด๋ฏธ์ง๋ฅผ ์๋ํด ์ฃผ์ธ์." | |
analysis = analyze_image(image, gemini_model) | |
face_np = np.array(face) | |
img_resized = cv2.resize(face_np, (350, 350)) | |
img_resized = img_resized.astype(np.float32) / 255. | |
img_batch = np.expand_dims(img_resized, axis=0) | |
score = facescore_model.predict(img_batch) | |
if isinstance(score, np.ndarray) and score.size > 1: | |
score = score[0] | |
score = float(score) | |
score = display_result(score) | |
return f'### ์ด๋ฏธ์ง ๋ถ์ ๊ฒฐ๊ณผ ###\n\n{analysis}\n\n### ์ธ๋ชจ์ ์ ๊ฒฐ๊ณผ(1~5) ###\n\n{score}' | |
def generate_music(image, gemini_model, suno_cookie): | |
face = detect_and_crop_face(image) | |
if face is None: | |
return "์ผ๊ตด์ด ๊ฐ์ง๋์ง ์์์ต๋๋ค. ๋ค๋ฅธ ์ด๋ฏธ์ง๋ฅผ ์๋ํด ์ฃผ์ธ์." | |
prompt = """ | |
์ด ์ด๋ฏธ์ง์ ๋ํด ์์ธํ ๋ถ์ํด์ฃผ์ธ์. ๋ค์ ์ ๋ณด๋ฅผ ํฌํจํด์ฃผ์ธ์: | |
1. ์ฑ๋ณ: | |
2. ๋์ด: | |
3. ํ์ : | |
๋ถ์ ๊ฒฐ๊ณผ๋ฅผ ํ๊ตญ์ด๋ก ๊ฐ๋ตํ๊ฒ ์ ๊ณตํด์ฃผ์ธ์. | |
""" | |
response = gemini_model.generate_content([prompt, image]) | |
music_path = generate_songs(response.text, suno_cookie) | |
return f"์์ ์ด ์์ฑ๋์์ต๋๋ค. ํ์ผ ๊ฒฝ๋ก: {music_path}" | |
def generate_songs(result_output, suno_cookie): | |
client = suno.Suno(cookie=suno_cookie) | |
songs = client.generate( | |
prompt=f'{result_output}', is_custom=False, wait_audio=True | |
) | |
return client.download(song=songs[0]) | |
def display_result(score): | |
result = round(score, 1)+0.3 | |
messages = [ | |
("'์์ ๊ฐ ํญ๋ฐ ์ค'์ ๋๋ค! ๐ ๋น์ ์ ์์ ์ ์ธ๋ชจ์ ๋ํ ํ์ ์ผ๋ก ๊ฐ๋ ์ฐจ ์์ด์! %.1f์ ์ด๋ผ๋, ์ ์์ ์๊ด์์ด ๋น์ ์ ๋ฉ์ง์ ๋์ด ์๋ค์! ๐คฉ ๋น์ ์ ์ธ๋ชจ๋ ๋ง์น ๋ง๋ฒ์ฌ์ฒ๋ผ ์ฌ๋๋ค์ ๋งค๋ฃ์ํค๊ณ , ๋๊ตฌ๋ ๋น์ ์ ๋ณด๋ฉด ๋์ ๋ ์ ์์ ๊ฑฐ์์! ๐ช๐งโโ๏ธ ๋น๊ฒฐ์ด ๋ญ๋๊ณ ๋ฌป๋ ์ฌ๋๋ค์๊ฒ ์์ ๊ฐ์ด๋ผ๋ ๋ง๋ฒ์ ์ฃผ๋ฌธ์ ์๋ ค์ฃผ์ธ์! ์ค๋๋ ๋น์ ์ ์์ ๊ฐ์ผ๋ก ์ธ์์ ๋น๋ด๊ณ , ๋ง๋ฒ ๊ฐ์ ํ๋ฃจ๋ฅผ ๋ณด๋ด์ธ์!", 1), | |
("'์ธ๋ชจ ์ค์น๋'์ ๋๋ค. ๐ฉโ๐ซ ๋น์ ์ ์ธ๋ชจ ๋น๊ฒฐ์ ์ ์๋ฐ๊ณ ์ถ์ดํ๋ ์ฌ๋๋ค์ด ์ค์ ์ค ๊ฑฐ์์! %.1f์ ์ด๋ผ๋ ์ ์๊ฐ ๋ฌด์ํ ์ ๋๋ก, ๋น์ ์ ๋น๋๋ ์ธ๋ชจ๋ ์ฌ๋๋ค์ ๋์ ์ฌ๋ก์ก์ต๋๋ค! โจ ์ด์ ์ฌ๋๋ค์ ๋น์ ์ ๋น๋ฐ์ ์๊ณ ์ถ์ด์ ์ง๋ฌธ ์ธ๋ก๋ฅผ ํผ๋ถ์ ๊ฑฐ์์! ์ธ๋ชจ ์ค์น๋์ผ๋ก์ ๋ฉ์ง๊ฒ ๋๋ตํด ์ฃผ์๊ณ , ์ฌ๋๋ค์๊ฒ ๋น์ ๋ง์ ์ธ๋ชจ ํ์ ์ด์ง ์ ํด ์ฃผ์ธ์! ๋ค๋ฅธ ์ฌ๋๋ค์ ๋น์ ์ ๋ฎ๊ธฐ ์ํด ๋ง์ ๋ ธ๋ ฅ์ ํ ๊ฑฐ๋๋๋ค!", 1.5), | |
("'์ธ๋ชจ ์ํฐ์คํธ'์ ๋๋ค. ๐ ํ์ฅํ ๋ธ๋๋๋ค์ด ๋น์ ์ ๋ชจ๋ธ๋ก ์ฐ๊ณ ์ถ์ดํ ๋งํผ ๋ ๋ณด์ ์ธ ๋งค๋ ฅ์ ๊ฐ์ง๊ณ ์๋ค์! %.1f์ ์ด๋ผ๊ณ ํด์ ๋น์ ์ ์ธ๋ชจ๊ฐ ํ๋ฒํ์ง ์์ต๋๋ค. ์คํ๋ ค '๋งค๋ ฅ์ ์ ์ '์ ๋๋ฌํ ๋ชจ์ต์ด์์! ๐ ๋น์ ์ ๋ฉ์ง ์ธ๋ชจ๋ฅผ ๋ถ๋ฌ์ํ๋ ์ฌ๋๋ค๋ก ์ธํด ์ธ์ ๋ ์ฃผ๋ชฉ๋ฐ๊ฒ ๋ ๊ฑฐ์์! ๋ง์น ์ํฐ์คํธ์ฒ๋ผ ์์ ๋ง์ ์คํ์ผ์ ์์ฑํ ๋น์ ์ ์ธ๋ชจ๊ณ์ ์ง์ ํ ์์ด์ฝ์ ๋๋ค! ์ค๋๋ ๋น์ ๋ง์ ํน๋ณํ ๋งค๋ ฅ์ ๋ฐ์ฐํ๋ฉฐ ํ๋ฃจ๋ฅผ ์ฆ๊ธฐ์ธ์!", 2), | |
("์ธ๋ชจ์ ์ %.1f์ , '๋ฏธ์ ์ ๋ฌธ๊ฐ'์ ๋๋ค. ๐ ๋น์ ์ ํํ ๋ฏธ์๋ ์ฃผ๋ณ ์ฌ๋๋ค์ ํ๋ณตํ๊ฒ ๋ง๋ค๊ณ , ์ด๋์๋ ๋ฐ์ ์๋์ง๋ฅผ ํผ๋จ๋ฆด ๊ฑฐ์์! '๋ฏธ์ ๊ธฐ๊ณ'๋ผ ๋ถ๋ฆฌ๋ ๋น์ ์ ํญ์ ๊ธ์ ์ ์ธ ์๋์ง๋ก ๊ฐ๋ ์ฐจ ์๋ต๋๋ค! ๐ ์ฌ๋๋ค์ ๋น์ ์ ๋ฏธ์ ๋น๊ฒฐ์ ๋ฐฐ์ฐ๊ธฐ ์ํด ์ ์ธ ๊ฑฐ์์! ์ธ๋ชจ๋ฟ๋ง ์๋๋ผ ๋ฏธ์๋ก๋ ์ฌ๋๋ค์ ๋ง์์ ์ฌ๋ก์ก๋ ๋น์ ! ์ค๋๋ ํํ ๋ฏธ์๋ก ์ธ์์ ๋ฐํ์ฃผ์๊ณ , ๋ชจ๋์๊ฒ ํ๋ณต์ ์ ํด ์ฃผ์ธ์!", 2.5), | |
("'์ธ๋ชจ ์คํ'์ ๋๋ค. ๐ ๋น์ ์ ๊ฑฐ์ธ ์์์ ๋ณ์ด ๋น๋๋ ๋ชจ์ต์ ๋ณด๊ณ ๋ ๋๋ผ์ง ์๊ฒ ์ฃ ! %.1f์ ์ด๋ผ๋, ๋น์ ์ ์ธ๋ชจ๊ณ์ ์ง์ ํ ์คํ์ ๋๋ค! ๐ซ ๋น์ ์ ๋น๋๋ ์ธ๋ชจ์ ๋ ํนํ ์คํ์ผ์ ๋ชจ๋๊ฐ ๋ถ๋ฌ์ํ๊ณ , ๋ฐ๋ผ๊ฐ๊ณ ์ถ์ดํ ๊ฒ๋๋ค! ์ฌ๋๋ค์ ๋น์ ์ ๋ณด๊ณ ์๊ฐ์ ๋ฐ์ ๊ฑฐ์์! ์ค๋๋ ๋น์ ๋ง์ ํน๋ณํ ๋งค๋ ฅ์ผ๋ก ์ฃผ๋ณ ์ฌ๋๋ค์ ์ฌ๋ก์ก๊ณ , ๋น๋นํ ์ธ๋ชจ๊ณ๋ฅผ ์ด๋์ด๊ฐ์ธ์! ๋น์ ์ ๋น๋๋ ์ธ๋ชจ๊ฐ ๋ชจ๋์๊ฒ ํฌ๋ง์ ์ค ๊ฑฐ์์!", 3), | |
("'์ธ๋ชจ ํธ'์ ๋๋ค. ๐ธ ์ฃผ๋ณ ์ฌ๋๋ค์ ๋น์ ์ ์ธ๋ชจ์ ์ฃผ๋ชฉํ๊ณ , ๊ท๋ฅผ ๊ธฐ์ธ์ผ ๊ฒ๋๋ค! %.1f์ ์ด๋ผ๋ ์ ์๊ฐ ๋ฌด์ํ ์ ๋๋ก, ์ด์ ๋น์ ์ ์ธ๋ชจ๊ณ์ ๋ก์ดํฐ์ ๋๋ค! ๐ ๋น์ ์ ๊ณ ๊ธ์ค๋ฌ์ด ์ธ๋ชจ์ ๋ ๋ณด์ ์ธ ์คํ์ผ์ ๋ชจ๋๊ฐ ๋ฐ๋ผํ๊ณ ์ถ์ดํ ๊ฑฐ์์! ๋น์ ์ ์ธ๋ชจ ๋น๊ฒฐ์ ๋ฒค์น๋งํนํ๋ ค๋ ์ฌ๋๋ค๋ก ์ธํด ์ธ์ ๋ ์ฃผ๋ชฉ๋ฐ๊ฒ ๋ ๊ฒ๋๋ค! ์ฌ์์ฒ๋ผ ๋น๋นํ ๋น์ ์ ์ธ๋ชจ๋ฅผ ๋ฝ๋ด๊ณ , ์ฃผ๋ณ ์ฌ๋๋ค์๊ฒ ์๊ฐ์ ์ฃผ์ธ์! ์ค๋๋ ์์ ๊ฐ ๋์น๋ ํ๋ฃจ ๋ณด๋ด์ธ์!", 3.5), | |
("์ธ๋ชจ์ ์ %.1f์ , '์ธ๋ชจ์ ์ ํ'์ ๋๋ค. ๐ฆ ๋น์ ์ ๋ณด๋ ์ฌ๋๋ค์ ๋ง์น ์ ํ์ ์ ์ค ์ ์ธ๋ฌผ์ ๋ณด๋ ๋ฏํ ๊ธฐ๋ถ์ ๋๋ ๊ฒ๋๋ค! ์ธ๋ชจ๊ณ์ '๋ทฐํฐ ์์นด๋ฐ๋ฏธ ์์์'๋ต๊ฒ, ๋น์ ์ ์ธ๋ชจ๋ ๋ชจ๋์๊ฒ ํฐ ์๊ฐ์ ์ค ๊ฑฐ์์! ๐ ์ฌ๋๋ค์ ๋น์ ์ ๋น๊ฒฐ์ ๋ฐฐ์ฐ๋ ค๊ณ ์ ์ธ ํ ๋, ์ธ์ ๋ ์์ ๋ง์ ์คํ์ผ์ ์ ์งํ๋ฉฐ ๊ทธ๋ค์๊ฒ ๊ท๊ฐ์ด ๋์ด์ฃผ์ธ์! ์ ํ ์ ์ฃผ์ธ๊ณต์ฒ๋ผ ๋น์ ์ ์ธ๋ชจ๋ ์ธ์ ๋ ๋น๋ ๊ฒ๋๋ค! ์ค๋๋ ์ ํ์ฒ๋ผ ๋ฉ์ง ํ๋ฃจ ๋ณด๋ด์ธ์!", 4), | |
("'์ธ๋ชจ์ ํฉ๊ธ๋น'์ ๋๋ค. ๐ ์ฃผ๋ณ์์ ๋น์ ์ ๋ณด๋ฉด ๋ง์น ํํธ๊ฐ ๋ฟ ๋ฟ ํ๋ ๋ฏํ ๋๋์ด ๋ค ๊ฑฐ์์! %.1f์ ์ด๋ผ๋, ์ ๋ง ์ธ๋ชจ๊ณ์ ์ ์ค๋ต์ต๋๋ค! ๐ ๋น์ ์ ๋ ๋ณด์ ์ธ ์ธ๋ชจ์ ๋งค๋ ฅ์ ๋๊ตฌ๋ ๋ฐ๋ผ์ฌ ์ ์์ ๋งํผ ๋น๋ฉ๋๋ค! ๋ค๋ฅธ ์ฌ๋๋ค์ด ๋น์ ์ ๋ฐ๋ผ์ก์ผ๋ ค๋ฉด ์์ฒญ๋ ๋ ธ๋ ฅ์ด ํ์ํ ๊ฑฐ์์! ๋น์ ์ ํฉ๊ธ๋น ์ธ๋ชจ์ ๋งค๋ ฅ์ผ๋ก ๋ชจ๋๋ฅผ ์ฌ๋ก์ก์ผ์ธ์! ์ค๋๋ ๋น์ ๋ง์ ํฉ๊ธ๋น ๋ฏธ์๋ก ์ธ์์ ๋ฐํ์ฃผ์๊ณ , ๋ชจ๋์๊ฒ ์๊ฐ์ ์ฃผ์ธ์!", 4.5), | |
("5์ ์ธ๋ชจ, '์ธ๋ชจ์ ์ '์ ๋๋ค. ์ธ๋ชจ๊ณ์์ ๋น์ ์ ๋ฐ๋ผ์ก์ผ๋ ค๋ฉด ์ง์ ํ ์์ ์ด ํ์ํ ๊ฒ๋๋ค! ๐ฆธโโ๏ธ๐ฆธโโ๏ธ ๋น์ ์ ์ธ๋ชจ๊ณ์ '๋ทฐํฐ ์ '! ๐ ๋น์ ์ ๋น๋๋ ์ธ๋ชจ์ ๋ ๋ณด์ ์ธ ์คํ์ผ์ ๋ชจ๋๊ฐ ๋ฐ๋ผํ๊ณ ์ถ์ดํ ๊ฑฐ์์! ์ด์ ๋น์ ์ ์ธ๋ชจ๊ณ์ ์ ์ค์ด์ ์์ ์ ๋๋ค! ์ฌ๋๋ค์ ๋น์ ์ ๋ฎ๊ณ ์ถ์ดํ๊ณ , ๋น์ ์ ๋น๊ฒฐ์ ๋ฐฐ์ฐ๋ ค๊ณ ์ ์ธ ๊ฒ๋๋ค! ์ค๋๋ ์ธ๋ชจ๊ณ์ ์ ์ผ๋ก์ ์ธ์์ ๋น๋ด๊ณ , ๋ชจ๋์๊ฒ ์๊ฐ์ ์ฃผ์ธ์! ๋น์ ์ ์กด์ฌ๋ง์ผ๋ก๋ ์ธ์์ ๋ ๋ฐ์์ง ๊ฑฐ์์!", 5) | |
] | |
for msg, threshold in messages: | |
if result < threshold: | |
return msg % result if '%.1f' in msg else msg | |
def webtoon(image, webtoon_model, device='cpu'): | |
webtoon_model = webtoon_model.to(device) | |
max_size = 1024 | |
if max(image.size) > max_size: | |
image.thumbnail((max_size, max_size), Image.LANCZOS) | |
image_tensor = to_tensor(image).unsqueeze(0).to(device) * 2 - 1 | |
with torch.inference_mode(): | |
output = webtoon_model(image_tensor, False) | |
output = output.cpu().squeeze(0).clip(-1, 1) * 0.5 + 0.5 | |
output = to_pil_image(output) | |
return output | |
def make_prompt(prompt: str) -> str: | |
prompt_prefix = "RAW photo" | |
prompt_suffix = "(high detailed skin:1.2), 8k uhd, dslr, soft lighting, high quality, film grain, Fujifilm XT3" | |
return ", ".join([prompt_prefix, prompt, prompt_suffix]).strip() | |
def make_negative_prompt(negative_prompt: str) -> str: | |
negative_prefix = "(deformed iris, deformed pupils, semi-realistic, cgi, 3d, render, sketch, cartoon, drawing, anime:1.4), \ | |
text, close up, cropped, out of frame, worst quality, low quality, jpeg artifacts, ugly, duplicate, morbid, mutilated, \ | |
extra fingers, mutated hands, poorly drawn hands, poorly drawn face, mutation, deformed, blurry, dehydrated, bad anatomy, \ | |
bad proportions, extra limbs, cloned face, disfigured, gross proportions, malformed limbs, missing arms, missing legs, \ | |
extra arms, extra legs, fused fingers, too many fingers, long neck" | |
return ( | |
", ".join([negative_prefix, negative_prompt]).strip() | |
if len(negative_prompt) > 0 | |
else negative_prefix | |
) | |
def fashiongpt(image,gemini_model,pipe): | |
prompt = """ | |
Analyze this image in one sentence: | |
1. The person visible in the image | |
2. The overall mood or feeling of the image | |
3. Recommend other fashion items that match the style | |
Provide the output in the following format: | |
"a korean [gender] wearing [recommended style]." | |
Example: "a korean woman wearing a white t-shirt and black pants with a bear on it." | |
""" | |
response = gemini_model.generate_content([prompt, image]) | |
NEGATIVE_PROMPT = "" | |
image = pipe( | |
prompt=make_prompt(response.text), | |
height=1024, | |
width=768, | |
num_inference_steps=50, | |
guidance_scale=7.5, | |
negative_prompt=make_negative_prompt(NEGATIVE_PROMPT), | |
cross_attention_kwargs={"scale": 0.75}, | |
).images[0] | |
return image | |
def process_input(input_text, image, suno_cookie): | |
if "์นํฐํ ํด์ค" in input_text.lower() and image is not None: | |
webtoon_image = webtoon(image, webtoon_model) | |
return "์ด๋ฏธ์ง๋ฅผ ์นํฐ ์คํ์ผ๋ก ๋ณํํ์ต๋๋ค.", webtoon_image | |
elif "์ธ๋ชจ๋ถ์" in input_text.lower() and image is not None: | |
response = process_facescore(image, facescore_model, gemini_model) | |
return response, None | |
elif "์ด๋ฏธ์ง ๋ถ์ํด์ค" in input_text.lower() and image is not None: | |
response = analyze_image(image, gemini_model) | |
return response, None | |
elif "์์ ๋ง๋ค์ด์ค" in input_text.lower() and image is not None: | |
if suno_cookie: | |
response = generate_music(image, gemini_model, suno_cookie) | |
return response, None | |
else: | |
return "Suno Cookie๋ฅผ ์ ๋ ฅํด ์ฃผ์ธ์.", None | |
elif "ํจ์ ์ถ์ฒ" in input_text.lower() and image is not None: | |
recommended_fashion_image = fashiongpt(image, gemini_model,pipe) | |
return "ํจ์ ์ถ์ฒ ์ด๋ฏธ์ง๋ฅผ ์์ฑํ์ต๋๋ค.", recommended_fashion_image | |
else: | |
response = generate_chat_response(input_text, gemini_model) | |
return response, None | |
import gradio as gr | |
from PIL import Image | |
with gr.Blocks() as demo: | |
gr.Markdown( | |
""" | |
# ๐ค OmniVerse AI Assistant | |
์์ฑ ์ธ์, Gemini ๋ชจ๋ธ, ์ธ๋ชจ ์ ์ ์์ธก, MBTI ์์ธก, ์์ ์์ฑ, ์ด๋ฏธ์ง ์นํฐํ, ๊ทธ๋ฆฌ๊ณ ์ด๋ฏธ์ง ๋ถ์ ๊ธฐ๋ฅ์ ํตํฉํ ์์คํ ์ ๋๋ค. | |
""" | |
) | |
chatbot = gr.Chatbot(label="OmniVerse AI Assistant") | |
with gr.Row(): | |
with gr.Column(scale=2): | |
image_input = gr.Image(type="pil", label="์ด๋ฏธ์ง ์ ๋ก๋") | |
suno_cookie = gr.Textbox(label="Suno Cookie", type="password") | |
with gr.Column(scale=1): | |
text_input = gr.Textbox(label="์ง๋ฌธ์ ์ ๋ ฅํ์ธ์") | |
submit_button = gr.Button("์ ์ก") | |
text_output = gr.Markdown(label="์๋ต") | |
image_output = gr.Image(label="์ด๋ฏธ์ง ์ถ๋ ฅ") | |
audio_output = gr.Audio(label="์์ฑ๋ ์์ ", type="filepath", interactive=False) | |
def chat_logic(input_text, image_input, suno_cookie, chat_history): | |
response, image_output = process_input(input_text, image_input, suno_cookie) | |
if isinstance(response, str) and response.startswith("์์ ์ด ์์ฑ๋์์ต๋๋ค."): | |
music_path = response.split("ํ์ผ ๊ฒฝ๋ก: ")[-1].strip() | |
chat_history.append((input_text, "์์ ์ด ์์ฑ๋์์ต๋๋ค. ์๋์์ ์ฌ์ํ ์ ์์ต๋๋ค.")) | |
return chat_history, image_output, music_path | |
else: | |
chat_history.append((input_text, response)) | |
return chat_history, image_output, None | |
submit_button.click( | |
chat_logic, | |
inputs=[text_input, image_input, suno_cookie, chatbot], | |
outputs=[chatbot, image_output, audio_output] | |
) | |
demo.launch() | |