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