Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -2,22 +2,50 @@ import gradio as gr
|
|
2 |
import requests
|
3 |
import json
|
4 |
|
5 |
-
# API URL ์ค์ (
|
6 |
-
API_URL = "https://api.openai.com/v1/
|
7 |
|
8 |
-
# ์คํฌ๋ฆฝํธ๋ฅผ ์ด๋ฏธ์ง ์์ฑ ํ๋กฌํํธ๋ก ๋ณํํ๋ ํจ์
|
9 |
-
def script_to_prompts(script):
|
10 |
-
lines = script.split('\n')
|
11 |
-
|
12 |
for line in lines:
|
13 |
-
#
|
14 |
-
|
15 |
-
|
16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
|
18 |
# OpenAI API๋ฅผ ์ฌ์ฉํ์ฌ ์คํฌ๋ฆฝํธ๋ฅผ ์์ฑํ๋ ํจ์
|
19 |
def predict(inputs, top_p, temperature, openai_api_key):
|
20 |
-
narration_prompt = f"์๋์ฉ ์ ๋๋ฉ์ด์
๋์์์ ์ฌ์ฉํ ์คํฌ๋ฆฝํธ๋ฅผ ์์ฑํ๋ผ. ๋ฐ๋์ ํ๊ธ๋ก ์์ฑํ ๊ฒ. ์
๋ ฅ:
|
21 |
|
22 |
headers = {
|
23 |
"Content-Type": "application/json",
|
@@ -25,18 +53,17 @@ def predict(inputs, top_p, temperature, openai_api_key):
|
|
25 |
}
|
26 |
|
27 |
payload = {
|
28 |
-
"model": "gpt-
|
29 |
-
"
|
30 |
"temperature": temperature,
|
31 |
"top_p": top_p,
|
32 |
-
"
|
33 |
-
"max_tokens": 1000
|
34 |
}
|
35 |
|
36 |
-
response = requests.post(API_URL, headers=headers, json
|
37 |
if response.status_code == 200:
|
38 |
-
response_data = response.
|
39 |
-
generated_text = response_data['choices'][0]['
|
40 |
return generated_text
|
41 |
else:
|
42 |
return "Error: Unable to generate response."
|
@@ -45,28 +72,20 @@ def predict(inputs, top_p, temperature, openai_api_key):
|
|
45 |
with gr.Blocks() as demo:
|
46 |
gr.Markdown("<h1 align='center'>ํ ๋ฆฌ์ ๋ชจํ: 3D ์ ๋๋ฉ์ด์
์์ฑ๊ธฐ</h1>")
|
47 |
with gr.Row():
|
48 |
-
openai_api_key = gr.Textbox(
|
49 |
-
inputs = gr.Textbox(
|
50 |
top_p = gr.Slider(minimum=0, maximum=1.0, value=1.0, step=0.05, label="Top-p (nucleus sampling)")
|
51 |
-
temperature = gr.Slider(minimum=0, maximum=
|
52 |
script_output = gr.Textbox(label="Generated Script", multiline=True)
|
53 |
prompt_output = gr.Textbox(label="Image Generation Prompts", multiline=True)
|
54 |
-
|
55 |
-
submit_button = gr.Button("Generate")
|
56 |
|
57 |
# ๋ฒํผ ํด๋ฆญ ์ ์คํฌ๋ฆฝํธ ์์ฑ ๋ฐ ํ๋กฌํํธ ๋ณํ
|
58 |
-
def generate_and_convert(
|
59 |
-
|
60 |
-
prompts = script_to_prompts(
|
61 |
-
return
|
62 |
-
|
63 |
-
submit_button.click(fn=generate_and_convert,
|
64 |
-
inputs=[inputs, top_p, temperature, openai_api_key],
|
65 |
-
outputs=[script_output, prompt_output])
|
66 |
|
67 |
-
|
68 |
-
["์คํฌ๋ฆฝํธ: ํ ๋ฆฌ๋ ๊ฒ์ ์ฒ์ผ๋ก ๋ชจํ์ ๋ ๋ฌ๋ค."],
|
69 |
-
["์คํฌ๋ฆฝํธ: ํ ๋ฆฌ๋ ๋ฐ๋ค๋ก ๋ชจํ์ ๋ ๋ฌ๋ค."]
|
70 |
-
], inputs=[inputs], fn=predict, outputs=script_output)
|
71 |
|
72 |
demo.launch()
|
|
|
2 |
import requests
|
3 |
import json
|
4 |
|
5 |
+
# API URL ์ค์ (OpenAI GPT-3 API)
|
6 |
+
API_URL = "https://api.openai.com/v1/engines/davinci-codex/completions"
|
7 |
|
8 |
+
# ์คํฌ๋ฆฝํธ๋ฅผ ์ด๋ฏธ์ง ์์ฑ ํ๋กฌํํธ๋ก ๋ณํํ๋ ํจ์ (๋ฒ์ญ ํฌํจ)
|
9 |
+
def script_to_prompts(script, openai_api_key):
|
10 |
+
lines = script.split('\n')
|
11 |
+
translated_prompts = []
|
12 |
for line in lines:
|
13 |
+
# ํ๊ตญ์ด ์คํฌ๋ฆฝํธ๋ฅผ ์์ด๋ก ๋ฒ์ญ
|
14 |
+
translated_line = translate_to_english(line, openai_api_key)
|
15 |
+
# ๋ฒ์ญ๋ ์์ด ๋ผ์ธ์ผ๋ก ์ด๋ฏธ์ง ์์ฑ ํ๋กฌํํธ ์์ฑ
|
16 |
+
prompt = f"3d style, like Harry Potter, {translated_line}, 4k"
|
17 |
+
translated_prompts.append(prompt)
|
18 |
+
return translated_prompts
|
19 |
+
|
20 |
+
# OpenAI API๋ฅผ ์ฌ์ฉํ์ฌ ํ
์คํธ๋ฅผ ์์ด๋ก ๋ฒ์ญํ๋ ํจ์
|
21 |
+
def translate_to_english(korean_text, openai_api_key):
|
22 |
+
translation_prompt = f"Translate the following Korean text to English: '{korean_text}'"
|
23 |
+
|
24 |
+
headers = {
|
25 |
+
"Content-Type": "application/json",
|
26 |
+
"Authorization": f"Bearer {openai_api_key}"
|
27 |
+
}
|
28 |
+
|
29 |
+
payload = {
|
30 |
+
"prompt": translation_prompt,
|
31 |
+
"temperature": 0.5, # ๋ฒ์ญ ์์
์ ์ค๊ฐ ์จ๋ ์ค์
|
32 |
+
"max_tokens": 60, # ๋ฒ์ญํ ๋จ์ด ์ ์ ํ
|
33 |
+
"top_p": 1.0,
|
34 |
+
"frequency_penalty": 0.0,
|
35 |
+
"presence_penalty": 0.0,
|
36 |
+
}
|
37 |
+
|
38 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
39 |
+
if response.status_code == 200:
|
40 |
+
response_data = response.json()
|
41 |
+
generated_text = response_data['choices'][0]['text'].strip()
|
42 |
+
return generated_text
|
43 |
+
else:
|
44 |
+
return "Error: Unable to generate response."
|
45 |
|
46 |
# OpenAI API๋ฅผ ์ฌ์ฉํ์ฌ ์คํฌ๋ฆฝํธ๋ฅผ ์์ฑํ๋ ํจ์
|
47 |
def predict(inputs, top_p, temperature, openai_api_key):
|
48 |
+
narration_prompt = f"์๋์ฉ ์ ๋๋ฉ์ด์
๋์์์ ์ฌ์ฉํ ์คํฌ๋ฆฝํธ๋ฅผ ์์ฑํ๋ผ. ๋ฐ๋์ ํ๊ธ๋ก ์์ฑํ ๊ฒ. ์ผ์ฒด์ ์ง๋ฌธ์ด๋ ์ง์, ๋ฐฐ๊ฒฝ ์ค๋ช
๋ฑ์ ๋
ธ์ถ ํ๊ฑฐ๋ ์ถ๋ ฅํ์ง ๋ง๊ณ ๊ธฐ์น์ ๊ฒฐ์ ๊ตฌ์กฐ๋ก ๋ชจํ์ ์ด์ /์๊ธฐ/๋์ /๋ฌธ์ ํด๊ฒฐ/๊ตํ์ ํฌํจํ์ฌ ์์ํ ๋๋ ์ด์
๋ง 1์ค์ฉ ์ถ๋ ฅํ์ฌ ์ต๋ 10์ค ์ด๋ด๋ก ์ถ๋ ฅ. ์
๋ ฅ:'{inputs}'"
|
49 |
|
50 |
headers = {
|
51 |
"Content-Type": "application/json",
|
|
|
53 |
}
|
54 |
|
55 |
payload = {
|
56 |
+
"model": "gpt-3.5-turbo",
|
57 |
+
"prompt": narration_prompt,
|
58 |
"temperature": temperature,
|
59 |
"top_p": top_p,
|
60 |
+
"max_tokens": 150
|
|
|
61 |
}
|
62 |
|
63 |
+
response = requests.post(API_URL, headers=headers, data=json.dumps(payload))
|
64 |
if response.status_code == 200:
|
65 |
+
response_data = json.loads(response.text)
|
66 |
+
generated_text = response_data['choices'][0]['text'].strip()
|
67 |
return generated_text
|
68 |
else:
|
69 |
return "Error: Unable to generate response."
|
|
|
72 |
with gr.Blocks() as demo:
|
73 |
gr.Markdown("<h1 align='center'>ํ ๋ฆฌ์ ๋ชจํ: 3D ์ ๋๋ฉ์ด์
์์ฑ๊ธฐ</h1>")
|
74 |
with gr.Row():
|
75 |
+
openai_api_key = gr.Textbox(label="Enter your OpenAI API key here", type='password')
|
76 |
+
inputs = gr.Textbox(label="์๋์ฉ ์ ๋๋ฉ์ด์
์คํฌ๋ฆฝํธ๋ฅผ ์์ฑํ๊ณ ์ถ์ ์ฃผ์ ์ด๋ ๋ฌธ์ฅ์ ์
๋ ฅํ์ธ์.", placeholder="์ฌ๊ธฐ์ ์
๋ ฅํ์ธ์.")
|
77 |
top_p = gr.Slider(minimum=0, maximum=1.0, value=1.0, step=0.05, label="Top-p (nucleus sampling)")
|
78 |
+
temperature = gr.Slider(minimum=0, maximum=1.0, value=0.5, step=0.01, label="Temperature")
|
79 |
script_output = gr.Textbox(label="Generated Script", multiline=True)
|
80 |
prompt_output = gr.Textbox(label="Image Generation Prompts", multiline=True)
|
81 |
+
generate_button = gr.Button("Generate")
|
|
|
82 |
|
83 |
# ๋ฒํผ ํด๋ฆญ ์ ์คํฌ๋ฆฝํธ ์์ฑ ๋ฐ ํ๋กฌํํธ ๋ณํ
|
84 |
+
def generate_and_convert(input_text, top_p, temperature, api_key):
|
85 |
+
generated_script = predict(input_text, top_p, temperature, api_key)
|
86 |
+
prompts = script_to_prompts(generated_script, api_key)
|
87 |
+
return generated_script, "\n".join(prompts)
|
|
|
|
|
|
|
|
|
88 |
|
89 |
+
generate_button.click(fn=generate_and_convert, inputs=[inputs, top_p, temperature, openai_api_key], outputs=[script_output, prompt_output])
|
|
|
|
|
|
|
90 |
|
91 |
demo.launch()
|