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

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

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

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

By Bongho, Lee
고객 경험이란 무엇일까?

고객 경험이란 무엇일까?

고객경험이란 무엇일까? 1. 과거 어느 대형 프로젝트에서 있던 일이다. 신사업을 위해서 예측 모델 값을 제공해야 하는 상황이었다. 데이터도 없고,어느정도의 정확도를 제공해야 하는지 답이 없었다. 점추정을 할 것인가? 구간 추정을 할 것인가를 가지고 논의중이었다. Product Manager 줄기차게 고객경험을 내세우며 점추정으로 해야 한다고 주장하였다. 근거는 오롯이 "고객 경험"이었다.

By Bongho, Lee
수요예측, 수정구슬이 아닌 목표를 향한 냉정한 나침반

수요예측, 수정구슬이 아닌 목표를 향한 냉정한 나침반

수요예측의 정의와 비즈니스에서의 중요성 기업의 성장과 운영 효율화를 위해 **수요예측(Demand Forecasting)**은 선택이 아닌 필수 요소로 자리 잡았다. 많은 경영진들이 수요예측을 미래 판매량을 정확히 맞히는 '예언'으로 기대하지만, 이는 수요예측의 본질을 오해하는 것이다. 수요예측의 진짜 의미: 미래를 점치는 수정구슬이 아니라, 우리가 도달해야 할 '목표'를

By Bongho, Lee