Spaces:
Sleeping
Sleeping
seawolf2357
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -7,23 +7,33 @@ openai.api_key = os.getenv("OPENAI_API_KEY")
|
|
7 |
|
8 |
def generate_keyword_from_text(input_text):
|
9 |
try:
|
10 |
-
# OpenAI ChatCompletion μ μ¬μ©νμ¬
|
11 |
response = openai.ChatCompletion.create(
|
12 |
-
model="gpt-3.5-turbo",
|
13 |
-
messages=[
|
14 |
-
|
15 |
-
|
|
|
16 |
)
|
17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
return keyword
|
19 |
except Exception as e:
|
20 |
print(f"μλ¬ λ°μ: {e}")
|
21 |
-
return "ν€μλ μμ± μ€ μλ¬
|
22 |
|
23 |
# Gradio μΈν°νμ΄μ€ μ€μ κ³Ό μ€ν
|
24 |
iface = gr.Interface(
|
25 |
fn=generate_keyword_from_text,
|
26 |
-
inputs=gr.Textbox(lines=2,
|
27 |
outputs="text",
|
28 |
title="GPTλ₯Ό μ΄μ©ν Pexels κ²μ ν€μλ μμ±",
|
29 |
description="μ 곡λ ν
μ€νΈλ₯Ό λ°νμΌλ‘ Pexels κ²μμ μ¬μ©ν μλ¬Έ ν€μλλ₯Ό μλ μμ±ν©λλ€."
|
|
|
7 |
|
8 |
def generate_keyword_from_text(input_text):
|
9 |
try:
|
10 |
+
# OpenAI ChatCompletion μ μ¬μ©νμ¬ ν μ€μ ν€μλ μμ²
|
11 |
response = openai.ChatCompletion.create(
|
12 |
+
model="gpt-3.5-turbo",
|
13 |
+
messages=[{
|
14 |
+
'role': 'user',
|
15 |
+
'content': f"λ€μ ν
μ€νΈλ₯Ό λ°νμΌλ‘ Pexels μμ μ κ²μλ μλ¬Έ ν€μλ ν μ€μ μμ±νμΈμ: '{input_text}'"
|
16 |
+
}]
|
17 |
)
|
18 |
+
# 첫 λ²μ§Έ choiceμμ message contentλ₯Ό λ°μμ΄
|
19 |
+
keyword_full_response = response['choices'][0]['message']['content']
|
20 |
+
|
21 |
+
# κ°μ₯ 첫 λ²μ§Έ λΌμΈλ§ μΆμΆ
|
22 |
+
keyword = keyword_full_response.split('\n', 1)[0].strip()
|
23 |
+
|
24 |
+
# '""'λ‘ κ°μΌ ν€μλ μ κ±°
|
25 |
+
if keyword.startswith('"') and keyword.endswith('"'):
|
26 |
+
keyword = keyword[1:-1].strip()
|
27 |
+
|
28 |
return keyword
|
29 |
except Exception as e:
|
30 |
print(f"μλ¬ λ°μ: {e}")
|
31 |
+
return f"ν€μλ μμ± μ€ μλ¬ λ°μ: {e}"
|
32 |
|
33 |
# Gradio μΈν°νμ΄μ€ μ€μ κ³Ό μ€ν
|
34 |
iface = gr.Interface(
|
35 |
fn=generate_keyword_from_text,
|
36 |
+
inputs=gr.Textbox(lines=2, placeholder="μ¬κΈ°μ Pexels κ²μμ μν ν
μ€νΈλ₯Ό μ
λ ₯νμΈμ."),
|
37 |
outputs="text",
|
38 |
title="GPTλ₯Ό μ΄μ©ν Pexels κ²μ ν€μλ μμ±",
|
39 |
description="μ 곡λ ν
μ€νΈλ₯Ό λ°νμΌλ‘ Pexels κ²μμ μ¬μ©ν μλ¬Έ ν€μλλ₯Ό μλ μμ±ν©λλ€."
|