Update app.py
Browse files
app.py
CHANGED
|
@@ -1,15 +1,13 @@
|
|
| 1 |
# app.py
|
| 2 |
import gradio as gr
|
| 3 |
-
from transformers import
|
| 4 |
|
| 5 |
-
# ---- Load model
|
| 6 |
MODEL_NAME = "vicgalle/gpt2-open-instruct-v1"
|
| 7 |
-
|
| 8 |
-
model = AutoModelForCausalLM.from_pretrained(MODEL_NAME)
|
| 9 |
|
| 10 |
-
# ----
|
| 11 |
def generate_response(instruction, max_new_tokens=150, temperature=0.7, top_k=50, top_p=0.9):
|
| 12 |
-
"""Generate text based on the given instruction."""
|
| 13 |
system_prompt = f"""Below is an instruction that describes a task. Write a response that appropriately completes the request.
|
| 14 |
|
| 15 |
### Instruction:
|
|
@@ -17,19 +15,18 @@ def generate_response(instruction, max_new_tokens=150, temperature=0.7, top_k=50
|
|
| 17 |
|
| 18 |
### Response:
|
| 19 |
"""
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
max_length=len(inputs["input_ids"][0]) + max_new_tokens,
|
| 24 |
-
num_return_sequences=1,
|
| 25 |
temperature=temperature,
|
| 26 |
top_k=top_k,
|
| 27 |
top_p=top_p,
|
| 28 |
-
|
|
|
|
| 29 |
)
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
return
|
| 33 |
|
| 34 |
# ---- Gradio UI ----
|
| 35 |
with gr.Blocks() as demo:
|
|
|
|
| 1 |
# app.py
|
| 2 |
import gradio as gr
|
| 3 |
+
from transformers import pipeline
|
| 4 |
|
| 5 |
+
# ---- Load model via pipeline ----
|
| 6 |
MODEL_NAME = "vicgalle/gpt2-open-instruct-v1"
|
| 7 |
+
pipe = pipeline("text-generation", model=MODEL_NAME, device_map="auto")
|
|
|
|
| 8 |
|
| 9 |
+
# ---- Inference function ----
|
| 10 |
def generate_response(instruction, max_new_tokens=150, temperature=0.7, top_k=50, top_p=0.9):
|
|
|
|
| 11 |
system_prompt = f"""Below is an instruction that describes a task. Write a response that appropriately completes the request.
|
| 12 |
|
| 13 |
### Instruction:
|
|
|
|
| 15 |
|
| 16 |
### Response:
|
| 17 |
"""
|
| 18 |
+
output = pipe(
|
| 19 |
+
system_prompt,
|
| 20 |
+
max_new_tokens=max_new_tokens,
|
|
|
|
|
|
|
| 21 |
temperature=temperature,
|
| 22 |
top_k=top_k,
|
| 23 |
top_p=top_p,
|
| 24 |
+
do_sample=True,
|
| 25 |
+
pad_token_id=pipe.tokenizer.eos_token_id,
|
| 26 |
)
|
| 27 |
+
# Clean up output text
|
| 28 |
+
text = output[0]["generated_text"]
|
| 29 |
+
return text.split("### Response:")[-1].strip()
|
| 30 |
|
| 31 |
# ---- Gradio UI ----
|
| 32 |
with gr.Blocks() as demo:
|