yukimama commited on
Commit
0b8555e
·
verified ·
1 Parent(s): 477c07b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -8
app.py CHANGED
@@ -1,8 +1,9 @@
1
- from transformers import GPT2LMHeadModel, GPT2Tokenizer, pipeline
 
2
 
3
- # 使用緩存加速載入
4
- model = GPT2LMHeadModel.from_pretrained("gpt2", cache_dir="./model_cache")
5
- tokenizer = GPT2Tokenizer.from_pretrained("gpt2", cache_dir="./model_cache")
6
 
7
  # 設置填充標記 ID
8
  tokenizer.pad_token = tokenizer.eos_token
@@ -29,7 +30,49 @@ def generate_command(prompt, max_length=100):
29
 
30
  return command
31
 
32
- # 使用示例
33
- prompt = "刪除一個文件"
34
- generated_command = generate_command(prompt)
35
- print(generated_command)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import GPT2LMHeadModel, GPT2Tokenizer
2
+ import gradio as gr
3
 
4
+ # Load the pre-trained GPT2 model and tokenizer
5
+ model = GPT2LMHeadModel.from_pretrained("gpt2")
6
+ tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
7
 
8
  # 設置填充標記 ID
9
  tokenizer.pad_token = tokenizer.eos_token
 
30
 
31
  return command
32
 
33
+ def predict(input_text):
34
+ output = generate_command(input_text)
35
+ return output
36
+
37
+ iface = gr.Interface(
38
+ fn=predict,
39
+ inputs=gr.Textbox(lines=2, placeholder="請輸入你的指令生成提示..."),
40
+ outputs="text",
41
+ title="使用 GPT2 生成指令",
42
+ description="根據你的中文輸入提示生成 Bash 指令。"
43
+ )
44
+
45
+ iface.launch()
46
+ ```
47
+
48
+ ### 使用示例
49
+
50
+ 1. **生成文件操作指令**:
51
+ ```python
52
+ prompt = "刪除一個文件"
53
+ generated_command = generate_command(prompt)
54
+ print(generated_command)
55
+ ```
56
+
57
+ 可能的輸出:
58
+ ```
59
+ rm filename.txt
60
+ ```
61
+
62
+ 2. **生成網絡操作指令**:
63
+ ```python
64
+ prompt = "從URL下載一個文件"
65
+ generated_command = generate_command(prompt)
66
+ print(generated_command)
67
+ ```
68
+
69
+ 可能的輸出:
70
+ ```
71
+ wget https://example.com/file.txt
72
+ ```
73
+
74
+ 3. **生成系統管理指令**:
75
+ ```python
76
+ prompt = "檢查系統記憶體使用情況"
77
+ generated_command = generate_command(prompt)
78
+ print(generated_command)