Spaces:
Running
Running
import gradio as gr | |
import openai | |
import json | |
import os | |
import random | |
# OpenAI API ํด๋ผ์ด์ธํธ ์ค์ | |
openai.api_key = os.getenv("OPENAI_API_KEY") | |
# JSON ๋ฐ์ดํฐ ๋ก๋ | |
json_path = "back.json" # JSON ํ์ผ ๊ฒฝ๋ก | |
with open(json_path, "r", encoding="utf-8") as f: | |
data = json.load(f) | |
# JSON ๋ฐ์ดํฐ๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ์นดํ ๊ณ ๋ฆฌ์ ๋ฐฐ๊ฒฝ ๋ชฉ๋ก ์์ฑ | |
categories = {} | |
for item in data: | |
category = item.get("์นดํ ๊ณ ๋ฆฌ", "Unknown") | |
korean_translation = item.get("Korean Translation", "Unknown") | |
english_prompt = item.get("English Prompt", "Unknown") | |
if category not in categories: | |
categories[category] = [] | |
categories[category].append({"korean": korean_translation, "english": english_prompt}) | |
# LLM ํธ์ถ ํจ์ | |
def call_api(content, system_message, max_tokens, temperature, top_p): | |
response = openai.ChatCompletion.create( | |
model="gpt-4o-mini", | |
messages=[ | |
{"role": "system", "content": system_message}, | |
{"role": "user", "content": content}, | |
], | |
max_tokens=max_tokens, | |
temperature=temperature, | |
top_p=top_p, | |
) | |
return response.choices[0].message['content'].strip() | |
# ์ฌ๋ฌผ ๋ฐ ๋ฐฐ๊ฒฝ ๋ฒ์ญ ํจ์ | |
def translate_object(object_input): | |
system_message = "Translate the following object description from Korean to English. Keep it simple and concise." | |
return call_api( | |
content=object_input, | |
system_message=system_message, | |
max_tokens=10, | |
temperature=0.7, | |
top_p=1.0 | |
) | |
def translate_background(background_input): | |
system_message = "Translate the following background description from Korean to English. Keep it simple and concise." | |
return call_api( | |
content=background_input, | |
system_message=system_message, | |
max_tokens=20, | |
temperature=0.7, | |
top_p=1.0 | |
) | |
# ์ง์ ๋ฐฐ๊ฒฝ ์ ๋ ฅ ํญ์ ํ๋กฌํํธ ์์ฑ ํจ์ | |
def generate_prompt(object_input, background_input): | |
translated_object = translate_object(object_input) | |
translated_background = translate_background(background_input) | |
return f"advertisement photography, {translated_object} {translated_background}" | |
# 1์ฐจ/2์ฐจ ์นดํ ๊ณ ๋ฆฌ ์ ํ ํญ์ ํ๋กฌํํธ ์์ฑ ํจ์ | |
def generate_category_prompt(object_input, category, background): | |
translated_object = translate_object(object_input) | |
background_english = next( | |
(item["english"] for item in categories.get(category, []) if item["korean"] == background), | |
"" | |
) | |
if translated_object and background_english: | |
return f"advertisement photography, {translated_object} {background_english}." | |
else: | |
return "Error: Please ensure both object and background are provided correctly." | |
# Gradio ์ธํฐํ์ด์ค ์์ฑ | |
with gr.Blocks() as demo: | |
with gr.Tab("์ง์ ๋ฐฐ๊ฒฝ ์ ๋ ฅ"): | |
gr.Markdown("### ์ง์ ๋ฐฐ๊ฒฝ ์ ๋ ฅ์ ํตํ ํ๋กฌํํธ ์์ฑ") | |
with gr.Row(): | |
object_input = gr.Textbox(label="์ฌ๋ฌผ ์ ๋ ฅ", placeholder="์: ์ค๋ ์ง ์ฃผ์ค") | |
background_input = gr.Textbox(label="๋ฐฐ๊ฒฝ ์ ๋ ฅ", placeholder="์: ๊นจ๋ํ ์ฃผ๋ฐฉ") | |
output = gr.Textbox(label="์์ฑ๋ ์์ด ํ๋กฌํํธ") | |
generate_button = gr.Button("ํ๋กฌํํธ ์์ฑ") | |
generate_button.click( | |
fn=generate_prompt, | |
inputs=[object_input, background_input], | |
outputs=output | |
) | |
with gr.Tab("1์ฐจ/2์ฐจ ์นดํ ๊ณ ๋ฆฌ ์ ํ"): | |
gr.Markdown("### 1์ฐจ/2์ฐจ ์นดํ ๊ณ ๋ฆฌ ์ ํ์ ํตํ ํ๋กฌํํธ ์์ฑ") | |
with gr.Row(): | |
object_input = gr.Textbox(label="์ฌ๋ฌผ ์ ๋ ฅ", placeholder="์: ์ค๋ ์ง ์ฃผ์ค") | |
category_dropdown = gr.Dropdown( | |
label="1์ฐจ ์นดํ ๊ณ ๋ฆฌ ์ ํ", | |
choices=list(categories.keys()), | |
interactive=True | |
) | |
background_dropdown = gr.Dropdown( | |
label="2์ฐจ ๋ฐฐ๊ฒฝ ์ ํ", | |
choices=[], | |
interactive=True | |
) | |
def update_background_options(category): | |
if category in categories: | |
return gr.update(choices=[item["korean"] for item in categories[category]], value=None) | |
else: | |
return gr.update(choices=[], value=None) | |
category_dropdown.change( | |
fn=update_background_options, | |
inputs=category_dropdown, | |
outputs=background_dropdown | |
) | |
generate_button = gr.Button("ํ๋กฌํํธ ์์ฑ") | |
category_prompt_output = gr.Textbox(label="์์ฑ๋ ์์ด ํ๋กฌํํธ") | |
generate_button.click( | |
fn=generate_category_prompt, | |
inputs=[object_input, category_dropdown, background_dropdown], | |
outputs=category_prompt_output | |
) | |
demo.launch() | |