imgtest2 / app.py
seawolf2357's picture
Update app.py
3b46ac0 verified
raw
history blame
3.88 kB
import gradio as gr
import requests
import json
API_URL = "https://api.openai.com/v1/chat/completions"
# 이미지 μƒμ„±μš© ν”„λ‘¬ν”„νŠΈλ₯Ό μž‘μ„±ν•˜λŠ” ν•¨μˆ˜
def create_image_prompts(script, openai_api_key):
# 슀크립트의 각 쀄을 λΆ„μ„ν•˜μ—¬ ν”„λ‘¬ν”„νŠΈ 생성 (μ—¬κΈ°μ„œλŠ” λ‹¨μˆœν™”λœ μ˜ˆμ‹œλ‘œ κ΅¬ν˜„)
lines = script.split('\n')
prompts = []
for line in lines:
# μ£Όμš” ν–‰μœ„ ν‚€μ›Œλ“œ μΆ”μΆœ (μ—¬κΈ°μ„œλŠ” λ‹¨μˆœν™”λœ ν˜•νƒœλ‘œ κ΅¬ν˜„)
keyword = "adventure" # μ˜ˆμ‹œ ν‚€μ›Œλ“œ, μ‹€μ œ κ΅¬ν˜„μ—μ„œλŠ” 뢄석을 톡해 μΆ”μΆœ
prompt = f"3d style, 4K, like Brad Pitt young boy, {keyword}"
prompts.append(prompt)
return "\n".join(prompts)
def predict(inputs, top_p, temperature, openai_api_key):
# OpenAI APIλ₯Ό μ‚¬μš©ν•˜μ—¬ 슀크립트 생성
narration_prompt = f"μ•„λ™μš© μ• λ‹ˆλ©”μ΄μ…˜ λ™μ˜μƒμ— μ‚¬μš©ν•  슀크립트λ₯Ό μž‘μ„±ν•˜λΌ. λ°˜λ“œμ‹œ ν•œκΈ€λ‘œ μž‘μ„±ν• κ²ƒ. 일체의 μ§€λ¬Έμ΄λ‚˜ μ§€μ‹œμ–΄, μ„€λͺ… λΆ€ν˜Έ 등을 ν¬ν•¨μ‹œν‚€μ§€ 말것. μž…λ ₯: '{inputs}'"
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {openai_api_key}"
}
payload = {
"model": "gpt-4-1106-preview",
"messages": [{"role": "system", "content": narration_prompt}],
"temperature": temperature,
"top_p": top_p,
"n": 1,
"max_tokens": 1000
}
response = requests.post(API_URL, headers=headers, json=payload)
if response.status_code == 200:
response_data = response.json()
generated_text = response_data['choices'][0]['message']['content']
return generated_text
else:
return "Error: Unable to generate response."
def create_detailed_image_prompts(script):
# 슀크립트의 각 쀄을 λΆ„μ„ν•˜μ—¬ ν”„λ‘¬ν”„νŠΈ 생성
lines = script.split('\n')
detailed_prompts = []
for line in lines:
# μ—¬κΈ°μ„œλŠ” μ˜ˆμ‹œλ‘œ λͺ‡ κ°€μ§€ κ°„λ‹¨ν•œ νŒ¨ν„΄μ„ μ‚¬μš©ν•©λ‹ˆλ‹€.
# μ‹€μ œ κ΅¬ν˜„μ—μ„œλŠ” 더 λ³΅μž‘ν•œ μžμ—°μ–΄ 처리λ₯Ό μ μš©ν•΄μ•Ό ν•  수 μžˆμŠ΅λ‹ˆλ‹€.
if "야ꡬμž₯에 κ°”λ‹€" in line:
action = "go to the baseball (background)"
prompt = f"3d style, 4K, like Brad Pitt young boy, {action}"
elif "μˆ²μ„ νƒν—˜ν–ˆλ‹€" in line:
action = "explore the forest (background)"
prompt = f"3d style, 4K, like Brad Pitt young boy, {action}"
else:
# μΌμΉ˜ν•˜λŠ” νŒ¨ν„΄μ΄ 없을 경우 κΈ°λ³Έ ν”„λ‘¬ν”„νŠΈ μ‚¬μš©
prompt = f"3d style, 4K, like Brad Pitt young boy, adventure"
detailed_prompts.append(prompt)
return "\n".join(detailed_prompts)
# Gradio μΈν„°νŽ˜μ΄μŠ€ μˆ˜μ •
with gr.Blocks() as demo:
gr.Markdown("<h1 align='center'>ν† λ¦¬μ˜ λͺ¨ν—˜: 3D μ• λ‹ˆλ©”μ΄μ…˜ 생성기</h1>")
with gr.Row():
openai_api_key = gr.Textbox(type='password', label="Enter your OpenAI API key here")
inputs = gr.Textbox(placeholder="여기에 μž…λ ₯ν•˜μ„Έμš”.", label="μ•„λ™μš© μ• λ‹ˆλ©”μ΄μ…˜ 슀크립트λ₯Ό μƒμ„±ν•˜κ³  싢은 μ£Όμ œμ–΄λ‚˜ λ¬Έμž₯을 μž…λ ₯ν•˜μ„Έμš”.")
top_p = gr.Slider(minimum=0, maximum=1.0, value=1.0, step=0.05, label="Top-p (nucleus sampling)")
temperature = gr.Slider(minimum=0, maximum=5.0, value=1.0, step=0.1, label="Temperature")
output = gr.Textbox(label="Generated Script", readonly=True)
prompts_output = gr.TextArea(label="Image Generation Prompts", readonly=True)
submit_button = gr.Button("Generate Script")
prompts_button = gr.Button("Generate Image Prompts")
def generate_prompts(script):
return create_detailed_image_prompts(script)
submit_button.click(fn=predict, inputs=[inputs, top_p, temperature, openai_api_key], outputs=output)
prompts_button.click(fn=generate_prompts, inputs=[output], outputs=prompts_output)
demo.launch()