Spaces:
Runtime error
Runtime error
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() |