IC-test3 / app.py
ssboost's picture
Update app.py
1e7ecaa verified
raw
history blame
4.91 kB
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()