test / app.py
Manggomee's picture
Upload 4 files
460508b verified
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()