Download app.py from Manggomee/test: direct link, hf CLI and curl.
- Browser
- Download file 3.12 kB
-
https://huggingface.co/spaces/Manggomee/test/resolve/main/app.py
- Command line
-
hf download hf://spaces/Manggomee/test/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Manggomee/test/resolve/main/app.py
3.12 kB
| 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() | |