from llama_cpp import Llama
def summarize_text(text, model, max_tokens=256, temperature=0.1):
"""
Summarize the given text using the loaded LLM model.
Args:
text (str): Text to summarize
model: Loaded LLM model
max_tokens (int): Maximum number of tokens to generate
temperature (float): Controls randomness in generation (lower = more deterministic)
Returns:
str: Generated summary
"""
prompt = f"""Please provide a concise summary of the following text:
{text}
Summary:"""
# Generate the summary
response = model(
prompt,
max_tokens=max_tokens,
temperature=temperature,
top_p=0.1,
echo=False,
stop=["###"]
)
# Extract the generated text
summary = response["choices"]["text"].strip()
return summary
# Path to your downloaded GGUF model
model_path = "path/to/qwen3-0.6b-q4_0.gguf" # or q6_k.gguf for Q6 quantization
# Load the model
llm = Llama(
model_path=model_path,
n_ctx=4096, # Context size - adjust based on your needs
n_batch=512, # Batch size for processing
n_threads=4, # Number of CPU threads to use
verbose=False # Set to True for more detailed output
)
# Example usage
text_to_summarize = """
The development of artificial intelligence has seen remarkable progress in recent years,
particularly in natural language processing. Large language models like GPT, Llama, and Qwen
have demonstrated impressive capabilities in understanding and generating human language.
These models are trained on vast amounts of data and can perform a wide range of tasks,
from answering questions to creative writing. However, they also raise important questions
about bias, accuracy, and ethical use. Researchers are continuously working to address these
challenges while exploring new applications for this technology.
"""
summary = summarize_text(text_to_summarize, llm)
print(summary)
To use this code:
- Install llama-cpp-python:
pip install llama-cpp-python
- Download the Qwen3 0.6b model in GGUF format with Q4 or Q6 quantization from Hugging Face
- Update the
model_path variable to point to your downloaded model file
- Adjust parameters like
n_ctx, n_threads, and n_batch based on your hardware capabilities and needs[1][2][9]
The summarization function creates a prompt that instructs the model to summarize the provided text. You can modify the prompt template to get different summarization styles or formats. For longer texts, you may need to increase the n_ctx parameter to accommodate the full text plus generated summary[4][9].