딥러닝 - 11

CYSSSSSSSSS·2023년 9월 7일
0

딥러닝

목록 보기
11/12

딥러닝 앱 만들기

프로그래밍 vs 딥러닝

  • 현재에 유용한 무언가를 만들기 위한 굉장히 좋은 도구(프로그래밍)

  • 앞으로 유용한 무언가를 만들기 위해 매우 필요한 도구(딥러닝)

  • 함수를 만드는 프로그래밍

  • 모델을 만드는 딥러닝

전통적인 APP 만들기

  • 직접 작성하지 않는다
  • 필요한 함수에 라이브러리 프레임워크를 찾는다
  • 함수 방법을 익힌다
  • 필요한 함수로 app 을 만든다.

딥러닝 APP 만들기

  • 직접 모든 함수를 작성하지 않는다
  • 앱을 만들기 위한 필요한 모델을 모아놓은 라이브러리/프레임워크를 찾는다
  • 라이브러리 /프레임워크 에서 필요한 모델 이용하는방법을 익힌다
  • 필요한 모델을 이용하여 app을 완성한다.

hugging face vs open ai

hugging face
hugging face

gradio
gradio

hugging face

#필요한 라이브러리 설치 
pip install transformers datasets xformers -q

#파이프라인(pipeline)
# 모델을 사용하고 가장 쉽고 빠른 방법
# 자연어처리 , 음성인식 , 컴퓨터 비전 및 멀티모달등 다양한 작업에 사용
# 이용 방법은 작업 선택 - 모델 선택 - 모델 이용 

# 감정분석
from transformers import pipeline

classifier = pipeline('sentiment-analysis')
classifier("good")
classifier(["good","not hate"])

# 텍스트 생성 (한글)
# text generation


generator = pipeline('text-generation','skt/kogpt2-base-v2')
result = generator("옛날 옛적에")
print(result)

# 텍스트 생성 영어
# text generation


generator = pipeline('text-generation')
result = generator("Once upon a time")
print(result)
# image classfication


img="https://huggingface.co/datasets/Narsil/image_dummy/raw/main/parrots.png"

classifier = pipeline("image-classification")
result = classifier(img)
print(result)

# image to text
caption = pipeline('image-to-text')
result = caption(caption)
print(result)


# speech to text
from datasets import load_dataset
dataset = load_dataset("PolyAI/minds14", name="en-US" , split = "train")


audio = dataset[0]['path'] 
# speech to text
recognizer = pipeline("automatic-speech-recognition")
result = recognizer(audio)
print(result)

Gradio- 앱 만들기

  • 딥러닝 엔지니어가 앱을 쉽게 만들수 있도록 도와주는 프레임워크
!pip install gradio

import gradio as gr

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

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

import gradio as gr

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

demo = gr.Interface(fn=greet, inputs="text", outputs="text")
    
demo.launch( share = True , debug = True)   

  • share True 를 하면 다른 사람들과 72시간동안 app을 공유 할수 있다.
import gradio as gr

def greet(name):
    classifier = pipeline("sentiment-analysis" , "matthewburke/korean_sentiment")
    result = classifier(name)
    d = {
        'LABEL_1':'긍정',
        'LABEL_0':'부정',
    }
    answer = d[result[0]['label']]
    return answer

demo = gr.Interface(fn=greet, inputs="text", outputs="text")
    
demo.launch( share = True , debug = True)   
  • gradio + hugging face 를 사용하여 앱을 만들수 도 있다.

chatbot 제작 실습

import gradio as gr

ch = pipeline("text-generation",max_length = 1000 , model = 'EasthShin/Youth_Chatbot_Kogpt2-base') # model
def chat(msg , history):
    result = ch(msg) #model 적용 
    print(result)
    history.append((msg , result[0]['generated_text']))
    return "",history

with gr.Blocks() as demo:
    chatbot = gr.Chatbot()
    msg = gr.Textbox()
    clear = gr.ClearButton([msg , chatbot])
    msg.submit(chat , [msg , chatbot],[msg , chatbot])
  

demo.launch(debug = True)
profile
개발자 되고 싶어요

0개의 댓글