Spaces:
Sleeping
Sleeping
seawolf2357
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,32 +1,70 @@
|
|
1 |
import gradio as gr
|
2 |
import openai
|
3 |
import os
|
|
|
4 |
|
5 |
# OpenAI API ν€ μ€μ
|
6 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
7 |
|
|
|
|
|
|
|
8 |
def generate_keyword_from_text(input_text):
|
9 |
try:
|
10 |
-
response = openai.
|
11 |
-
model="gpt-3.5-turbo",
|
12 |
-
|
13 |
-
|
14 |
-
|
|
|
15 |
)
|
16 |
-
keyword_full_response = response
|
17 |
keyword = keyword_full_response.split('\n', 1)[0].strip()
|
|
|
|
|
18 |
return keyword
|
19 |
except Exception as e:
|
20 |
print(f"μλ¬ λ°μ: {e}")
|
21 |
return f"ν€μλ μμ± μ€ μλ¬ λ°μ: {e}"
|
22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
# Gradio μΈν°νμ΄μ€ μ€μ κ³Ό μ€ν
|
24 |
iface = gr.Interface(
|
25 |
-
fn=
|
26 |
-
inputs=gr.Textbox(lines=
|
27 |
outputs=gr.Gallery(),
|
28 |
title="GPT λ° Pexels APIλ₯Ό μ΄μ©ν λ€μ€ μ΄λ―Έμ§ κ²μ",
|
29 |
description="μ 곡λ ν
μ€νΈμ κ° μ€μ λ°νμΌλ‘ Pexels κ²μμ μ¬μ©ν μλ¬Έ ν€μλλ₯Ό μλ μμ±νκ³ , ν΄λΉ ν€μλλ‘ Pexelsμμ μ΄λ―Έμ§λ₯Ό κ²μν©λλ€."
|
30 |
)
|
31 |
|
32 |
-
iface.launch()
|
|
|
1 |
import gradio as gr
|
2 |
import openai
|
3 |
import os
|
4 |
+
import requests
|
5 |
|
6 |
# OpenAI API ν€ μ€μ
|
7 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
8 |
|
9 |
+
# Pexels API ν€ μ€μ
|
10 |
+
PEXELS_API_KEY = "5woz23MGx1QrSY0WHFb0BRi29JvbXPu97Hg0xnklYgHUI8G0w23FKH62"
|
11 |
+
|
12 |
def generate_keyword_from_text(input_text):
|
13 |
try:
|
14 |
+
response = openai.ChatCompletion.create(
|
15 |
+
model="gpt-3.5-turbo",
|
16 |
+
messages=[{
|
17 |
+
'role': 'user',
|
18 |
+
'content': f"λ€μ ν
μ€νΈλ₯Ό λ°νμΌλ‘ Pexelsμμ μ κ²μλ μλ¬Έ ν€μλ ν μ€μ μμ±νμΈμ: '{input_text}'"
|
19 |
+
}]
|
20 |
)
|
21 |
+
keyword_full_response = response['choices'][0]['message']['content']
|
22 |
keyword = keyword_full_response.split('\n', 1)[0].strip()
|
23 |
+
if keyword.startswith('"') and keyword.endswith('"'):
|
24 |
+
keyword = keyword[1:-1].strip()
|
25 |
return keyword
|
26 |
except Exception as e:
|
27 |
print(f"μλ¬ λ°μ: {e}")
|
28 |
return f"ν€μλ μμ± μ€ μλ¬ λ°μ: {e}"
|
29 |
|
30 |
+
def search_pexels(keyword):
|
31 |
+
headers = {
|
32 |
+
'Authorization': PEXELS_API_KEY
|
33 |
+
}
|
34 |
+
params = {
|
35 |
+
'query': keyword,
|
36 |
+
'per_page': 10
|
37 |
+
}
|
38 |
+
try:
|
39 |
+
response = requests.get('https://api.pexels.com/v1/search', headers=headers, params=params)
|
40 |
+
response.raise_for_status()
|
41 |
+
data = response.json()
|
42 |
+
image_urls = [photo['src']['medium'] for photo in data['photos']]
|
43 |
+
return image_urls
|
44 |
+
except Exception as e:
|
45 |
+
print(f"Pexels API μλ¬: {e}")
|
46 |
+
return []
|
47 |
+
|
48 |
+
def keyword_to_images(input_text):
|
49 |
+
lines = input_text.split('\n') # μ
λ ₯ ν
μ€νΈλ₯Ό μ€ λ¨μλ‘ λΆλ¦¬
|
50 |
+
all_image_urls = []
|
51 |
+
for line in lines:
|
52 |
+
if not line.strip(): # λΉμ΄ μλ μ€μ 건λλ
|
53 |
+
continue
|
54 |
+
keyword = generate_keyword_from_text(line)
|
55 |
+
if keyword.startswith("ν€μλ μμ± μ€ μλ¬ λ°μ"):
|
56 |
+
continue # ν€μλ μμ± μ€ μλ¬κ° λ°μν κ²½μ°, κ·Έ μ€μ 건λλ
|
57 |
+
image_urls = search_pexels(keyword)
|
58 |
+
all_image_urls.extend(image_urls) # κ° ν€μλλ³ κ²μ κ²°κ³Όλ₯Ό λͺ¨λ ν©μΉ¨
|
59 |
+
return all_image_urls
|
60 |
+
|
61 |
# Gradio μΈν°νμ΄μ€ μ€μ κ³Ό μ€ν
|
62 |
iface = gr.Interface(
|
63 |
+
fn=keyword_to_images,
|
64 |
+
inputs=gr.Textbox(lines=10, placeholder="μ¬κΈ°μ Pexels κ²μμ μν ν
μ€νΈλ₯Ό μ
λ ₯νμΈμ. κ° μ€λ§λ€ λ³λμ κ²μ ν€μλκ° μμ±λ©λλ€."),
|
65 |
outputs=gr.Gallery(),
|
66 |
title="GPT λ° Pexels APIλ₯Ό μ΄μ©ν λ€μ€ μ΄λ―Έμ§ κ²μ",
|
67 |
description="μ 곡λ ν
μ€νΈμ κ° μ€μ λ°νμΌλ‘ Pexels κ²μμ μ¬μ©ν μλ¬Έ ν€μλλ₯Ό μλ μμ±νκ³ , ν΄λΉ ν€μλλ‘ Pexelsμμ μ΄λ―Έμ§λ₯Ό κ²μν©λλ€."
|
68 |
)
|
69 |
|
70 |
+
iface.launch()
|