Spaces:
Sleeping
Sleeping
""" | |
์ ํธ๋ฆฌํฐ ํจ์๋ค | |
""" | |
import random | |
import requests | |
from PIL import Image, ImageDraw, ImageFont | |
import io | |
from config import CHATBOT_RESPONSES, MEME_TEMPLATES | |
def create_meme_image(template_info, top_text, bottom_text): | |
"""๋ฐ ์ด๋ฏธ์ง ์์ฑ""" | |
try: | |
# ๊ธฐ๋ณธ ์บ๋ฒ์ค ์์ฑ (์ค์ ํ๊ฒฝ์์๋ ํ ํ๋ฆฟ ์ด๋ฏธ์ง ๋ก๋) | |
width, height = 500, 400 | |
img = Image.new('RGB', (width, height), color='white') | |
draw = ImageDraw.Draw(img) | |
# ํฐํธ ์ค์ (๊ธฐ๋ณธ ํฐํธ ์ฌ์ฉ) | |
try: | |
font_large = ImageFont.truetype("arial.ttf", 32) | |
font_small = ImageFont.truetype("arial.ttf", 24) | |
except: | |
font_large = ImageFont.load_default() | |
font_small = ImageFont.load_default() | |
# ๋ฐฐ๊ฒฝ ๊ทธ๋ผ๋ฐ์ด์ ํจ๊ณผ | |
for y in range(height): | |
color_value = int(255 * (y / height)) | |
color = (color_value, color_value, 255) | |
draw.line([(0, y), (width, y)], fill=color) | |
# ํ ์คํธ ์ถ๊ฐ | |
if top_text: | |
# ์๋จ ํ ์คํธ | |
text_width = draw.textlength(top_text, font=font_large) | |
x = (width - text_width) // 2 | |
y = 30 | |
# ํ ์คํธ ์ธ๊ณฝ์ | |
for dx in [-1, 0, 1]: | |
for dy in [-1, 0, 1]: | |
if dx != 0 or dy != 0: | |
draw.text((x+dx, y+dy), top_text, font=font_large, fill='black') | |
draw.text((x, y), top_text, font=font_large, fill='white') | |
if bottom_text: | |
# ํ๋จ ํ ์คํธ | |
text_width = draw.textlength(bottom_text, font=font_large) | |
x = (width - text_width) // 2 | |
y = height - 70 | |
# ํ ์คํธ ์ธ๊ณฝ์ | |
for dx in [-1, 0, 1]: | |
for dy in [-1, 0, 1]: | |
if dx != 0 or dy != 0: | |
draw.text((x+dx, y+dy), bottom_text, font=font_large, fill='black') | |
draw.text((x, y), bottom_text, font=font_large, fill='white') | |
return img | |
except Exception as e: | |
print(f"์ด๋ฏธ์ง ์์ฑ ์ค๋ฅ: {e}") | |
return create_error_image(str(e)) | |
def create_error_image(error_msg): | |
"""์๋ฌ ์ด๋ฏธ์ง ์์ฑ""" | |
img = Image.new('RGB', (400, 300), color='red') | |
draw = ImageDraw.Draw(img) | |
try: | |
font = ImageFont.load_default() | |
draw.text((50, 150), f"์ค๋ฅ: {error_msg}", font=font, fill='white') | |
except: | |
pass | |
return img | |
def add_text_to_image(img, text, position, font_size=32, color='white'): | |
"""์ด๋ฏธ์ง์ ํ ์คํธ ์ถ๊ฐ""" | |
draw = ImageDraw.Draw(img) | |
try: | |
font = ImageFont.truetype("arial.ttf", font_size) | |
except: | |
font = ImageFont.load_default() | |
x, y = position | |
# ์ธ๊ณฝ์ ํจ๊ณผ | |
for dx in [-1, 0, 1]: | |
for dy in [-1, 0, 1]: | |
if dx != 0 or dy != 0: | |
draw.text((x+dx, y+dy), text, font=font, fill='black') | |
draw.text((x, y), text, font=font, fill=color) | |
return img | |
def generate_response(message, qa_pipeline=None, text_generator=None): | |
"""์ฑ๋ด ์๋ต ์์ฑ""" | |
message_lower = message.lower() | |
# ์ธ์ฌ๋ง ์ฒ๋ฆฌ | |
if any(greeting in message_lower for greeting in ['์๋ ', 'ํ์ด', 'ํฌ๋ก', '๋ฐ๊ฐ']): | |
return random.choice(CHATBOT_RESPONSES["greetings"]) | |
# ๋ฐ ๊ด๋ จ ์ง๋ฌธ ์ฒ๋ฆฌ | |
if any(word in message_lower for word in ['๋ฐ', 'meme', '์ด๋ฏธ์ง', '์์ฑ']): | |
return random.choice(CHATBOT_RESPONSES["meme_help"]) | |
# AI ๋ชจ๋ธ์ ์ฌ์ฉํ ์๋ต ์์ฑ ์๋ | |
if qa_pipeline and len(message) > 10: | |
try: | |
# ๊ฐ๋จํ ์ปจํ ์คํธ๋ก QA ์ํ | |
context = "์ด๊ฒ์ ๋ฐ ์์ฑ๊ณผ AI ์ฑํ ์๋น์ค์ ๋๋ค. ์ฌ์ฉ์๋ค์ด ์ฌ๋ฏธ์๋ ๋ฐ์ ๋ง๋ค๊ณ AI์ ๋ํํ ์ ์์ต๋๋ค." | |
result = qa_pipeline(question=message, context=context) | |
if result and 'answer' in result: | |
return f"๐ค {result['answer']}" | |
except Exception as e: | |
print(f"QA ํ์ดํ๋ผ์ธ ์ค๋ฅ: {e}") | |
# ํ ์คํธ ์์ฑ ๋ชจ๋ธ ์ฌ์ฉ ์๋ | |
if text_generator: | |
try: | |
result = text_generator(f"์ฌ์ฉ์: {message}\n๋ด:", max_length=50, num_return_sequences=1) | |
if result and len(result) > 0: | |
generated_text = result[0]['generated_text'] | |
bot_response = generated_text.split('๋ด:')[-1].strip() | |
if bot_response and len(bot_response) > 5: | |
return f"๐ค {bot_response}" | |
except Exception as e: | |
print(f"ํ ์คํธ ์์ฑ ์ค๋ฅ: {e}") | |
# ๊ธฐ๋ณธ ์๋ต | |
return random.choice(CHATBOT_RESPONSES["default"]) | |
def validate_input(text, max_length=100): | |
"""์ ๋ ฅ ํ ์คํธ ๊ฒ์ฆ""" | |
if not text or not text.strip(): | |
return False, "ํ ์คํธ๋ฅผ ์ ๋ ฅํด์ฃผ์ธ์." | |
if len(text) > max_length: | |
return False, f"ํ ์คํธ๊ฐ ๋๋ฌด ๊น๋๋ค. {max_length}์ ์ดํ๋ก ์ ๋ ฅํด์ฃผ์ธ์." | |
# ํน์๋ฌธ์ ํํฐ๋ง (์ ํ์ ) | |
forbidden_chars = ['<', '>', '&', '"', "'"] | |
if any(char in text for char in forbidden_chars): | |
return False, "ํ์ฉ๋์ง ์๋ ํน์๋ฌธ์๊ฐ ํฌํจ๋์ด ์์ต๋๋ค." | |
return True, "๊ฒ์ฆ ํต๊ณผ" | |
def get_random_meme_idea(): | |
"""๋๋ค ๋ฐ ์์ด๋์ด ์ ๊ณต""" | |
ideas = [ | |
("When you finish your project", "But remember you didn't write tests"), | |
("Me: I'll go to bed early tonight", "Also me at 3 AM:"), | |
("CSS in theory", "CSS in practice"), | |
("My code works", "I have no idea why"), | |
("Expected behavior", "Actual behavior") | |
] | |
return random.choice(ideas) | |
def format_trend_data(keyword, data=None): | |
"""ํธ๋ ๋ ๋ฐ์ดํฐ ํฌ๋งทํ """ | |
if not data: | |
# ์๋ฎฌ๋ ์ด์ ๋ฐ์ดํฐ ์์ฑ | |
popularity = random.randint(60, 95) | |
growth = random.randint(-20, 50) | |
related_terms = random.sample([ | |
'๋ฐ์ด๋ด', '์ธ๊ธฐ', 'ํธ๋ ๋', '์๊ธด', '์ฌ๋ฏธ์๋', | |
'ํซ์ด์', 'ํ์ ', '๋ฐ', 'ํจ๋ฌ๋', '์ ๋จธ' | |
], 3) | |
result = f""" | |
๐ '{keyword}' ํธ๋ ๋ ๋ถ์ ๊ฒฐ๊ณผ | |
๐ฅ ์ธ๊ธฐ๋: {popularity}/100 | |
๐ ์ฑ์ฅ๋ฅ : {growth:+d}% | |
๐ท๏ธ ๊ด๋ จ ํค์๋: {', '.join(related_terms)} | |
โฐ ๋ถ์ ์๊ฐ: ๋ฐฉ๊ธ ์ | |
๐ก ์ถ์ฒ: {'์์น ํธ๋ ๋์ ๋๋ค!' if growth > 0 else '๊ด์ฌ์ ๋ ์ ์๋ ํค์๋์ ๋๋ค.'} | |
""" | |
return result.strip() | |
return str(data) | |