1234 / app.py
Kims12's picture
Update app.py
7cd814b verified
import gradio as gr
import pandas as pd
from transformers import AutoTokenizer, AutoModelForCausalLM
import os
# Hugging Face API 토큰을 ν™˜κ²½ λ³€μˆ˜λ‘œ μ„€μ • (둜그인 ν›„ 토큰을 얻은 ν›„ μ‚¬μš©)
os.environ["HF_TOKEN"] = "your_huggingface_token"
# Parquet 파일 λ‘œλ“œ
data_file_path = "train-00000-of-00001-7f15f39e4c3a7ee9.parquet" # 파일이 λ™μΌν•œ 디렉토리에 μžˆλ‹€κ³  κ°€μ •
dataset = pd.read_parquet(data_file_path)
# λͺ¨λΈκ³Ό ν† ν¬λ‚˜μ΄μ € λ‘œλ“œ
model_id = "CohereForAI/c4ai-command-r-plus-08-2024"
tokenizer = AutoTokenizer.from_pretrained(model_id, use_auth_token=os.getenv("HF_TOKEN"))
model = AutoModelForCausalLM.from_pretrained(model_id, use_auth_token=os.getenv("HF_TOKEN"))
# 질문-λ‹΅λ³€ 데이터셋 (ν•„λ“œ 이름이 'Question', 'Answer'라고 κ°€μ •)
questions = dataset['Question'].tolist()
answers = dataset['Answer'].tolist()
# LLM을 μ‚¬μš©ν•˜μ—¬ μ§ˆλ¬Έμ— λŒ€ν•œ λ‹΅λ³€ 생성
def get_answer(user_question):
# μž…λ ₯ λ©”μ‹œμ§€λ₯Ό μ±„νŒ… ν…œν”Œλ¦Ώμ— 맞좰 ν¬λ§·νŒ…
messages = [{"role": "user", "content": user_question}]
input_ids = tokenizer.apply_chat_template(messages, tokenize=True, add_generation_prompt=True, return_tensors="pt")
# λ‹΅λ³€ 생성
gen_tokens = model.generate(
input_ids,
max_new_tokens=100,
do_sample=True,
temperature=0.3,
)
# μƒμ„±λœ ν…μŠ€νŠΈ λ””μ½”λ”©
gen_text = tokenizer.decode(gen_tokens[0])
return gen_text
# Gradio UI ꡬ성
iface = gr.Interface(
fn=get_answer, # μ‹€ν–‰ν•  ν•¨μˆ˜
inputs="text", # μž…λ ₯은 ν…μŠ€νŠΈ
outputs="text", # 좜λ ₯은 ν…μŠ€νŠΈ
title="의료 상담 AI",
description="μ§ˆλ¬Έμ„ μž…λ ₯ν•˜κ³  μ‹€ν–‰ λ²„νŠΌμ„ λˆ„λ₯΄λ©΄ LLM을 μ‚¬μš©ν•΄ 닡변을 μƒμ„±ν•©λ‹ˆλ‹€.",
)
# UI μ‹€ν–‰
iface.launch()