Spaces:
Sleeping
Sleeping
File size: 6,623 Bytes
487eb4e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
"""
์ ํธ๋ฆฌํฐ ํจ์๋ค
"""
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)
|