File size: 3,879 Bytes
d9f0669
c64d829
 
d9f0669
40b0f4a
faf98f2
40b0f4a
 
 
3e6b150
40b0f4a
324ddc8
40b0f4a
 
 
 
 
3e6b150
c64d829
40b0f4a
494959e
c64d829
 
 
 
 
40b0f4a
 
c64d829
 
40b0f4a
 
c64d829
40b0f4a
c64d829
40b0f4a
 
324ddc8
c64d829
324ddc8
faf98f2
3b46ac0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c64d829
 
 
40b0f4a
 
c64d829
40b0f4a
 
 
 
 
a8c6d92
3b46ac0
 
 
40b0f4a
3b46ac0
3a9a368
3b46ac0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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()