Python

Gradio๋กœ AI์ฑ„ํŒ…์ฐฝ ๊ตฌํ˜„ํ•˜๊ธฐ (๋งค์šฐ์‰ฌ์›€)

ZZJJing 2025. 2. 16. 17:37
๋ฐ˜์‘ํ˜•

 

๐Ÿ“Œ Python - Gradio Example Project

์ด ํ”„๋กœ์ ํŠธ๋Š” Gradio๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ„๋‹จํ•œ ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๋งŒ๋“œ๋Š” ์˜ˆ์ œ์ž…๋‹ˆ๋‹ค.

๐Ÿ“Œ ์„ค์น˜ ๋ฐฉ๋ฒ•

์ด ํ”„๋กœ์ ํŠธ๋ฅผ ์‹คํ–‰ํ•˜๋ ค๋ฉด Python๊ณผ pip๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ ๋ช…๋ น์–ด๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ•„์š”ํ•œ ํŒจํ‚ค์ง€๋ฅผ ์„ค์น˜ํ•˜์„ธ์š”:

pip install gradio

๐Ÿ“Œ ์‹คํ–‰ ๋ฐฉ๋ฒ•

*.py ํŒŒ์ผ์„ ์‹คํ–‰ํ•˜์—ฌ Gradio ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค:

python *.py

๐Ÿ“Œ ๊ตฌ๋™๋ฐฉ์‹

  1. Gradio ์„œ๋ฒ„์—์„œ ๊ตฌ๋™ : ์‰์–ด๊ฐ€๋Šฅ์˜ต์…˜์…˜ 72์‹œ๊ฐ„๊นŒ์ง€๋งŒ ์œ ์ง€
  2. HuggingFace(HF) spaces์—์„œ ๊ตฌ๋™ (https://huggingface.co/new-space ์—์„œ remote repo ์ƒ์„ฑ)
  3. ๊ฐœ์ธ ์„œ๋ฒ„์—์„œ ๊ตฌ๋™ ex) demo.launch( server_name="0.0.0.0", server_port=8020, auth=("noname", "passwd1") )

๐Ÿ“Œ ์˜ˆ์ œ ํŒŒ์ผ ์„ค๋ช…

  • basic_interface.py : gr.Interface(), csv ์ €์žฅ ์˜ˆ์ œ
  • basic_chatHistory.py : gr.Chatbot(), textํ˜• history ์˜ˆ์ œ
  • chatInterface.py : gr.ChatInterface() ์‚ฌ์šฉ์˜ˆ์ œ
  • component_test.py : ์ด๋ฏธ์ง€ ์—…๋กœ๋“œ ๋ฐ ์ปดํฌ๋„ŒํŠธ ์‚ฌ์šฉ์˜ˆ์ œ
  • tensorflow_test.py

๐Ÿ” gr.Chatbot() vs gr.ChatInterface() ์ฐจ์ด์  ์ •๋ฆฌ

gr.Chatbot() VS gr.ChatInterface()

์—ญํ•  ์ฑ„ํŒ… UI ์ปดํฌ๋„ŒํŠธ ์™„์„ฑํ˜• ์ฑ„ํŒ… ์ธํ„ฐํŽ˜์ด์Šค
๊ธฐ๋Šฅ ๋ฉ”์‹œ์ง€ ํ‘œ์‹œ + ์‚ฌ์šฉ์ž ์ž…๋ ฅ ์ง์ ‘ ์ฒ˜๋ฆฌ ํ•„์š” ๋‚ด๋ถ€์ ์œผ๋กœ ๋ฉ”์‹œ์ง€ ๊ธฐ๋ก ๊ด€๋ฆฌ, ์ž…๋ ฅ์ฐฝ ํฌํ•จ
๋ฉ”์‹œ์ง€ ํ˜•์‹ ํŠœํ”Œ((user, bot)) ๋˜๋Š” type="messages" ์‚ฌ์šฉ ๊ฐ€๋Šฅ type="messages"(OpenAI ์Šคํƒ€์ผ) ๊ธฐ๋ณธ ์ง€์›
์‚ฌ์šฉ ๋ฐฉ์‹ gr.Chatbot()์„ ์ง์ ‘ ์‚ฌ์šฉํ•˜๊ณ  ์ž…๋ ฅ/์ถœ๋ ฅ์„ ์—ฐ๊ฒฐํ•ด์•ผ ํ•จ ChatInterface(fn)์œผ๋กœ ํ•จ์ˆ˜๋งŒ ์—ฐ๊ฒฐํ•˜๋ฉด ์ž๋™ ์ฒ˜๋ฆฌ
์ปค์Šคํ„ฐ๋งˆ์ด์ง• ์ง์ ‘ ์ž…๋ ฅ์ฐฝ + ๋ฒ„ํŠผ์„ ์ถ”๊ฐ€ํ•ด์•ผ ํ•จ ์ž…๋ ฅ์ฐฝ, ์ „์†ก ๋ฒ„ํŠผ ๊ธฐ๋ณธ ์ œ๊ณต
     

๐Ÿ† ์–ธ์ œ ๋ฌด์—‡์„ ์‚ฌ์šฉํ•ด์•ผ ํ• ๊นŒ?

  • gr.Chatbot() โ†’ ์™„์ „ํ•œ ์ปค์Šคํ„ฐ๋งˆ์ด์ง•์ด ํ•„์š”ํ•  ๋•Œ (์ž…๋ ฅ ๋ฐฉ์‹, UI ์ง์ ‘ ์กฐ์ž‘)
  • gr.ChatInterface() โ†’ ๋น ๋ฅด๊ฒŒ ์ฑ„ํŒ… ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๋งŒ๋“ค๊ณ  ์‹ถ์„ ๋•Œ (์ž๋™ํ™”๋œ ๊ธฐ๋Šฅ ์‚ฌ์šฉ) ๐Ÿ‘‰ ๊ฐ„๋‹จํ•œ ์ฑ—๋ด‡์„ ๋งŒ๋“ค ๋• gr.ChatInterface() ๐Ÿ‘‰ ๊ธฐ๋Šฅ์„ ์ง์ ‘ ์ปจํŠธ๋กคํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด gr.Chatbot()

๐Ÿš€ Gradio vs ๋‹ค๋ฅธ UI ํ”„๋ ˆ์ž„์›Œํฌ ๋น„๊ต

ํ”„๋ ˆ์ž„์›Œํฌ ํŠน์ง• ๋‚œ์ด๋„ ๋จธ์‹ ๋Ÿฌ๋‹ ์นœํ™”๋„

Gradio Python ์ฝ”๋“œ ๋ช‡ ์ค„๋กœ UI ๊ฐ€๋Šฅ, AI ๋ชจ๋ธ ๋ฐฐํฌ ์ตœ์ ํ™” โญ (๋งค์šฐ ์‰ฌ์›€) โญโญโญโญโญ
Streamlit ๋ฐ์ดํ„ฐ ์‹œ๊ฐํ™” ๋ฐ ๋Œ€์‹œ๋ณด๋“œ ์ตœ์ ํ™” โญโญ (์‰ฌ์›€) โญโญโญโญ
Flask/Django ๋ฐฑ์—”๋“œ ์ค‘์‹ฌ์˜ ์›น ํ”„๋ ˆ์ž„์›Œํฌ โญโญโญ (์ค‘๊ฐ„) โญโญ
React/Vue.js ํ”„๋ก ํŠธ์—”๋“œ ์›น ๊ฐœ๋ฐœ โญโญโญโญ (์–ด๋ ค์›€) โญ

๐ŸŽฏ ๊ฒฐ๋ก : Gradio๋ฅผ ์จ์•ผ ํ•˜๋Š” ์ด์œ 

โœ… ์ฝ”๋“œ ๋ช‡ ์ค„๋กœ ์›น UI ๊ฐ€๋Šฅ โœ… ๋จธ์‹ ๋Ÿฌ๋‹ ๋ชจ๋ธ๊ณผ ์‰ฝ๊ฒŒ ์—ฐ๋™ โœ… ๊ณต์œ  (share=True) ๊ธฐ๋Šฅ์œผ๋กœ ๋ฐฐํฌ ํŽธ๋ฆฌ โœ… ๋‹ค์–‘ํ•œ ๋ฐ์ดํ„ฐ ์ž…๋ ฅ/์ถœ๋ ฅ ์ง€์› โœ… Hugging Face, OpenAI, TensorFlow, PyTorch ๋“ฑ๊ณผ ํ˜ธํ™˜

์ฐธ๊ณ ์ž๋ฃŒ

https://www.gradio.app/guides/quickstart https://knowgyu.github.io/posts/G

 

 

Quickstart

A Step-by-Step Gradio Tutorial

www.gradio.app

 


0. ๋กœ์ปฌ ๋ผ๋งˆ ์„ค์น˜ ํ›„ ์ง„ํ–‰ ํ•„์š” 

 

1. gr.Chatbot() ์‚ฌ์šฉ ์˜ˆ์ œ 

import gradio as gr
import ollama

# ํžˆ์Šคํ† ๋ฆฌ ํŒจ๋„ ๊ฐ€์‹œ์„ฑ ์ƒํƒœ
history_visible = True  

# ์ฑ„ํŒ… ์‘๋‹ต ํ•จ์ˆ˜
def chat_with_ai(user_input, history):
    if history is None:
        history = []  # ์ดˆ๊ธฐํ™”

    # ๋ฉ”์‹œ์ง€ ํ˜•์‹ ๋ณ€ํ™˜ (OpenAI ์Šคํƒ€์ผ)
    messages = [{"role": "user", "content": msg[0]} for msg in history] + [{"role": "user", "content": user_input}]
    response = ollama.chat(model="llama3.2-vision", messages=messages)
    bot_reply = response["message"]["content"]

    # 1์ค„ ์š”์•ฝ ๋ฆฌ์ŠคํŠธ์— ์ถ”๊ฐ€ (์ฒซ ์ค„๋งŒ)
    summary = bot_reply.split("\n")[0]  # ์ฒซ ์ค„๋งŒ ์š”์•ฝ
    history.append((user_input, bot_reply))  
    history_list = [f"๐Ÿ‘ค {msg[0]} | ๐Ÿค– {msg[1].split('.')[0]}" for msg in history]  # ์š”์•ฝ ๋ฆฌ์ŠคํŠธ

    return history, history_list  # ์ฑ„ํŒ… UI + ํžˆ์Šคํ† ๋ฆฌ ๋ฆฌ์ŠคํŠธ ๋™๊ธฐํ™”

# ํžˆ์Šคํ† ๋ฆฌ ํŒจ๋„ ํ† ๊ธ€ ํ•จ์ˆ˜
def toggle_history():
    global history_visible
    history_visible = not history_visible
    return gr.update(visible=history_visible)

def main():
    with gr.Blocks() as demo:
        history_state = gr.State([])  # ๐Ÿ”น ํžˆ์Šคํ† ๋ฆฌ ์ €์žฅ์šฉ State

        with gr.Row():
            # ํžˆ์Šคํ† ๋ฆฌ ํŒจ๋„ (ํ† ๊ธ€ ๊ฐ€๋Šฅ)
            with gr.Column(scale=2, visible=history_visible) as history_panel:
                gr.Markdown("### ๐Ÿ“ ์ฑ„ํŒ… ํžˆ์Šคํ† ๋ฆฌ")
                chat_history_list = gr.List(label="ํžˆ์Šคํ† ๋ฆฌ", interactive=False)  # ํžˆ์Šคํ† ๋ฆฌ ๋ฆฌ์ŠคํŠธ UI

            # ๋ฉ”์ธ ์ฑ„ํŒ… ์ธํ„ฐํŽ˜์ด์Šค
            with gr.Column(scale=5):
                chatbot = gr.Chatbot()  
                user_input = gr.Textbox(placeholder="๋ฉ”์‹œ์ง€๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”...")
                send_button = gr.Button("์ „์†ก")

            # ํžˆ์Šคํ† ๋ฆฌ ํ† ๊ธ€ ๋ฒ„ํŠผ
            toggle_button = gr.Button("๐Ÿ“‚ ํžˆ์Šคํ† ๋ฆฌ ํ† ๊ธ€")

        # ์ „์†ก ๋ฒ„ํŠผ ํด๋ฆญ ์‹œ ์‘๋‹ต ์ฒ˜๋ฆฌ
        send_button.click(
            chat_with_ai, 
            inputs=[user_input, history_state], 
            outputs=[chatbot, chat_history_list]  # ๐Ÿ”น ์ฑ„ํŒ… UI + ํžˆ์Šคํ† ๋ฆฌ ๋ฆฌ์ŠคํŠธ ๋™๊ธฐํ™”
        )

        # ๋ฒ„ํŠผ ํด๋ฆญ ์‹œ ํžˆ์Šคํ† ๋ฆฌ ํ† ๊ธ€
        toggle_button.click(toggle_history, None, history_panel)

    demo.launch(share=True)

if __name__ == "__main__":
    main()

 

2. gr.ChatInterface() ์˜ˆ์ œ 

import gradio as gr
import ollama


def chat_with_ai(user_input, history):
    if history is None:
        history = []

    # OpenAI ์Šคํƒ€์ผ ๋ฉ”์‹œ์ง€ ํฌ๋งท ์ ์šฉ
    messages = history + [{"role": "user", "content": user_input}]
    
    # ๋กœ์ปฌ์ธ๊ฒฝ์šฐ 
    response = ollama.chat(model="llama3.2-vision", messages=messages)

    bot_reply = response["message"]["content"] 

    return f"๐Ÿค– : {bot_reply}" 


def main():
    with gr.Blocks() as demo:

        gr.Markdown("### ๐Ÿค– ๋ผ๋งˆ์™€ ๋Œ€ํ™”ํ•˜๊ธฐ")

        chatbot = gr.ChatInterface(
            chat_with_ai,               # โ‘  ์ฑ„ํŒ… ์‘๋‹ต์„ ์ฒ˜๋ฆฌํ•˜๋Š” ํ•จ์ˆ˜
            type="messages",            # โ‘ก ๋ฉ”์‹œ์ง€ ๋ฐ์ดํ„ฐ ํ˜•์‹ ์ง€์ •
            flagging_mode="manual",     # โ‘ข ์ˆ˜๋™ ํ”Œ๋ž˜๊น…(์‹ ๊ณ ) ๊ธฐ๋Šฅ ํ™œ์„ฑํ™”
            flagging_options=["Like", "Spam", "Inappropriate", "Other"],   # โ‘ฃ ์‹ ๊ณ  ์˜ต์…˜ ๋ฆฌ์ŠคํŠธ
            save_history=True,             # โ‘ค ์ฑ„ํŒ… ๊ธฐ๋ก์„ ์ž๋™ ์ €์žฅ
            theme="gradio/monochrome"
        )
            
    # ์‹คํ–‰1 -Gradio ์„œ๋ฒ„ 
    demo.launch(share=True)


if __name__ == "__main__":
    main()

 

728x90
๋ฐ˜์‘ํ˜•