Gradio와 Steamlit의 코드 및 인터페이스 비교

Gradio와 Steamlit의 코드 및 인터페이스 비교
Photo by Ben Kolde / Unsplash

Gradio

import tensorflow as tf  
import numpy as np  
import gradio as gr  
  
# MNIST 데이터 로드  
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()  
  
# 데이터 전처리  
x_train = x_train.reshape(-1, 28, 28, 1).astype('float32') / 255  
x_test = x_test.reshape(-1, 28, 28, 1).astype('float32') / 255  
  
# 모델 정의  
def create_model():  
    model = tf.keras.Sequential([  
        tf.keras.layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)),  
        tf.keras.layers.MaxPooling2D((2, 2)),  
        tf.keras.layers.Flatten(),  
        tf.keras.layers.Dense(128, activation='relu'),  
        tf.keras.layers.Dense(10, activation='softmax')  
    ])  
    model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])  
    return model  
  
model = create_model()  
  
# 학습 함수  
def train_model():  
    class AccuracyCallback(tf.keras.callbacks.Callback):  
        def __init__(self):  
            self.accuracies = []  
  
        def on_epoch_end(self, epoch, logs=None):  
            self.accuracies.append(logs['accuracy'])  
  
    accuracy_callback = AccuracyCallback()  
  
    history = model.fit(x_train, y_train, epochs=3, validation_split=0.1, verbose=0,   
                        callbacks=[accuracy_callback])  
      
    accuracy_text = "Training accuracies:\n"  
    for epoch, acc in enumerate(accuracy_callback.accuracies, 1):  
        accuracy_text += f"Epoch {epoch}: {acc:.4f}\n"  
    accuracy_text += f"\nFinal accuracy: {accuracy_callback.accuracies[-1]:.4f}"  
    return accuracy_text  
  
# 예측 함수  
def predict():  
    idx = np.random.randint(0, len(x_test))  
    image = x_test[idx]  
    true_label = y_test[idx]  
      
    prediction = model.predict(image[np.newaxis, ...])[0]  
    predicted_label = np.argmax(prediction)  
      
    display_image = (image.reshape(28, 28) * 255).astype(np.uint8)  
      
    return (f"Predicted: {predicted_label}, True: {true_label}", display_image)  
  
# Gradio 인터페이스  
with gr.Blocks() as demo:  
    gr.Markdown("# MNIST 분류기")  
      
    with gr.Row():  
        train_button = gr.Button("학습")  
        output_text = gr.Textbox(label="학습 결과", lines=7)  
      
    with gr.Row():  
        predict_button = gr.Button("예측")  
        output_label = gr.Textbox(label="예측 결과")  
        output_image = gr.Image(label="예측 이미지")  
      
    train_button.click(train_model, outputs=output_text)  
    predict_button.click(predict, outputs=[output_label, output_image])  
  
demo.launch()

Streamlit

import streamlit as st  
import tensorflow as tf  
import numpy as np  
import matplotlib.pyplot as plt  
  
# MNIST 데이터 로드  
@st.cache_resource  
def load_data():  
    return tf.keras.datasets.mnist.load_data()  
  
(x_train, y_train), (x_test, y_test) = load_data()  
  
# 데이터 전처리  
x_train = x_train.reshape(-1, 28, 28, 1).astype('float32') / 255  
x_test = x_test.reshape(-1, 28, 28, 1).astype('float32') / 255  
  
# 모델 정의  
@st.cache_resource  
def create_model():  
    model = tf.keras.Sequential([  
        tf.keras.layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)),  
        tf.keras.layers.MaxPooling2D((2, 2)),  
        tf.keras.layers.Flatten(),  
        tf.keras.layers.Dense(128, activation='relu'),  
        tf.keras.layers.Dense(10, activation='softmax')  
    ])  
    model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])  
    return model  
  
model = create_model()  
  
# 학습 함수  
def train_model():  
    class AccuracyCallback(tf.keras.callbacks.Callback):  
        def on_epoch_end(self, epoch, logs=None):  
            accuracy = logs.get('accuracy')  
            st.session_state.accuracies.append(accuracy)  
            st.session_state.progress_bar.progress((epoch + 1) / 5)  
            st.session_state.status_text.text(f"Epoch {epoch + 1}/5 completed")  
  
    accuracy_callback = AccuracyCallback()  
      
    history = model.fit(x_train, y_train, epochs=5, validation_split=0.1, verbose=0,   
                        callbacks=[accuracy_callback])  
      
    st.session_state.trained = True  
  
# 예측 함수  
def predict():  
    idx = np.random.randint(0, len(x_test))  
    image = x_test[idx]  
    true_label = y_test[idx]  
      
    prediction = model.predict(image[np.newaxis, ...])[0]  
    predicted_label = np.argmax(prediction)  
      
    return image, predicted_label, true_label  
  
# Streamlit 앱  
st.title("MNIST 분류기")  
  
if 'trained' not in st.session_state:  
    st.session_state.trained = False  
    st.session_state.accuracies = []  
  
col1, col2 = st.columns(2)  
  
with col1:  
    if st.button("학습"):  
        st.session_state.accuracies = []  
        st.session_state.progress_bar = st.progress(0)  
        st.session_state.status_text = st.empty()  
        train_model()  
        st.session_state.progress_bar.empty()  
        st.session_state.status_text.empty()  
          
        accuracy_text = "Training accuracies:\n"  
        for epoch, acc in enumerate(st.session_state.accuracies, 1):  
            accuracy_text += f"Epoch {epoch}: {acc:.4f}\n"  
        accuracy_text += f"\nFinal accuracy: {st.session_state.accuracies[-1]:.4f}"  
        st.text_area("학습 결과", accuracy_text, height=200)  
  
with col2:  
    if st.button("예측"):  
        if not st.session_state.trained:  
            st.warning("먼저 모델을 학습시켜주세요.")  
        else:  
            image, predicted_label, true_label = predict()  
            st.image(image.reshape(28, 28), caption=f"Predicted: {predicted_label}, True: {true_label}", width=200)  
  
st.markdown("---")  
st.markdown("학습 버튼을 눌러 모델을 학습시킨 후, 예측 버튼을 눌러 랜덤한 MNIST 이미지에 대한 예측을 확인하세요.")
  • 학습시에는 Progess Bar로 진척도를 보여주다가 종료되면 Epoch별 Metric을 보여주었는데, 예측버튼 클릭시이 부분이 사라졌다

Read more

[책]Reshuffle: Who wins when AI restacks the knowledge economy

[책]Reshuffle: Who wins when AI restacks the knowledge economy

원래는 Amazon에 가서 Personal Knowledge Managment에 관한 책을 사려고 했다. Sketch Your Mind라는 책이었는데, 그 때 이 책 “Reshuffle”을 발견하였다. AI가 어떻게 Knowledge Economy를 흔들 것가? 라는 부제를 훑어보면서 저자가 쓴 다른 책을 보게 되었는데 거기에 내가 좋아했던 책을쓴 저자라는 것을 알게 되었다. 그래서 크게 고민하지 않고 구매를 하고

By Bongho, Lee
[책]올라운드투자, 누군가의 투자일기

[책]올라운드투자, 누군가의 투자일기

“올라운드 투자”라는 제목을 보았을 때는, “올라운드 플레이어”가 생각이 났다. “올라운드”라는 표현을 오랜만에 들어본 까닭이었다. 그럼에도 불구하고 이 책을 고른 것은 저자가 그간 보여준 컨텐츠에 대한 신뢰가 있던 까닭이었다. 컨텐츠를 다양하게 보는 편이지만 깊이가 아주 있지는 않았다. 여기서 깊이라 함은 기존 전문적인 정량적 분석의 내용의 수준을 말하는 것이다.

By Bongho, Lee
내가 놓치고 있던 미래, 먼저 온 미래를 읽고

내가 놓치고 있던 미래, 먼저 온 미래를 읽고

장강명 작가의 책은, 유학시절 읽고 처음이었다. 유학시절 "한국이 싫어서"라는 책은 동기부여가 상당히 되는 책이었다. 한국을 떠나 새로운 정채성을 학생으로서 Build up 해나가고 있던 상황에서 이 책은 제목부터 꽤 솔깃하였다. 물론 결말이 기억날 정도로 인상깊은 책은 아니었지만 말이다. 그렇게 시간이 흘러 장강명 작가의 책은 더 이상 읽지 않던

By Bongho, Lee