File size: 3,118 Bytes
4fd3e67 d0a7e74 460508b d0a7e74 460508b d0a7e74 460508b d0a7e74 460508b d0a7e74 460508b 4fd3e67 460508b d0a7e74 460508b d0a7e74 460508b 20308f4 460508b | 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 | 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()
|