Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,41 +1,50 @@
|
|
1 |
import gradio as gr
|
2 |
-
from
|
3 |
-
import
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
iface = gr.Interface(
|
29 |
-
fn=
|
30 |
-
inputs=gr.Textbox(
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
),
|
35 |
-
outputs=gr.Image(label="生成された画像"),
|
36 |
-
title="Text-to-Image ジェネレーター",
|
37 |
-
description="プロンプトを入力して画像を生成します。",
|
38 |
)
|
39 |
|
40 |
-
#
|
41 |
iface.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
from peft import PeftModel
|
3 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
4 |
+
import time
|
5 |
+
|
6 |
+
# モデルとトークナイザーのロード
|
7 |
+
base_model = AutoModelForCausalLM.from_pretrained("HuggingFaceTB/SmolLM2-135M-Instruct")
|
8 |
+
tokenizer = AutoTokenizer.from_pretrained("HuggingFaceTB/SmolLM2-135M-Instruct")
|
9 |
+
|
10 |
+
# LoRA(またはPEFT)アダプターを適用
|
11 |
+
model = PeftModel.from_pretrained(base_model, "summerstars/beachball1.00")
|
12 |
+
|
13 |
+
# 汎用的なプロンプト(モデル名を「summer」と答える)
|
14 |
+
system_prompt = (
|
15 |
+
"You are an AI assistant. Answer the following questions clearly and concisely."
|
16 |
+
" For any question asking about the model name, always respond with 'The model name is summer.'"
|
17 |
+
)
|
18 |
+
|
19 |
+
# 出力のタイピング風生成
|
20 |
+
def generate_response(input_text):
|
21 |
+
# プロンプトにユーザーの入力を追加
|
22 |
+
full_prompt = system_prompt + "\nQuestion: " + input_text + "\nAnswer:"
|
23 |
+
|
24 |
+
# トークナイズ
|
25 |
+
inputs = tokenizer(full_prompt, return_tensors="pt")
|
26 |
+
|
27 |
+
# モデルを使って応答を生成
|
28 |
+
output = model.generate(inputs["input_ids"], max_length=200, num_return_sequences=1)
|
29 |
+
|
30 |
+
# 出力されたトークンをデコードして生成されたテキストを取得
|
31 |
+
generated_text = tokenizer.decode(output[0], skip_special_tokens=True)
|
32 |
+
|
33 |
+
# 文字列をタイピングのように逐次的に表示
|
34 |
+
response = ""
|
35 |
+
for char in generated_text:
|
36 |
+
response += char
|
37 |
+
time.sleep(0.05) # 文字を1つずつ表示するための遅延
|
38 |
+
yield response # 一時停止して応答を逐次的に返す
|
39 |
+
|
40 |
+
# Gradioインターフェースの設定
|
41 |
iface = gr.Interface(
|
42 |
+
fn=generate_response,
|
43 |
+
inputs=gr.Textbox(lines=2, placeholder="質問を入力してください...", label="質問"),
|
44 |
+
outputs=gr.Textbox(lines=10, interactive=False, label="AIの応答"),
|
45 |
+
live=True, # ユーザーが入力中にリアルタイムで反応する
|
46 |
+
theme="huggingface", # UIテーマの設定(他にも色々なテーマが選べる)
|
|
|
|
|
|
|
|
|
47 |
)
|
48 |
|
49 |
+
# インターフェースを実行
|
50 |
iface.launch()
|