|
import gradio as gr |
|
import pandas as pd |
|
from transformers import AutoTokenizer, AutoModelForCausalLM |
|
import os |
|
|
|
|
|
os.environ["HF_TOKEN"] = "your_huggingface_token" |
|
|
|
|
|
|
|
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")) |
|
|
|
|
|
questions = dataset['Question'].tolist() |
|
answers = dataset['Answer'].tolist() |
|
|
|
|
|
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 |
|
|
|
|
|
iface = gr.Interface( |
|
fn=get_answer, |
|
inputs="text", |
|
outputs="text", |
|
title="μλ£ μλ΄ AI", |
|
description="μ§λ¬Έμ μ
λ ₯νκ³ μ€ν λ²νΌμ λλ₯΄λ©΄ LLMμ μ¬μ©ν΄ λ΅λ³μ μμ±ν©λλ€.", |
|
) |
|
|
|
|
|
iface.launch() |
|
|