Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import os
|
3 |
+
os.system("pip install torch transformers sentencepiece accelerate")
|
4 |
+
import torch
|
5 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
6 |
+
|
7 |
+
# モデルとトークナイザの初期化
|
8 |
+
model = AutoModelForCausalLM.from_pretrained("cyberagent/open-calm-1b", device=torch.device("cuda" if torch.cuda.is_available() else "cpu"), torch_dtype=torch.float16)
|
9 |
+
tokenizer = AutoTokenizer.from_pretrained("cyberagent/open-calm-1b")
|
10 |
+
|
11 |
+
# 推論用の関数
|
12 |
+
def generate_text(input_text, max_new_tokens, temperature, top_p, repetition_penalty):
|
13 |
+
inputs = tokenizer(input_text, return_tensors="pt").to(model.device)
|
14 |
+
with torch.no_grad():
|
15 |
+
tokens = model.generate(
|
16 |
+
**inputs,
|
17 |
+
max_new_tokens=max_new_tokens,
|
18 |
+
do_sample=True,
|
19 |
+
temperature=temperature,
|
20 |
+
top_p=top_p,
|
21 |
+
repetition_penalty=repetition_penalty,
|
22 |
+
pad_token_id=tokenizer.pad_token_id,
|
23 |
+
)
|
24 |
+
output = tokenizer.decode(tokens[0], skip_special_tokens=True)
|
25 |
+
return output
|
26 |
+
|
27 |
+
# Streamlitアプリの設定
|
28 |
+
st.title("Causal Language Modeling")
|
29 |
+
st.write("AIによる文章生成")
|
30 |
+
|
31 |
+
# パラメータの入力
|
32 |
+
input_text = st.text_area("入力テキスト")
|
33 |
+
max_new_tokens = st.slider("生成する最大トークン数", min_value=1, max_value=512, value=64)
|
34 |
+
temperature = st.slider("Temperature", min_value=0.1, max_value=2.0, value=0.7)
|
35 |
+
top_p = st.slider("Top-p", min_value=0.1, max_value=1.0, value=0.9)
|
36 |
+
repetition_penalty = st.slider("Repetition Penalty", min_value=0.1, max_value=2.0, value=1.05)
|
37 |
+
|
38 |
+
# 推論結果の表示
|
39 |
+
if st.button("生成"):
|
40 |
+
output = generate_text(input_text, max_new_tokens, temperature, top_p, repetition_penalty)
|
41 |
+
st.write("生成されたテキスト:")
|
42 |
+
st.write(output)
|