summerstars commited on
Commit
b961b79
·
verified ·
1 Parent(s): 03c637e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -36
app.py CHANGED
@@ -1,41 +1,50 @@
1
  import gradio as gr
2
- from diffusers import AutoPipelineForText2Image
3
- import torch
4
-
5
- # デバイス選択(CPUかGPUか)
6
- device = "cuda" if torch.cuda.is_available() else "cpu"
7
-
8
- # パイプラインのロード
9
- pipeline = AutoPipelineForText2Image.from_pretrained(
10
- 'runwayml/stable-diffusion-v1-5',
11
- torch_dtype=torch.float32
12
- ).to(device)
13
-
14
- # LoRAウェイトをロード
15
- pipeline.load_lora_weights('OVAWARE/plixel-minecraft', weight_name='Plixel-SD-1.5.safetensors')
16
-
17
- # 画像生成関数
18
- def generate_image(prompt):
19
- if not prompt.strip():
20
- return "プロンプトを入力してください!"
21
- try:
22
- image = pipeline(prompt).images[0]
23
- return image
24
- except Exception as e:
25
- return f"エラーが発生しました: {str(e)}"
26
-
27
- # Gradioインターフェースの作成
 
 
 
 
 
 
 
 
 
 
 
 
 
28
  iface = gr.Interface(
29
- fn=generate_image,
30
- inputs=gr.Textbox(
31
- lines=2,
32
- placeholder="例: A futuristic city with flying cars",
33
- label="プロンプト"
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()