Update app.py
Browse files
app.py
CHANGED
@@ -8,8 +8,8 @@ tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
|
|
8 |
# 設置填充標記 ID
|
9 |
tokenizer.pad_token = tokenizer.eos_token
|
10 |
|
11 |
-
def
|
12 |
-
full_prompt = f"Generate
|
13 |
inputs = tokenizer.encode(full_prompt, return_tensors="pt")
|
14 |
|
15 |
output = model.generate(
|
@@ -21,25 +21,26 @@ def generate_code(prompt, max_length=200):
|
|
21 |
)
|
22 |
generated_text = tokenizer.decode(output[0], skip_special_tokens=False)
|
23 |
|
24 |
-
#
|
25 |
-
start = generated_text.find("```
|
26 |
end = generated_text.find("```", start)
|
27 |
if end == -1:
|
28 |
end = len(generated_text)
|
29 |
-
|
30 |
|
31 |
-
return
|
32 |
|
33 |
def predict(input_text):
|
34 |
-
output =
|
35 |
return output
|
36 |
|
37 |
iface = gr.Interface(
|
38 |
fn=predict,
|
39 |
-
inputs=gr.Textbox(lines=2, placeholder="Enter your
|
40 |
outputs="text",
|
41 |
-
title="
|
42 |
-
description="Generate
|
43 |
)
|
44 |
|
45 |
-
iface.launch()
|
|
|
|
8 |
# 設置填充標記 ID
|
9 |
tokenizer.pad_token = tokenizer.eos_token
|
10 |
|
11 |
+
def generate_command(prompt, max_length=100):
|
12 |
+
full_prompt = f"Generate a command for {prompt}:```bash\n"
|
13 |
inputs = tokenizer.encode(full_prompt, return_tensors="pt")
|
14 |
|
15 |
output = model.generate(
|
|
|
21 |
)
|
22 |
generated_text = tokenizer.decode(output[0], skip_special_tokens=False)
|
23 |
|
24 |
+
# 提取生成的指令
|
25 |
+
start = generated_text.find("```bash") + len("```bash")
|
26 |
end = generated_text.find("```", start)
|
27 |
if end == -1:
|
28 |
end = len(generated_text)
|
29 |
+
command = generated_text[start:end].strip()
|
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="Enter your command generation prompt here..."),
|
40 |
outputs="text",
|
41 |
+
title="Command Generation with GPT2",
|
42 |
+
description="Generate bash commands based on your input prompt."
|
43 |
)
|
44 |
|
45 |
+
iface.launch()
|
46 |
+
```
|