Update app.py
Browse files
app.py
CHANGED
|
@@ -1,41 +1,53 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
-
TITLE = "News
|
| 5 |
DESCRIPTION = (
|
| 6 |
-
"
|
| 7 |
-
"
|
| 8 |
)
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
|
| 13 |
def generate_text(prompt: str, temperature: float, top_p: float, max_length: int) -> str:
|
| 14 |
if not prompt or not prompt.strip():
|
| 15 |
return "Please enter a prompt."
|
| 16 |
|
| 17 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
temperature = max(0.01, float(temperature))
|
| 19 |
|
| 20 |
outputs = generator(
|
| 21 |
-
|
| 22 |
do_sample=True,
|
| 23 |
temperature=temperature,
|
| 24 |
top_p=float(top_p),
|
| 25 |
-
|
| 26 |
-
num_return_sequences=1,
|
| 27 |
pad_token_id=generator.tokenizer.eos_token_id,
|
| 28 |
)
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
|
|
|
| 32 |
|
|
|
|
| 33 |
examples = [
|
| 34 |
-
["
|
| 35 |
-
["
|
| 36 |
-
["
|
| 37 |
-
["Researchers at the university published a study showing", 0.5, 0.9, 100],
|
| 38 |
-
["This week in technology: the biggest story is", 0.5, 0.9, 100],
|
| 39 |
]
|
| 40 |
|
| 41 |
with gr.Blocks(title=TITLE) as demo:
|
|
@@ -45,45 +57,17 @@ with gr.Blocks(title=TITLE) as demo:
|
|
| 45 |
with gr.Row():
|
| 46 |
with gr.Column():
|
| 47 |
prompt = gr.Textbox(
|
| 48 |
-
label="Prompt",
|
| 49 |
-
placeholder="
|
| 50 |
lines=4,
|
| 51 |
)
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
value=0.5,
|
| 57 |
-
step=0.1,
|
| 58 |
-
label="Temperature",
|
| 59 |
-
info="Controls how creative/wild the writing is",
|
| 60 |
-
)
|
| 61 |
-
|
| 62 |
-
top_p = gr.Slider(
|
| 63 |
-
minimum=0.1,
|
| 64 |
-
maximum=1.0,
|
| 65 |
-
value=0.9,
|
| 66 |
-
step=0.05,
|
| 67 |
-
label="Top-p",
|
| 68 |
-
info="Controls word diversity",
|
| 69 |
-
)
|
| 70 |
-
|
| 71 |
-
max_length = gr.Slider(
|
| 72 |
-
minimum=20,
|
| 73 |
-
maximum=200,
|
| 74 |
-
value=100,
|
| 75 |
-
step=1,
|
| 76 |
-
label="Max Length",
|
| 77 |
-
info="Controls how much text it generates",
|
| 78 |
-
)
|
| 79 |
-
|
| 80 |
-
generate_button = gr.Button("Generate")
|
| 81 |
|
| 82 |
with gr.Column():
|
| 83 |
-
output = gr.Textbox(
|
| 84 |
-
label="Generated Text",
|
| 85 |
-
lines=16,
|
| 86 |
-
)
|
| 87 |
|
| 88 |
generate_button.click(
|
| 89 |
fn=generate_text,
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
+
TITLE = "Qwen News Assistant"
|
| 5 |
DESCRIPTION = (
|
| 6 |
+
"Using Qwen2.5-0.5B-Instruct to generate news drafts. "
|
| 7 |
+
"This model is optimized for CPU efficiency and instruction following."
|
| 8 |
)
|
| 9 |
|
| 10 |
+
# Load the lightweight Qwen 0.5B model
|
| 11 |
+
generator = pipeline("text-generation", model="Qwen/Qwen2.5-0.5B-Instruct")
|
| 12 |
|
| 13 |
def generate_text(prompt: str, temperature: float, top_p: float, max_length: int) -> str:
|
| 14 |
if not prompt or not prompt.strip():
|
| 15 |
return "Please enter a prompt."
|
| 16 |
|
| 17 |
+
# Instruct models perform best when prompts are formatted correctly
|
| 18 |
+
# For Qwen, we wrap the prompt in a simple instruction format
|
| 19 |
+
messages = [
|
| 20 |
+
{"role": "system", "content": "You are a helpful news writing assistant."},
|
| 21 |
+
{"role": "user", "content": prompt},
|
| 22 |
+
]
|
| 23 |
+
|
| 24 |
+
# Apply the chat template
|
| 25 |
+
formatted_prompt = generator.tokenizer.apply_chat_template(
|
| 26 |
+
messages,
|
| 27 |
+
tokenize=False,
|
| 28 |
+
add_generation_prompt=True
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
temperature = max(0.01, float(temperature))
|
| 32 |
|
| 33 |
outputs = generator(
|
| 34 |
+
formatted_prompt,
|
| 35 |
do_sample=True,
|
| 36 |
temperature=temperature,
|
| 37 |
top_p=float(top_p),
|
| 38 |
+
max_new_tokens=int(max_length), # max_new_tokens is safer for instruct models
|
|
|
|
| 39 |
pad_token_id=generator.tokenizer.eos_token_id,
|
| 40 |
)
|
| 41 |
|
| 42 |
+
# Extract only the newly generated text
|
| 43 |
+
generated_text = outputs[0]["generated_text"]
|
| 44 |
+
return generated_text.split("<|im_start|>assistant\n")[-1].strip()
|
| 45 |
|
| 46 |
+
# Updated examples to be more "Instruction" focused
|
| 47 |
examples = [
|
| 48 |
+
["Write a breaking news headline about a discovery on Mars.", 0.7, 0.9, 100],
|
| 49 |
+
["Write the opening paragraph for a story about local students winning a robotics competition.", 0.5, 0.9, 150],
|
| 50 |
+
["Summarize the importance of artificial intelligence in 2024.", 0.3, 0.9, 200],
|
|
|
|
|
|
|
| 51 |
]
|
| 52 |
|
| 53 |
with gr.Blocks(title=TITLE) as demo:
|
|
|
|
| 57 |
with gr.Row():
|
| 58 |
with gr.Column():
|
| 59 |
prompt = gr.Textbox(
|
| 60 |
+
label="Prompt / Instruction",
|
| 61 |
+
placeholder="e.g., 'Write a news report about...'",
|
| 62 |
lines=4,
|
| 63 |
)
|
| 64 |
+
temperature = gr.Slider(minimum=0.1, maximum=2.0, value=0.7, step=0.1, label="Temperature")
|
| 65 |
+
top_p = gr.Slider(minimum=0.1, maximum=1.0, value=0.9, step=0.05, label="Top-p")
|
| 66 |
+
max_length = gr.Slider(minimum=20, maximum=300, value=150, step=1, label="Max New Tokens")
|
| 67 |
+
generate_button = gr.Button("Generate", variant="primary")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
with gr.Column():
|
| 70 |
+
output = gr.Textbox(label="Generated News Draft", lines=16)
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
generate_button.click(
|
| 73 |
fn=generate_text,
|