Sangwon78 commited on
Commit
e9341ca
ยท
verified ยท
1 Parent(s): f69f20c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +149 -170
app.py CHANGED
@@ -1,177 +1,156 @@
1
- import requests
2
- from bs4 import BeautifulSoup
3
  import gradio as gr
4
- import openai
5
- import os
6
- from typing import Optional
7
-
8
- #############################
9
- # OpenAI API ํด๋ผ์ด์–ธํŠธ ์„ค์ •
10
- #############################
11
- openai.api_key = os.getenv("OPENAI_API_KEY")
12
- if not openai.api_key:
13
- raise ValueError("OpenAI API ํ† ํฐ(OPENAI_API_KEY)์ด ์„ค์ •๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค.")
14
-
15
- def call_openai_api(
16
- content: str,
17
- system_message: str,
18
- max_tokens: int,
19
- temperature: float,
20
- top_p: float
21
- ) -> str:
22
- """
23
- OpenAI์˜ GPT-4o-mini ๋ชจ๋ธ์„ ์ด์šฉํ•ด ํ•œ ๋ฒˆ์˜ ์งˆ๋ฌธ(content)์— ๋Œ€ํ•œ ๋‹ต๋ณ€์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜.
24
- """
25
- try:
26
- response = openai.ChatCompletion.create(
27
- model="gpt-4o-mini",
28
- messages=[
29
- {"role": "system", "content": system_message},
30
- {"role": "user", "content": content},
31
- ],
32
- max_tokens=max_tokens,
33
- temperature=temperature,
34
- top_p=top_p,
35
- )
36
- assistant_message = response.choices[0].message['content']
37
- return assistant_message
38
- except Exception as e:
39
- return f"์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค: {str(e)}"
40
-
41
- #############################
42
- # ๋„ค์ด๋ฒ„ ๋ธ”๋กœ๊ทธ ์Šคํฌ๋ž˜ํ•‘ ๊ธฐ๋Šฅ
43
- #############################
44
- def convert_to_mobile_url(url):
45
- """
46
- PC URL์„ ๋ชจ๋ฐ”์ผ URL๋กœ ๋ณ€ํ™˜.
47
- """
48
- if "m.blog.naver.com" not in url:
49
- if "blog.naver.com" in url:
50
- url_parts = url.split("/")
51
- if len(url_parts) >= 5:
52
- user_id = url_parts[3]
53
- post_id = url_parts[4]
54
- return f"https://m.blog.naver.com/{user_id}/{post_id}"
55
- return url
56
-
57
- def scrape_naver_blog(url):
58
- """
59
- ๋„ค์ด๋ฒ„ ๋ธ”๋กœ๊ทธ์˜ ์ œ๋ชฉ๊ณผ ๋‚ด์šฉ(ํ…์ŠคํŠธ๋งŒ)์„ ์Šคํฌ๋ž˜ํ•‘.
60
- """
61
- try:
62
- # ๋ชจ๋ฐ”์ผ URL ๋ณ€ํ™˜
63
- mobile_url = convert_to_mobile_url(url)
64
- print(f"Converted Mobile URL: {mobile_url}")
65
-
66
- response = requests.get(mobile_url)
67
- response.raise_for_status()
68
-
69
- soup = BeautifulSoup(response.text, 'html.parser')
70
-
71
- # ์ œ๋ชฉ ์Šคํฌ๋ž˜ํ•‘
72
- title_element = soup.find("div", class_="se-module se-module-text se-title-text")
73
- title = title_element.get_text(strip=True) if title_element else "์ œ๋ชฉ์„ ์ฐพ์„ ์ˆ˜ ์—†์Œ"
74
-
75
- # ๋ณธ๋ฌธ ๋‚ด์šฉ ์Šคํฌ๋ž˜ํ•‘
76
- content_elements = soup.find_all("div", class_="se-module se-module-text")
77
- content = "\n".join(elem.get_text(strip=True) for elem in content_elements) if content_elements else "๋‚ด์šฉ์„ ์ฐพ์„ ์ˆ˜ ์—†์Œ"
78
-
79
- print(f"Scraped Title: {title}")
80
- print(f"Scraped Content: {content}")
81
-
82
- result = f"์ œ๋ชฉ: {title}\n\n๋‚ด์šฉ: {content}"
83
- return result
84
-
85
- except Exception as e:
86
- print(f"Error: {e}")
87
- return f"Error: {e}"
88
-
89
- def run_scraper(url):
90
- return scrape_naver_blog(url)
91
-
92
- #############################
93
- # ๋ธ”๋กœ๊ทธ ์ƒ์„ฑ ๊ธฐ๋Šฅ (ChatGPT ๊ธฐ๋ฐ˜)
94
- #############################
95
- OPENAI_SYSTEM_MESSAGE = """๋ฐ˜๋“œ์‹œ ํ•œ๊ธ€๋กœ ๋‹ต๋ณ€ํ•  ๊ฒƒ.
96
- ๋„ˆ๋Š” ์ตœ๊ณ ์˜ ๋น„์„œ์ด๋‹ค.
97
- ๋‚ด๊ฐ€ ์š”๊ตฌํ•˜๋Š” ๊ฒƒ๋“ค์„ ์ตœ๋Œ€ํ•œ ์ž์„ธํ•˜๊ณ  ์ •ํ™•ํ•˜๊ฒŒ ๋‹ต๋ณ€ํ•˜๋ผ.
98
- ##[๊ธฐ๋ณธ๊ทœ์น™]
99
- 1. ๋ฐ˜๋“œ์‹œ ํ•œ๊ตญ์–ด(ํ•œ๊ธ€)๋กœ ์ž‘์„ฑํ•˜๋ผ.
100
- 2. ๋„ˆ๋Š” ๊ฐ€์žฅ ์ฃผ๋ชฉ๋ฐ›๋Š” ๋งˆ์ผ€ํ„ฐ์ด๋ฉฐ ๋ธ”๋กœ๊ทธ ๋งˆ์ผ€ํŒ… ์ „๋ฌธ๊ฐ€์ด๋‹ค.
101
- 3. ํŠนํžˆ ๋„ˆ๋Š” '์ •๋ณด์„ฑ(Informative)' ์ „๋ฌธ ๋ธ”๋กœ๊ทธ ๋งˆ์ผ€ํŒ… ์ „๋ฌธ๊ฐ€์ด๋‹ค.
102
- 4. ์ •๋ณด ์ œ๊ณต์— ์ดˆ์ ์„ ๋งž์ถ”์–ด ์ž‘์„ฑํ•œ๋‹ค.
103
- ##[ํ…์ŠคํŠธ ์ž‘์„ฑ ๊ทœ์น™]
104
- 1. ์†Œ์ฃผ์ œ๋ฅผ 5๊ฐœ๋กœ ๊ตฌ๋ถ„ํ•˜์—ฌ 2000์ž ์ด์ƒ๋˜๋„๋ก ์ž‘์„ฑํ•˜๋ผ.
105
- 2. ์ „์ฒด ๋งฅ๋ฝ์„ ์ดํ•ดํ•˜๊ณ  ๋ฌธ์žฅ์˜ ์ผ๊ด€์„ฑ์„ ์œ ์ง€ํ•˜๋ผ.
106
- 3. ์ ˆ๋Œ€๋กœ ์ฐธ๊ณ ๊ธ€์„ ํ•œ๋ฌธ์žฅ ์ด์ƒ ๊ทธ๋Œ€๋กœ ์ถœ๋ ฅํ•˜์ง€ ๋ง ๊ฒƒ.
107
- 4. ์ฃผ์ œ์™€ ์ƒํ™ฉ์— ๋งž๋Š” ์ ์ ˆํ•œ ์–ดํœ˜๋ฅผ ์„ ํƒํ•˜๋ผ.
108
- 5. ํ•œ๊ธ€ ์–ดํœ˜์˜ ๋‚œ์ด๋„๋Š” ์‰ฝ๊ฒŒ ์ž‘์„ฑํ•˜๋ผ.
109
- 6. ์ ˆ๋Œ€ ๋ฌธ์žฅ์˜ ๋์— '๋‹ต๋‹ˆ๋‹ค'๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ๋ง ๊ฒƒ.
110
- ###[์ •๋ณด์„ฑ ๋ธ”๋กœ๊ทธ ์ž‘์„ฑ ๊ทœ์น™]
111
- 1. ๋…์ž๊ฐ€ ์–ป๊ณ ์ž ํ•˜๋Š” ์œ ์šฉํ•œ ์ •๋ณด์™€ ํฅ๋ฏธ๋กœ์šด ์ •๋ณด๋ฅผ ์ œ๊ณตํ•˜๋„๋ก ์ž‘์„ฑํ•˜๋ผ.
112
- 2. ๋…์ž์˜ ๊ณต๊ฐ์„ ์ด๋Œ์–ด๋‚ด๊ณ  ๊ถ๊ธˆ์ฆ์„ ํ•ด๊ฒฐํ•˜๋„๋ก ์ž‘์„ฑํ•˜๋ผ.
113
- 3. ๋…์ž์˜ ๊ด€์‹ฌ์‚ฌ๋ฅผ ์ถฉ์กฑ์‹œํ‚ค๋„๋ก ์ž‘์„ฑํ•˜๋ผ.
114
- 4. ๋…์ž์—๊ฒŒ ์ด๋“์ด ๋˜๋Š” ์ •๋ณด๋ฅผ ์ž‘์„ฑํ•˜๋ผ.
115
- ##[์ œ์™ธ ๊ทœ์น™]
116
- 1. ๋ฐ˜๋“œ์‹œ ๋น„์†์–ด ๋ฐ ์š•์„ค(expletive, abusive language, slang)์€ ์ œ์™ธํ•˜๋ผ.
117
- 2. ๋ฐ˜๋“œ์‹œ ์ฐธ๊ณ ๊ธ€์˜ ๋งํฌ(URL)๋Š” ์ œ์™ธํ•˜๋ผ.
118
- 3. ์ฐธ๊ณ ๊ธ€์—์„œ '๋งํฌ๋ฅผ ํ™•์ธํ•ด์ฃผ์„ธ์š”'์™€ ๊ฐ™์€ ๋งํฌ ์ด๋™์˜ ๋ฌธ๊ตฌ๋Š” ์ œ์™ธํ•˜๋ผ.
119
- 4. ์ฐธ๊ณ ๊ธ€์— ์žˆ๋Š” ์ž‘์„ฑ์ž, ํ™”์ž, ์œ ํŠœ๋ฒ„, ๊ธฐ์ž์˜ ์ด๋ฆ„, ์• ์นญ, ๋‹‰๋„ค์ž„์€ ๋ฐ˜๋“œ์‹œ ์ œ์™ธํ•˜๋ผ.
120
- 5. ๋ฐ˜๋“œ์‹œ ๋ฌธ์žฅ์˜ ๋๋ถ€๋ถ„์ด ์–ด์ƒ‰ํ•œ ํ•œ๊ตญ์–ด ํ‘œํ˜„์€ ์ œ์™ธํ•˜๋ผ('์˜ˆ์š”', '๋‹ต๋‹ˆ๋‹ค', 'ํ•ด์š”', 'ํ•ด์ฃผ์ฃ ', '๋์ฃ ', '๋์–ด์š”', '๊ณ ์š”' ๋“ฑ.)
121
- """
122
-
123
- OPENAI_MAX_TOKENS = 4000
124
- OPENAI_TEMPERATURE = 0.7
125
- OPENAI_TOP_P = 0.95
126
-
127
- def generate_blog(tone_value: str, ref1_value: str, ref2_value: str, ref3_value: str) -> str:
128
  """
129
- ChatGPT๋ฅผ ์ด์šฉํ•ด ๋ธ”๋กœ๊ทธ ํฌ์ŠคํŠธ๋ฅผ ์ƒ์„ฑํ•˜๋Š” ํ•จ์ˆ˜.
130
  """
131
- question = (
132
- f"๋งํˆฌ: {tone_value}\n"
133
- f"์ฐธ์กฐ๊ธ€1: {ref1_value}\n"
134
- f"์ฐธ์กฐ๊ธ€2: {ref2_value}\n"
135
- f"์ฐธ์กฐ๊ธ€3: {ref3_value}\n"
136
- )
137
- response = call_openai_api(
138
- content=question,
139
- system_message=OPENAI_SYSTEM_MESSAGE,
140
- max_tokens=OPENAI_MAX_TOKENS,
141
- temperature=OPENAI_TEMPERATURE,
142
- top_p=OPENAI_TOP_P
143
- )
144
- return response
145
-
146
- #############################
147
  # Gradio ์ธํ„ฐํŽ˜์ด์Šค (ํƒญ ๊ตฌ์„ฑ)
148
- #############################
149
- with gr.Blocks() as demo:
150
- gr.Markdown("# ์ข…ํ•ฉ ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜")
151
-
 
152
  with gr.Tabs():
153
- # ์ฒซ ๋ฒˆ์งธ ํƒญ: ๋„ค์ด๋ฒ„ ๋ธ”๋กœ๊ทธ ์Šคํฌ๋ž˜ํ•‘
154
- with gr.TabItem("๋„ค์ด๋ฒ„ ๋ธ”๋กœ๊ทธ ์Šคํฌ๋ž˜ํ•‘"):
155
- gr.Markdown("## ๋„ค์ด๋ฒ„ ๋ธ”๋กœ๊ทธ ์Šคํฌ๋ž˜ํ•‘")
156
- scraper_url = gr.Textbox(label="๋„ค์ด๋ฒ„ ๋ธ”๋กœ๊ทธ URL", placeholder="https://blog.naver.com/...")
157
- scraper_output = gr.Textbox(label="์Šคํฌ๋ž˜ํ•‘ ๊ฒฐ๊ณผ", lines=20, interactive=False)
158
- run_scraper_button = gr.Button("์Šคํฌ๋ž˜ํ•‘ ์‹คํ–‰")
159
- run_scraper_button.click(fn=run_scraper, inputs=scraper_url, outputs=scraper_output)
160
-
161
- # ๋‘ ๋ฒˆ์งธ ํƒญ: ๋ธ”๋กœ๊ทธ ์ƒ์„ฑ๊ธฐ (ChatGPT ๊ธฐ๋ฐ˜)
162
- with gr.TabItem("๋ธ”๋กœ๊ทธ ์ƒ์„ฑ๊ธฐ"):
163
- gr.Markdown("## ๋ธ”๋กœ๊ทธ ์ƒ์„ฑ๊ธฐ")
164
- tone_radio = gr.Radio(
165
- label="๋งํˆฌ๋ฐ”๊พธ๊ธฐ",
166
- choices=["์นœ๊ทผํ•˜๊ฒŒ", "์ผ๋ฐ˜์ ์ธ", "์ „๋ฌธ์ ์ธ"],
167
- value="์ผ๋ฐ˜์ ์ธ"
 
 
 
 
 
 
 
168
  )
169
- ref1 = gr.Textbox(label="์ฐธ์กฐ๊ธ€ 1")
170
- ref2 = gr.Textbox(label="์ฐธ์กฐ๊ธ€ 2")
171
- ref3 = gr.Textbox(label="์ฐธ์กฐ๊ธ€ 3")
172
- blog_output = gr.Textbox(label="์ƒ์„ฑ๋œ ๋ธ”๋กœ๊ทธ", lines=20, interactive=False)
173
- generate_button = gr.Button("์ƒ์„ฑํ•˜๊ธฐ")
174
- generate_button.click(fn=generate_blog, inputs=[tone_radio, ref1, ref2, ref3], outputs=blog_output)
175
 
176
- if __name__ == "__main__":
177
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ import cv2
3
+ import numpy as np
4
+ from PIL import Image, ImageEnhance, ImageDraw, ImageFont
5
+ from gradio_imageslider import ImageSlider
6
+
7
+ ######################################
8
+ # ํ•„ํ„ฐ ์ ์šฉ ๊ด€๋ จ ํ•จ์ˆ˜๋“ค
9
+ ######################################
10
+ def apply_filter(image, filter_type, intensity):
11
+ image = np.array(image)
12
+ normalized_intensity = intensity / 100.0
13
+
14
+ if filter_type == "Grayscale":
15
+ return convert_to_grayscale(image)
16
+ elif filter_type == "Soft Glow":
17
+ base_intensity = 0.1
18
+ adjusted_intensity = base_intensity + (normalized_intensity * (1 - base_intensity))
19
+ gaussian = cv2.GaussianBlur(image, (15, 15), 0)
20
+ soft_glow = cv2.addWeighted(image, 1 - adjusted_intensity, gaussian, adjusted_intensity, 0)
21
+ return soft_glow
22
+ elif filter_type == "Portrait Enhancer":
23
+ base_intensity = 0.5
24
+ adjusted_intensity = base_intensity + (normalized_intensity * (1 - base_intensity))
25
+ image_pil = Image.fromarray(image)
26
+ enhancer = ImageEnhance.Sharpness(image_pil)
27
+ image_pil = enhancer.enhance(1 + 0.5 * adjusted_intensity)
28
+ enhancer = ImageEnhance.Color(image_pil)
29
+ image_pil = enhancer.enhance(1 + 0.5 * adjusted_intensity)
30
+ enhanced_image = np.array(image_pil)
31
+ return enhanced_image
32
+ elif filter_type == "Warm Tone":
33
+ warm_image = cv2.addWeighted(image, 1.0, np.full(image.shape, (20, 66, 112), dtype=np.uint8), 0.3 * normalized_intensity, 0)
34
+ return warm_image
35
+ elif filter_type == "Cold Tone":
36
+ cold_image = cv2.addWeighted(image, 1.0, np.full(image.shape, (112, 66, 20), dtype=np.uint8), 0.3 * normalized_intensity, 0)
37
+ return cold_image
38
+ elif filter_type == "High-Key":
39
+ high_key = cv2.convertScaleAbs(image, alpha=1.0 + 0.3 * normalized_intensity, beta=20)
40
+ return high_key
41
+ elif filter_type == "Low-Key":
42
+ low_key = cv2.convertScaleAbs(image, alpha=1.0 - 0.1 * normalized_intensity, beta=-10)
43
+ return low_key
44
+ elif filter_type == "Haze":
45
+ haze = cv2.addWeighted(image, 1.0, np.full(image.shape, 255, dtype=np.uint8), 0.3 * normalized_intensity, 0)
46
+ return haze
47
+ else:
48
+ return image
49
+
50
+ def convert_to_grayscale(image):
51
+ gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
52
+ return cv2.cvtColor(gray_image, cv2.COLOR_GRAY2BGR)
53
+
54
+ def convert_and_save(image, filter_type, intensity):
55
+ # OpenCV ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•ด RGB -> BGR ๋ณ€ํ™˜
56
+ image_cv = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
57
+ filtered_image = apply_filter(image_cv, filter_type, intensity)
58
+
59
+ # ์Šฌ๋ผ์ด๋” ๋น„๊ต์šฉ ์›๋ณธ, ํ•„ํ„ฐ ์ ์šฉ ์ด๋ฏธ์ง€ (BGR -> RGB)
60
+ original_image_pil = Image.fromarray(cv2.cvtColor(image_cv, cv2.COLOR_BGR2RGB))
61
+ filtered_image_pil = Image.fromarray(cv2.cvtColor(filtered_image, cv2.COLOR_BGR2RGB))
62
+
63
+ # ๋‹ค์šด๋กœ๋“œ์šฉ ํŒŒ์ผ ์ €์žฅ (JPEG ํ˜•์‹)
64
+ output_path = "filtered_image.jpg"
65
+ filtered_image_pil.save(output_path)
66
+
67
+ # ์ถ”๊ฐ€: ํ•„ํ„ฐ ์ ์šฉ ์ด๋ฏธ์ง€์˜ ํ‘๋ฐฑ(Grayscale) ๋ฒ„์ „ ์ƒ์„ฑ
68
+ grayscale_image = convert_to_grayscale(filtered_image)
69
+ grayscale_image_pil = Image.fromarray(cv2.cvtColor(grayscale_image, cv2.COLOR_BGR2RGB))
70
+
71
+ return [original_image_pil, filtered_image_pil], output_path, grayscale_image_pil
72
+
73
+ def get_filter_description(filter_type):
74
+ descriptions = {
75
+ "Grayscale": "์ด๋ฏธ์ง€๋ฅผ ํ‘๋ฐฑ์œผ๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค.",
76
+ "Soft Glow": "๋ถ€๋“œ๋Ÿฌ์šด ๋น›์„ ์ถ”๊ฐ€ํ•˜์—ฌ ์ด๋ฏธ์ง€๋ฅผ ์€์€ํ•˜๊ฒŒ ๋งŒ๋“ญ๋‹ˆ๋‹ค.",
77
+ "Portrait Enhancer": "ํ”ผ๋ถ€ ํ†ค์„ ๊ท ์ผํ•˜๊ฒŒ ํ•˜๊ณ  ์„ ๋ช…๋„๋ฅผ ์กฐ์ ˆํ•˜์—ฌ ์ธ๋ฌผ์„ ๋”์šฑ ๋‹๋ณด์ด๊ฒŒ ๋งŒ๋“ญ๋‹ˆ๋‹ค.",
78
+ "Warm Tone": "๋”ฐ๋œปํ•œ ์ƒ‰์กฐ๋ฅผ ์ถ”๊ฐ€ํ•˜์—ฌ ์ด๋ฏธ์ง€์— ์˜จ๊ธฐ๋ฅผ ๋”ํ•ฉ๋‹ˆ๋‹ค.",
79
+ "Cold Tone": "์ฐจ๊ฐ€์šด ์ƒ‰์กฐ๋ฅผ ์ถ”๊ฐ€ํ•˜์—ฌ ์ด๋ฏธ์ง€์— ์‹œ์›ํ•จ์„ ๋”ํ•ฉ๋‹ˆ๋‹ค.",
80
+ "High-Key": "๋ฐ๊ณ  ํ™”์‚ฌํ•œ ์ด๋ฏธ์ง€๋ฅผ ๋งŒ๋“ค์–ด๋ƒ…๋‹ˆ๋‹ค.",
81
+ "Low-Key": "์–ด๋‘์šด ํ†ค์„ ๊ฐ•์กฐํ•˜์—ฌ ๋ถ„์œ„๊ธฐ ์žˆ๋Š” ์ด๋ฏธ์ง€๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค.",
82
+ "Haze": "๋ถ€๋“œ๋Ÿฝ๊ณ  ํ๋ฆฟํ•œ ํšจ๊ณผ๋ฅผ ์ถ”๊ฐ€ํ•˜์—ฌ ๋ชฝํ™˜์ ์ธ ์ด๋ฏธ์ง€๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค."
83
+ }
84
+ return descriptions.get(filter_type, "")
85
+
86
+ ######################################
87
+ # ์ด๋ฏธ์ง€ ๋ฉ”์ดํ‚น ๊ด€๋ จ ํ•จ์ˆ˜
88
+ ######################################
89
+ def generate_custom_image(width, height, bg_color, text):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90
  """
91
+ ์ฃผ์–ด์ง„ ํฌ๊ธฐ์™€ ๋ฐฐ๊ฒฝ์ƒ‰, ๊ทธ๋ฆฌ๊ณ  ํ…์ŠคํŠธ๋ฅผ ์ด์šฉํ•ด ์ด๋ฏธ์ง€๋ฅผ ์ƒ์„ฑํ•˜๋Š” ํ•จ์ˆ˜.
92
  """
93
+ # PIL์—์„œ bg_color๋Š” ์ผ๋ฐ˜์ ์œผ๋กœ hex ์ฝ”๋“œ๋‚˜ ํŠœํ”Œ(R,G,B)๋กœ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.
94
+ # gr.ColorPicker๋Š” hex ์ฝ”๋“œ ํ˜•ํƒœ๋กœ ๊ฐ’์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.
95
+ image = Image.new("RGB", (width, height), bg_color)
96
+ if text:
97
+ draw = ImageDraw.Draw(image)
98
+ font = ImageFont.load_default()
99
+ text_size = draw.textsize(text, font=font)
100
+ text_position = ((width - text_size[0]) / 2, (height - text_size[1]) / 2)
101
+ draw.text(text_position, text, font=font, fill="black")
102
+ return image
103
+
104
+ ######################################
 
 
 
 
105
  # Gradio ์ธํ„ฐํŽ˜์ด์Šค (ํƒญ ๊ตฌ์„ฑ)
106
+ ######################################
107
+ with gr.Blocks() as iface:
108
+ gr.Markdown("# ์ด๋ฏธ์ง€ ์ฒ˜๋ฆฌ ๋„๊ตฌ")
109
+ gr.Markdown("์ธ๋ฌผ ์‚ฌ์ง„ ํ•„ํ„ฐ์™€ ์ด๋ฏธ์ง€ ๋ฉ”์ดํ‚น ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.")
110
+
111
  with gr.Tabs():
112
+ # ํƒญ 1: ์ธ๋ฌผ ์‚ฌ์ง„ ํ•„ํ„ฐ
113
+ with gr.TabItem("์ธ๋ฌผ ์‚ฌ์ง„ ํ•„ํ„ฐ"):
114
+ with gr.Row():
115
+ with gr.Column():
116
+ image_input = gr.Image(type="pil", label="์ด๋ฏธ์ง€ ์—…๋กœ๋“œ")
117
+ filter_input = gr.Radio(
118
+ ["Grayscale", "Soft Glow", "Portrait Enhancer", "Warm Tone", "Cold Tone", "High-Key", "Low-Key", "Haze"],
119
+ label="ํ•„ํ„ฐ ์„ ํƒ",
120
+ value="Soft Glow"
121
+ )
122
+ intensity_slider = gr.Slider(1, 100, value=50, label="ํ•„ํ„ฐ ๊ฐ•๋„")
123
+ description_output = gr.Markdown(get_filter_description("Soft Glow"))
124
+ with gr.Column():
125
+ slider_output = ImageSlider(label="Before and After", type="pil")
126
+ download_link = gr.File(label="Download Filtered Image")
127
+ grayscale_output = gr.Image(label="Grayscale Output")
128
+ filter_input.change(fn=get_filter_description, inputs=filter_input, outputs=description_output)
129
+ process_button = gr.Button("ํ•„ํ„ฐ ์ ์šฉ")
130
+ process_button.click(
131
+ fn=convert_and_save,
132
+ inputs=[image_input, filter_input, intensity_slider],
133
+ outputs=[slider_output, download_link, grayscale_output]
134
  )
 
 
 
 
 
 
135
 
136
+ # ํƒญ 2: ์ด๋ฏธ์ง€ ๋ฉ”์ดํ‚น
137
+ with gr.TabItem("์ด๋ฏธ์ง€ ๋ฉ”์ดํ‚น"):
138
+ with gr.Row():
139
+ with gr.Column():
140
+ width_slider = gr.Slider(100, 1000, value=500, label="๊ฐ€๋กœ ํฌ๊ธฐ")
141
+ height_slider = gr.Slider(100, 1000, value=500, label="์„ธ๋กœ ํฌ๊ธฐ")
142
+ bg_color_picker = gr.ColorPicker(value="#FFFFFF", label="๋ฐฐ๊ฒฝ ์ƒ‰์ƒ")
143
+ text_input = gr.Textbox(label="ํ…์ŠคํŠธ (์˜ต์…˜)", placeholder="์ด๋ฏธ์ง€์— ์‚ฝ์ž…ํ•  ํ…์ŠคํŠธ")
144
+ generate_image_button = gr.Button("์ด๋ฏธ์ง€ ์ƒ์„ฑ")
145
+ with gr.Column():
146
+ image_output = gr.Image(label="์ƒ์„ฑ๋œ ์ด๋ฏธ์ง€", type="pil")
147
+ generate_image_button.click(
148
+ fn=generate_custom_image,
149
+ inputs=[width_slider, height_slider, bg_color_picker, text_input],
150
+ outputs=image_output
151
+ )
152
+
153
+ iface.title = "์ด๋ฏธ์ง€ ์ฒ˜๋ฆฌ ๋„๊ตฌ"
154
+ iface.description = "์ธ๋ฌผ ์‚ฌ์ง„ ํ•„ํ„ฐ์™€ ์ด๋ฏธ์ง€ ๋ฉ”์ดํ‚น ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค."
155
+
156
+ iface.launch()