๐ Python - Gradio Example Project
์ด ํ๋ก์ ํธ๋ Gradio๋ฅผ ์ฌ์ฉํ์ฌ ๊ฐ๋จํ ์น ์ ํ๋ฆฌ์ผ์ด์ ์ ๋ง๋๋ ์์ ์ ๋๋ค.
๐ ์ค์น ๋ฐฉ๋ฒ
์ด ํ๋ก์ ํธ๋ฅผ ์คํํ๋ ค๋ฉด Python๊ณผ pip๊ฐ ํ์ํฉ๋๋ค. ๋ค์ ๋ช ๋ น์ด๋ฅผ ์ฌ์ฉํ์ฌ ํ์ํ ํจํค์ง๋ฅผ ์ค์นํ์ธ์:
pip install gradio
๐ ์คํ ๋ฐฉ๋ฒ
*.py ํ์ผ์ ์คํํ์ฌ Gradio ์ธํฐํ์ด์ค๋ฅผ ์์ํฉ๋๋ค:
python *.py
๐ ๊ตฌ๋๋ฐฉ์
- Gradio ์๋ฒ์์ ๊ตฌ๋ : ์์ด๊ฐ๋ฅ์ต์ ์ 72์๊ฐ๊น์ง๋ง ์ ์ง
- HuggingFace(HF) spaces์์ ๊ตฌ๋ (https://huggingface.co/new-space ์์ remote repo ์์ฑ)
- ๊ฐ์ธ ์๋ฒ์์ ๊ตฌ๋ 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()
'Python' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
์ธํ ๋ฆฌ์ ์ด์์ ํ์ด์ฌ ์คํ ํ๊ฒฝ ๊ตฌ์ฑํ๊ธฐ (0) | 2025.02.25 |
---|---|
์ ํ๋ธ mp3์์ ์ถ์ถ (0) | 2025.02.22 |
[Python/OCR] Tessearact ์ค๋ฅ ์ ๋ฆฌ (0) | 2025.02.18 |
(MAC) python3์ python ๋ช ๋ น์ด๋ก ์ค์ ํ๋ ๋ฐฉ๋ฒ (0) | 2025.02.14 |
[ํ์ด์ฌ] ์นํฌ๋กค๋ง์ ํ์ํ BeautifulSoup4 (BS4) ์ค์น (0) | 2021.09.08 |