Spaces:
Sleeping
Sleeping
seawolf2357
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -5,38 +5,31 @@ import os
|
|
5 |
# OpenAI API ν€ μ€μ
|
6 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
7 |
|
8 |
-
def
|
9 |
-
|
10 |
-
|
11 |
-
|
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 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
|
33 |
# Gradio μΈν°νμ΄μ€ μ€μ κ³Ό μ€ν
|
34 |
iface = gr.Interface(
|
35 |
-
fn=
|
36 |
-
inputs=gr.Textbox(lines=
|
37 |
-
outputs=
|
38 |
-
title="GPTλ₯Ό μ΄μ©ν
|
39 |
-
description="μ 곡λ
|
40 |
)
|
41 |
|
42 |
iface.launch()
|
|
|
5 |
# OpenAI API ν€ μ€μ
|
6 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
7 |
|
8 |
+
def generate_keywords_and_search_images(input_text):
|
9 |
+
# μ
λ ₯ ν
μ€νΈλ₯Ό μ€λ³λ‘ λΆλ¦¬
|
10 |
+
lines = input_text.strip().split('\n')
|
11 |
+
all_image_urls = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
+
for line in lines:
|
14 |
+
# κ° μ€μ λν ν€μλ μμ±
|
15 |
+
keyword = generate_keyword_from_text(line)
|
16 |
+
if keyword.startswith("ν€μλ μμ± μ€ μλ¬ λ°μ"):
|
17 |
+
print(keyword)
|
18 |
+
continue # ν€μλ μμ± μ€ν¨ μ, λ€μ μ€λ‘ λμ΄κ°
|
19 |
+
|
20 |
+
# μμ±λ ν€μλλ‘ Pexels κ²μ
|
21 |
+
image_urls = search_pexels(keyword)
|
22 |
+
all_image_urls.extend(image_urls) # κ²μλ μ΄λ―Έμ§ URLλ€μ μ 체 λͺ©λ‘μ μΆκ°
|
23 |
+
|
24 |
+
return all_image_urls
|
25 |
|
26 |
# Gradio μΈν°νμ΄μ€ μ€μ κ³Ό μ€ν
|
27 |
iface = gr.Interface(
|
28 |
+
fn=generate_keywords_and_search_images,
|
29 |
+
inputs=gr.Textbox(lines=5, placeholder="μ¬κΈ°μ Pexels κ²μμ μν ν
μ€νΈλ₯Ό μ
λ ₯νμΈμ. κ° μ€λ§λ€ λ³λμ κ²μ ν€μλκ° μμ±λ©λλ€."),
|
30 |
+
outputs=gr.Gallery(),
|
31 |
+
title="GPT λ° Pexels APIλ₯Ό μ΄μ©ν λ€μ€ μ΄λ―Έμ§ κ²μ",
|
32 |
+
description="μ 곡λ ν
μ€νΈμ κ° μ€μ λ°νμΌλ‘ Pexels κ²μμ μ¬μ©ν μλ¬Έ ν€μλλ₯Ό μλ μμ±νκ³ , ν΄λΉ ν€μλλ‘ Pexelsμμ μ΄λ―Έμ§λ₯Ό κ²μν©λλ€."
|
33 |
)
|
34 |
|
35 |
iface.launch()
|