Spaces:
Runtime error
Runtime error
import gradio as gr | |
import requests | |
import json | |
from transformers import pipeline | |
API_URL = "https://api.openai.com/v1/chat/completions" | |
# λ²μ νμ΄νλΌμΈ μ΄κΈ°ν | |
translator = pipeline("translation", model="Helsinki-NLP/opus-mt-ko-en") | |
def translate_text(text): | |
# μ λ ₯λ ν μ€νΈλ₯Ό μμ΄λ‘ λ²μ | |
translation = translator(text, max_length=512) | |
translated_text = translation[0]['translation_text'] | |
return translated_text | |
def predict(inputs, top_p, temperature, openai_api_key): | |
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 generate_prompts(script): | |
# μ€ν¬λ¦½νΈμ κ° μ€μ μμ΄λ‘ λ²μνμ¬ ν둬ννΈ μμ± | |
lines = script.split('\n') | |
translated_prompts = [translate_text(line) for line in lines if line.strip() != ''] | |
return "\n".join(translated_prompts) | |
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="Translated Image Generation Prompts", readonly=True) | |
submit_button = gr.Button("Generate Script") | |
prompts_button = gr.Button("Translate Prompts") | |
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() | |