ThGptSan / app.py
OzoneAsai's picture
Update app.py
5a7bef8
import os
# コマンドを実行する
os.system("pip install transformers torch psutil")
# コマンドの実行結果を取得する(stdoutとstderrは出力されない)
result = os.system("pip install transformers")
import os
# コマンドを実行する
os.system("pip install transformers torch psutil")
# コマンドの実行結果を取得する(stdoutとstderrは出力されない)
result = os.system("pip install transformers")
from transformers import AutoModel, AutoTokenizer, trainer_utils
import gradio as gr
import psutil
device = "cpu"
model = AutoModel.from_pretrained("Tanrei/GPTSAN-japanese").to(device)
tokenizer = AutoTokenizer.from_pretrained("Tanrei/GPTSAN-japanese")
trainer_utils.set_seed(30)
def get_memory_usage():
process = psutil.Process()
memory_usage = process.memory_info().rss / 1024 / 1024 # メモリ使用量をMB単位で取得
return f"Memory Usage: {memory_usage:.2f} MB"
def generate_text(input_text, num_repeats):
usag=get_memory_usage()
x_token = tokenizer("", prefix_text=input_text, return_tensors="pt")
input_ids = x_token.input_ids.to(device)
token_type_ids = x_token.token_type_ids.to(device)
gen_token = model.generate(input_ids, token_type_ids=token_type_ids, max_new_tokens=10)
output_text = tokenizer.decode(gen_token[0])
repeated_text = output_text
for _ in range(num_repeats):
x_token = tokenizer("", prefix_text=repeated_text, return_tensors="pt")
input_ids = x_token.input_ids.to(device)
token_type_ids = x_token.token_type_ids.to(device)
gen_token = model.generate(input_ids, token_type_ids=token_type_ids, max_new_tokens=10)
repeated_text += tokenizer.decode(gen_token[0])
return repeated_text
input_text = gr.inputs.Textbox(lines=5, label="Input Text")
num_repeats = gr.inputs.Number(default=1, label="Number of Repeats")
output_text = gr.outputs.Textbox(label="Generated Text")
interface = gr.Interface(
fn=generate_text,
inputs=[input_text, num_repeats],
outputs=output_text,
title=get_memory_usage(),
description="Enter a prompt in Japanese to generate text."
)