A paraphrasing tool that rewrites text in formal, casual, and concise styles using a free HuggingFace Inference API — no GPU or local model required.
HuggingFace hosts thousands of models with a free serverless inference tier — about 1000 requests/day.
import requests, os
HF_TOKEN = os.environ.get('HF_TOKEN') or 'hf_your_token_here'
API_URL = 'https://api-inference.huggingface.co/models/facebook/bart-large-cnn'
headers = {'Authorization': f'Bearer {HF_TOKEN}'}
def hf_generate(model_url, payload):
r = requests.post(model_url, headers=headers, json=payload, timeout=60)
r.raise_for_status()
return r.json()
# Test with a summarisation model first
text = "Artificial intelligence has transformed the way we interact with technology. Machine learning models can now generate human-quality text, recognise faces, translate languages, and drive cars."
result = hf_generate(API_URL, {'inputs': text, 'parameters': {'max_length': 60}})
print(result[0]['summary_text'])
T5 (Text-to-Text Transfer Transformer) can rewrite text when given the right prompt prefix.
# Valhalla t5-base-qa-qg-hl is a versatile T5 model
# We use the "paraphrase" task prefix
T5_URL = 'https://api-inference.huggingface.co/models/Valhalla/t5-base-qa-qg-hl'
def paraphrase(text, num_variations=3):
results = []
for _ in range(num_variations):
payload = {
'inputs': f'paraphrase: {text}',
'parameters': {
'max_length': 200,
'num_return_sequences': 1,
'do_sample': True,
'temperature': 0.9,
}
}
r = hf_generate(T5_URL, payload)
results.append(r[0]['generated_text'])
return results
original = "The meeting was cancelled because the CEO had an unexpected scheduling conflict."
variations = paraphrase(original)
for i, v in enumerate(variations, 1):
print(f"Version {i}: {v}")
Version 1: The CEO's sudden schedule conflict led to the cancellation of the meeting. Version 2: Due to an unforeseen scheduling conflict with the CEO, the meeting could not take place. Version 3: An unexpected conflict in the CEO's calendar resulted in the meeting being called off.
For style control (formal/casual/concise), use Gemini with style instructions.
import google.generativeai as genai
genai.configure(api_key=os.environ.get('GEMINI_API_KEY'))
model = genai.GenerativeModel('gemini-1.5-flash')
def paraphrase_styled(text, style):
styles = {
'formal': 'Rewrite this text in a formal, professional tone suitable for business communication.',
'casual': 'Rewrite this text in a friendly, conversational tone — as if texting a colleague.',
'concise': 'Rewrite this text as concisely as possible. Cut every unnecessary word.',
}
prompt = f"{styles[style]}\n\nOriginal text:\n{text}\n\nRewritten:"
return model.generate_content(prompt).text.strip()
original = "I wanted to reach out to let you know that the project deadline has been moved to next Friday and the team should make sure all deliverables are submitted before then."
for style in ['formal', 'casual', 'concise']:
print(f"\n[{style.upper()}]")
print(paraphrase_styled(original, style))
[FORMAL] Please be advised that the project deadline has been revised to next Friday. All team members are required to submit their deliverables prior to this date. [CASUAL] Hey everyone, just a heads up — project deadline got pushed to next Friday. Make sure your stuff is in before then! [CONCISE] Project deadline: next Friday. Submit all deliverables beforehand.
You combined two AI backends — HuggingFace for local-model paraphrasing and Gemini for style-aware rewriting. This dual-backend pattern is common in production AI tools: use a free/local model for speed, and a frontier model for quality.
Spotted a bug, broken code, or something that doesn't look right? Tell us what's off and we'll fix it.