|
|
|
import os |
|
|
|
|
|
os.system("pip install transformers torch psutil") |
|
|
|
|
|
result = os.system("pip install transformers") |
|
|
|
import os |
|
|
|
|
|
os.system("pip install transformers torch psutil") |
|
|
|
|
|
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 |
|
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." |
|
) |
|
|