import gradio as gr from transformers import pipeline import random from PIL import Image, ImageDraw, ImageFont import io # 모델 초기화 try: qa_pipeline = pipeline("question-answering") except: qa_pipeline = None def create_meme(template_choice, top_text, bottom_text): """밈 이미지 생성""" try: # 간단한 밈 이미지 생성 width, height = 500, 400 img = Image.new('RGB', (width, height), color='lightblue') draw = ImageDraw.Draw(img) # 텍스트 추가 if top_text: draw.text((50, 50), top_text, fill='black') if bottom_text: draw.text((50, 350), bottom_text, fill='black') return img except Exception as e: return f"오류: {str(e)}" def chatbot_response(message, history): """챗봇 응답""" responses = [ "흥미로운 질문이네요!", "더 자세히 말씀해주세요.", "그것에 대해 생각해보겠습니다.", "좋은 아이디어입니다!" ] return random.choice(responses) # Gradio 인터페이스 with gr.Blocks(title="밈 생성 & 챗봇") as demo: gr.Markdown("# 🎭 밈 생성 & AI 챗봇 서비스") with gr.Tabs(): # 밈 생성 탭 with gr.TabItem("🖼️ 밈 생성기"): with gr.Row(): with gr.Column(): template_dropdown = gr.Dropdown( choices=["드레이크 밈", "디스트랙티드 보이프렌드", "원 두스 낫 심플리"], label="밈 템플릿 선택", value="드레이크 밈" ) top_text = gr.Textbox(label="상단 텍스트", value="When you see a bug") bottom_text = gr.Textbox(label="하단 텍스트", value="But it works in production") create_btn = gr.Button("밈 생성하기", variant="primary") with gr.Column(): meme_output = gr.Image(label="생성된 밈") create_btn.click(create_meme, [template_dropdown, top_text, bottom_text], meme_output) # 챗봇 탭 with gr.TabItem("🤖 AI 챗봇"): chatbot = gr.Chatbot(label="AI 어시스턴트", height=400) msg = gr.Textbox(label="메시지 입력", placeholder="궁금한 것을 물어보세요...") def user_msg(message, history): return "", history + [[message, None]] def bot_msg(history): if history and history[-1][1] is None: user_message = history[-1][0] bot_response = chatbot_response(user_message, history) history[-1][1] = bot_response return history msg.submit(user_msg, [msg, chatbot], [msg, chatbot]).then(bot_msg, chatbot, chatbot) if __name__ == "__main__": demo.launch()