wormgpt / app.py
yukimama's picture
Update app.py
58c8998 verified
raw
history blame
2.02 kB
from transformers import GPT2LMHeadModel, GPT2Tokenizer
import gradio as gr
# Load the pre-trained GPT2 model and tokenizer
model = GPT2LMHeadModel.from_pretrained("gpt2")
tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
# 設置填充標記 ID
tokenizer.pad_token = tokenizer.eos_token
def generate_command(prompt, max_length=100):
full_prompt = f"Generate a Bash command for {prompt}:```bash\n"
inputs = tokenizer.encode(full_prompt, return_tensors="pt")
output = model.generate(
inputs,
max_length=max_length,
num_return_sequences=1,
temperature=0.7,
pad_token_id=tokenizer.pad_token_id
)
generated_text = tokenizer.decode(output[0], skip_special_tokens=False)
# 提取生成的指令
start = generated_text.find("```bash") + len("```bash")
end = generated_text.find("```", start)
if end == -1:
end = len(generated_text)
command = generated_text[start:end].strip()
return command
def predict(input_text):
output = generate_command(input_text)
return output
iface = gr.Interface(
fn=predict,
inputs=gr.Textbox(lines=2, placeholder="Enter your command generation prompt here..."),
outputs="text",
title="Command Generation with GPT2",
description="Generate bash commands based on your input prompt."
)
iface.launch()
```
### 使用示例
1. **生成文件操作指令**:
```python
prompt = "deleting a file"
generated_command = generate_command(prompt)
print(generated_command)
```
可能的輸出:
```
rm filename.txt
```
2. **生成網絡操作指令**:
```python
prompt = "downloading a file from a URL"
generated_command = generate_command(prompt)
print(generated_command)
```
可能的輸出:
```
wget https://example.com/file.txt
```
3. **生成系統管理指令**:
```python
prompt = "checking system memory usage"
generated_command = generate_command(prompt)
print(generated_command)