course documentation

သင်၏ ပထမဆုံး Demo ကို တည်ဆောက်ခြင်း

Hugging Face's logo
Join the Hugging Face community

and get access to the augmented documentation experience

to get started

သင်၏ ပထမဆုံး Demo ကို တည်ဆောက်ခြင်း

Ask a Question Open In Colab Open In Studio Lab

Gradio ကို install လုပ်ခြင်းဖြင့် စတင်ကြရအောင်။ ဒါက Python package တစ်ခုဖြစ်တာကြောင့်၊ ရိုးရှင်းစွာ အောက်ပါ command ကို run လိုက်ပါ။

$ pip install gradio

သင် Gradio ကို ဘယ်နေရာကနေမဆို run နိုင်ပါတယ်၊ သင်အကြိုက်ဆုံး Python IDE ကနေ၊ Jupyter notebooks တွေအထိ ဒါမှမဟုတ် Google Colab မှာပါ run နိုင်ပါတယ် 🤯! ဒါကြောင့် Gradio ကို Python run တဲ့ ဘယ်နေရာမှာမဆို install လုပ်ပါ။

Gradio syntax နဲ့ ရင်းနှီးစေဖို့ ရိုးရှင်းတဲ့ “Hello World” ဥပမာတစ်ခုနဲ့ စတင်ကြရအောင်။

import gradio as gr


def greet(name):
    return "Hello " + name


demo = gr.Interface(fn=greet, inputs="text", outputs="text")

demo.launch()

အပေါ်က code ကို တစ်ဆင့်ချင်းစီ ရှင်းပြပါမယ်။

  • ပထမဆုံး၊ ကျွန်တော်တို့ greet() လို့ခေါ်တဲ့ function တစ်ခုကို define လုပ်ပါတယ်။ ဒီကိစ္စမှာ၊ ဒါက သင့်နာမည်ရှေ့မှာ “Hello” ထည့်ပေးတဲ့ ရိုးရှင်းတဲ့ function တစ်ခုဖြစ်ပေမယ့်၊ ယေဘုယျအားဖြင့် မည်သည့် Python function မဆို ဖြစ်နိုင်ပါတယ်။ ဥပမာ၊ machine learning applications တွေမှာ၊ ဒီ function က input တစ်ခုပေါ်မှာ prediction လုပ်ဖို့ model ကို ခေါ်ပြီး output ကို ပြန်ပေးပါလိမ့်မယ်။
  • ပြီးရင်၊ fn, inputs, နဲ့ outputs ဆိုတဲ့ arguments သုံးခုနဲ့ Gradio Interface တစ်ခုကို ကျွန်တော်တို့ ဖန်တီးပါတယ်။ ဒီ arguments တွေက prediction function ကို define လုပ်ပြီး၊ ကျွန်တော်တို့ လိုချင်တဲ့ input နဲ့ output components တွေရဲ့ type ကိုပါ သတ်မှတ်ပေးပါတယ်။ ကျွန်တော်တို့ရဲ့ ကိစ္စမှာတော့ components နှစ်ခုလုံးက ရိုးရှင်းတဲ့ text boxes တွေပါ။
  • ပြီးတော့ ကျွန်တော်တို့ ဖန်တီးထားတဲ့ Interface ပေါ်မှာ launch() method ကို ခေါ်ပါတယ်။

ဒီ code ကို run မယ်ဆိုရင်၊ အောက်က interface ဟာ Jupyter/Colab notebook ထဲမှာ အလိုအလျောက် ပေါ်လာပါလိမ့်မယ်၊ ဒါမှမဟုတ် script ကနေ run တယ်ဆိုရင် http://localhost:7860 မှာ browser ထဲမှာ ပေါ်လာပါလိမ့်မယ်။

သင့်နာမည် ဒါမှမဟုတ် အခြား input အချို့နဲ့ ဒီ GUI ကို အခုပဲ စမ်းသုံးကြည့်ပါ။

ဒီ GUI မှာ Gradio က input parameter (name) ရဲ့ နာမည်ကို အလိုအလျောက် ခန့်မှန်းပြီး textbox ရဲ့ အပေါ်မှာ label အဖြစ် အသုံးပြုထားတာကို သင်သတိထားမိပါလိမ့်မယ်။ အဲဒါကို ပြောင်းလဲချင်ရင် ဘယ်လိုလုပ်မလဲ။ ဒါမှမဟုတ် textbox ကို အခြားနည်းလမ်းနဲ့ စိတ်ကြိုက်ပြင်ဆင်ချင်ရင်ရော။ ဒီလိုအခြေအနေမှာတော့ input component ကို ကိုယ်စားပြုတဲ့ class object တစ်ခုကို instantiate လုပ်နိုင်ပါတယ်။

အောက်က ဥပမာကို ကြည့်လိုက်ပါ။

import gradio as gr


def greet(name):
    return "Hello " + name


# ကျွန်တော်တို့ Textbox class ကို instantiate လုပ်ပါတယ်
textbox = gr.Textbox(label="Type your name here:", placeholder="John Doe", lines=2)

gr.Interface(fn=greet, inputs=textbox, outputs="text").launch()

ဒီနေရာမှာ၊ ကျွန်တော်တို့ဟာ label တစ်ခု၊ placeholder တစ်ခုနဲ့ သတ်မှတ်ထားတဲ့ lines အရေအတွက်ပါဝင်တဲ့ input textbox တစ်ခုကို ဖန်တီးခဲ့ပါတယ်။ output textbox အတွက်လည်း အလားတူ လုပ်ဆောင်နိုင်ပေမယ့်၊ အခုတော့ ဒါကို ချန်ထားခဲ့ပါမယ်။

code လိုင်းအနည်းငယ်နဲ့ Gradio က မည်သည့် function မဆိုအတွက် မည်သည့် inputs သို့မဟုတ် outputs အမျိုးအစားမဆိုနဲ့ ရိုးရှင်းတဲ့ interface တစ်ခုကို ဖန်တီးနိုင်စေတယ်ဆိုတာ ကျွန်တော်တို့ တွေ့ခဲ့ရပါတယ်။ ဒီအပိုင်းမှာ၊ ကျွန်တော်တို့ဟာ ရိုးရှင်းတဲ့ textbox နဲ့ စတင်ခဲ့ပေမယ့်၊ နောက်အပိုင်းတွေမှာတော့ အခြား inputs နဲ့ outputs အမျိုးအစားတွေကို ဖော်ပြသွားမှာပါ။ Gradio application တစ်ခုထဲမှာ NLP အချို့ကို ထည့်သွင်းတာကို အခု ကြည့်ရအောင်။

🤖 Model Predictions များကို ထည့်သွင်းခြင်း

အခု text-generation model တစ်ခုဖြစ်တဲ့ GPT-2 လိုမျိုး model ကို demo ပြသနိုင်မယ့် ရိုးရှင်းတဲ့ interface တစ်ခုကို တည်ဆောက်ကြရအောင်။

ကျွန်တော်တို့ရဲ့ model ကို 🤗 Transformers က pipeline() function ကို အသုံးပြုပြီး load လုပ်ပါမယ်။ လျင်မြန်စွာ ပြန်လည်လေ့လာဖို့ လိုအပ်ရင် Chapter 1 မှာပါတဲ့ အဲဒီအပိုင်းကို ပြန်သွားနိုင်ပါတယ်။

ပထမဆုံး၊ text prompt တစ်ခုကို input အဖြစ်ယူပြီး text completion ကို ပြန်ပေးတဲ့ prediction function တစ်ခုကို ကျွန်တော်တို့ define လုပ်ပါတယ်။

from transformers import pipeline

model = pipeline("text-generation")


def predict(prompt):
    completion = model(prompt)[0]["generated_text"]
    return completion

ဒီ function က သင်ပေးတဲ့ prompts တွေကို ဖြည့်စွက်ပေးပြီး၊ ဒါဘယ်လိုအလုပ်လုပ်လဲဆိုတာကို ကြည့်ဖို့ သင့်ကိုယ်ပိုင် input prompts တွေနဲ့ run နိုင်ပါတယ်။ ဒီနေရာမှာ ဥပမာတစ်ခု (သင်ကွဲပြားတဲ့ completion တစ်ခု ရနိုင်ပါတယ်)-

predict("My favorite programming language is")
>> My favorite programming language is Haskell. I really enjoyed the Haskell language, but it doesn't have all the features that can be applied to any other language. For example, all it does is compile to a byte array.

အခု ကျွန်တော်တို့ predictions တွေ ထုတ်လုပ်ဖို့ function တစ်ခု ရှိပြီဆိုတော့၊ အရင်က လုပ်ခဲ့တဲ့အတိုင်း Interface တစ်ခုကို ဖန်တီးပြီး launch လုပ်နိုင်ပါပြီ-

import gradio as gr

gr.Interface(fn=predict, inputs="text", outputs="text").launch()

ဒါပါပဲ! အခု သင်ဟာ အောက်မှာ ပြသထားတဲ့အတိုင်း GPT-2 model ကို အသုံးပြုပြီး text တွေကို generate လုပ်ဖို့ ဒီ interface ကို အသုံးပြုနိုင်ပါပြီ 🤯။

Gradio နဲ့ အခြား demos အမျိုးအစားတွေကို ဘယ်လိုတည်ဆောက်ရမယ်ဆိုတာကို ဆက်ဖတ်ပါ။

ဝေါဟာရ ရှင်းလင်းချက် (Glossary)

  • Gradio: Python library တစ်ခုဖြစ်ပြီး machine learning မော်ဒယ်တွေအတွက် အသုံးပြုရလွယ်ကူတဲ့ web interface တွေ ဒါမှမဟုတ် demo တွေကို အလွယ်တကူ ဖန်တီးနိုင်စေပါတယ်။
  • Python Package: Python programming language တွင် အသုံးပြုရန်အတွက် စုစည်းထားသော modules နှင့် code များ။
  • pip install gradio: Gradio library ကို install လုပ်ရန်အတွက် command line command။
  • Python IDE (Integrated Development Environment): Python code ရေးရန်၊ run ရန်နှင့် debug လုပ်ရန်အတွက် ကိရိယာများပါဝင်သော software application။
  • Jupyter Notebooks: code, text, images, နှင့် mathematical equations တို့ကို ပေါင်းစပ်နိုင်သော interactive computing environment။
  • Google Colab: Google မှ ပံ့ပိုးပေးထားသော cloud-based Jupyter Notebook environment တစ်ခုဖြစ်ပြီး Python code များကို web browser မှတစ်ဆင့် run နိုင်စေသည်။
  • “Hello World” Example: ပရိုဂရမ်းမင်းဘာသာစကားတစ်ခုကို စတင်လေ့လာရာတွင် အခြေခံအကျဆုံးနှင့် အရိုးရှင်းဆုံး ပရိုဂရမ် (မျက်နှာပြင်ပေါ်တွင် “Hello World” ဟု ပြသခြင်း)။
  • Gradio Syntax: Gradio library ကို အသုံးပြုရန်အတွက် code ရေးသားပုံ စည်းမျဉ်းများ။
  • import gradio as gr: Gradio library ကို gr ဟူသော အတိုကောက်ဖြင့် import လုပ်ခြင်း။
  • greet() Function: ဥပမာတွင် အသုံးပြုသော Python function တစ်ခု။
  • Prediction Function: Machine Learning မော်ဒယ်တစ်ခုက input data ကို အခြေခံပြီး ခန့်မှန်းချက်များ ပြုလုပ်ရန် ဒီဇိုင်းထုတ်ထားသော function။
  • gr.Interface: Gradio တွင် web-based UI (User Interface) ကို ဖန်တီးရန်အတွက် class။
  • fn Argument: gr.Interface တွင် prediction function ကို သတ်မှတ်ရန် အသုံးပြုသော argument။
  • inputs Argument: gr.Interface တွင် input component ၏ အမျိုးအစား သို့မဟုတ် object ကို သတ်မှတ်ရန် အသုံးပြုသော argument။
  • outputs Argument: gr.Interface တွင် output component ၏ အမျိုးအစား သို့မဟုတ် object ကို သတ်မှတ်ရန် အသုံးပြုသော argument။
  • Input Components: အသုံးပြုသူက data များကို ထည့်သွင်းနိုင်သော UI အစိတ်အပိုင်းများ (ဥပမာ- textbox, image uploader)။
  • Output Components: Model ၏ ရလဒ်များကို ပြသနိုင်သော UI အစိတ်အပိုင်းများ (ဥပမာ- textbox, image display)။
  • Text Boxes: စာသားထည့်သွင်းရန် သို့မဟုတ် ပြသရန်အတွက် UI component။
  • launch() Method: Gradio Interface ကို စတင်ပြီး web browser တွင် ပြသရန်အတွက် method။
  • Jupyter/Colab Notebook: interactive code execution environment။
  • GUI (Graphical User Interface): အသုံးပြုသူများနှင့် ဂရပ်ဖစ်ပုံစံဖြင့် အပြန်အလှန်တုံ့ပြန်နိုင်သော ဆော့ဖ်ဝဲလ် interface။
  • Input Parameter: Function တစ်ခုသို့ ပေးပို့သော data (အချက်အလက်)။
  • Label: UI component တစ်ခု၏ အပေါ်တွင် ပြသထားသော ဖော်ပြချက်။
  • Textbox Class (gr.Textbox): Gradio တွင် စိတ်ကြိုက်ပြင်ဆင်နိုင်သော textbox input/output component ကို ဖန်တီးရန် class။
  • label (Textbox argument): textbox အတွက် label ကို သတ်မှတ်ရန်။
  • placeholder (Textbox argument): textbox တွင် input မထည့်မီ ပြသထားသော ဥပမာစာသား။
  • lines (Textbox argument): textbox ၏ မြင်နိုင်သော lines အရေအတွက်။
  • NLP (Natural Language Processing): ကွန်ပျူတာတွေ လူသားဘာသာစကားကို နားလည်၊ အဓိပ္ပာယ်ဖော်ပြီး၊ ဖန်တီးနိုင်အောင် လုပ်ဆောင်ပေးတဲ့ Artificial Intelligence (AI) ရဲ့ နယ်ပယ်ခွဲတစ်ခုပါ။
  • Text Generation Model: input text ကို အခြေခံပြီး ဆက်စပ်နေသော စာသားအသစ်များကို ထုတ်လုပ်နိုင်သော model။
  • GPT-2: OpenAI မှ ဖန်တီးထားသော ထင်ရှားသည့် text generation model တစ်ခု။
  • pipeline() Function: Hugging Face Transformers library မှာ ပါဝင်တဲ့ လုပ်ဆောင်ချက်တစ်ခုဖြစ်ပြီး မော်ဒယ်တွေကို သီးခြားလုပ်ငန်းတာဝန်များ (ဥပမာ- စာသားခွဲခြားသတ်မှတ်ခြင်း၊ စာသားထုတ်လုပ်ခြင်း) အတွက် အသုံးပြုရလွယ်ကူအောင် ပြုလုပ်ပေးပါတယ်။
  • 🤗 Transformers: Hugging Face က ထုတ်လုပ်ထားတဲ့ library တစ်ခုဖြစ်ပြီး Transformer မော်ဒယ်တွေကို အသုံးပြုပြီး Natural Language Processing (NLP), computer vision, audio processing စတဲ့ နယ်ပယ်တွေမှာ အဆင့်မြင့် AI မော်ဒယ်တွေကို တည်ဆောက်ပြီး အသုံးပြုနိုင်စေပါတယ်။
  • Text Prompt: text generation model သို့ ထည့်သွင်းသော စတင်စာသား။
  • Text Completion: text generation model မှ ထုတ်လုပ်သော နောက်ဆက်တွဲစာသား။
  • generated_text: pipeline() function မှ text generation output တွင် ပါဝင်သော generated text field။
Update on GitHub