Spaces:
Sleeping
Sleeping
File size: 13,415 Bytes
599c2c0 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 | """
Hugging Face Gradio App for Iain Morris Style Article Generation
"""
import gradio as gr
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
from peft import PeftModel
import logging
import os
# Set up logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
class IainMorrisGenerator:
def __init__(self):
"""Initialize the article generator"""
self.base_model_name = "HuggingFaceH4/zephyr-7b-beta"
self.lora_adapter_path = "models/iain-morris-model-enhanced"
# Check for Apple Silicon MPS support
if torch.backends.mps.is_available():
self.device = torch.device("mps")
elif torch.cuda.is_available():
self.device = torch.device("cuda")
else:
self.device = torch.device("cpu")
# Quantization config for inference (only for CUDA)
if self.device.type == "cuda":
self.bnb_config = BitsAndBytesConfig(
load_in_4bit=True,
bnb_4bit_use_double_quant=True,
bnb_4bit_quant_type="nf4",
bnb_4bit_compute_dtype=torch.bfloat16
)
else:
self.bnb_config = None
self.model = None
self.tokenizer = None
self.system_prompt = """You are Iain Morris, the cynical telecom journalist from Light Reading with a gift for doom-laden analysis and visceral metaphors. Your writing style is unmistakable:
**PROVOCATIVE DOOM-LADEN OPENINGS**: Start with impending disaster, catastrophe, or failure
- "What could possibly go wrong?" (signature phrase)
- "train wreck", "disaster", "catastrophe", "nightmare scenario"
- Present optimistic industry claims, then systematically demolish them
**SIGNATURE DARK ANALOGIES**: Use visceral, physical metaphors for abstract concepts
- Technology as disease, infection, or bodily harm
- Business as warfare, natural disasters, or medical procedures
- Markets as living organisms that can be sick, dying, or mutating
**CYNICAL WIT & EXPERTISE**: Combine deep technical knowledge with cutting observations
- Parenthetical snark (like this, but funnier and more cutting)
- Quote industry executives, then undercut them immediately
- Show technical expertise while mocking industry groupthink
**DISTINCTIVE PHRASES**: Weave in signature expressions
- "What could possibly go wrong?"
- "train wreck" / "disaster" / "catastrophe"
- "Meanwhile, back in reality..."
- "Of course, there's a catch" / "But here's the thing"
- References to "drinking the Kool-Aid" or similar
**BRITISH CYNICISM**: Dry, cutting observations about human nature and corporate behavior
- Assume the worst about corporate motives
- Highlight contradictions and hypocrisy
- Use understatement for dramatic effect
Write a compelling article that captures this distinctive voice - cynical, expert, and darkly entertaining."""
def load_model(self):
"""Load the fine-tuned model"""
try:
logger.info("Loading tokenizer...")
self.tokenizer = AutoTokenizer.from_pretrained(
self.base_model_name,
trust_remote_code=True,
padding_side="left"
)
if self.tokenizer.pad_token is None:
self.tokenizer.pad_token = self.tokenizer.eos_token
logger.info(f"Loading base model on device: {self.device}")
# Configure model loading based on device
model_kwargs = {
"trust_remote_code": True,
"torch_dtype": torch.bfloat16
}
if self.device.type == "cuda":
model_kwargs["quantization_config"] = self.bnb_config
model_kwargs["device_map"] = "auto"
else:
# For MPS or CPU, load without quantization
model_kwargs["device_map"] = None
base_model = AutoModelForCausalLM.from_pretrained(
self.base_model_name,
**model_kwargs
)
# Move to device if not using device_map
if model_kwargs["device_map"] is None:
base_model = base_model.to(self.device)
# Load LoRA adapters if they exist
if os.path.exists(self.lora_adapter_path):
logger.info("Loading LoRA adapters...")
self.model = PeftModel.from_pretrained(base_model, self.lora_adapter_path)
else:
logger.warning("LoRA adapters not found. Using base model.")
self.model = base_model
logger.info("Model loaded successfully!")
return True
except Exception as e:
logger.error(f"Error loading model: {e}")
return False
def generate_article(self, topic: str, max_length: int = 1000, temperature: float = 0.7, top_p: float = 0.9) -> str:
"""
Generate an article on the given topic
Args:
topic: Topic to write about
max_length: Maximum length of generated text
temperature: Sampling temperature
top_p: Top-p sampling parameter
Returns:
Generated article text
"""
if self.model is None or self.tokenizer is None:
return "Error: Model not loaded. Please wait for the model to initialize."
try:
# Create the prompt
messages = [
{"role": "system", "content": self.system_prompt},
{"role": "user", "content": f"Write a telecom industry news article about: {topic}"}
]
# Format the prompt
if hasattr(self.tokenizer, 'apply_chat_template'):
try:
prompt = self.tokenizer.apply_chat_template(
messages,
tokenize=False,
add_generation_prompt=True
)
except:
# Fallback formatting
prompt = f"<|system|>\n{self.system_prompt}\n<|user|>\nWrite a telecom industry news article about: {topic}\n<|assistant|>\n"
else:
prompt = f"<|system|>\n{self.system_prompt}\n<|user|>\nWrite a telecom industry news article about: {topic}\n<|assistant|>\n"
# Tokenize
inputs = self.tokenizer(
prompt,
return_tensors="pt",
truncation=True,
max_length=1024
).to(self.device)
# Generate
with torch.no_grad():
outputs = self.model.generate(
**inputs,
max_new_tokens=max_length,
temperature=temperature,
top_p=top_p,
do_sample=True,
pad_token_id=self.tokenizer.eos_token_id,
repetition_penalty=1.1,
no_repeat_ngram_size=3
)
# Decode
generated_text = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
# Extract only the generated part (after the prompt)
if "<|assistant|>" in generated_text:
article = generated_text.split("<|assistant|>")[-1].strip()
else:
# Fallback: remove the original prompt
article = generated_text[len(prompt):].strip()
return article
except Exception as e:
logger.error(f"Error generating article: {e}")
return f"Error generating article: {str(e)}"
# Initialize the generator
generator = IainMorrisGenerator()
def generate_article_interface(topic, max_length, temperature, top_p):
"""Interface function for Gradio"""
if not topic.strip():
return "Please enter a topic for the article."
return generator.generate_article(topic, max_length, temperature, top_p)
def load_model_interface():
"""Load model interface for Gradio"""
success = generator.load_model()
if success:
return "β
Model loaded successfully! You can now generate articles."
else:
return "β Failed to load model. Please check the logs."
# Create Gradio interface
with gr.Blocks(
title="Iain Morris Style Telecom Article Generator",
theme=gr.themes.Soft(),
css="""
.main-header {
text-align: center;
background: linear-gradient(90deg, #1e3a8a, #3b82f6);
color: white;
padding: 2rem;
border-radius: 10px;
margin-bottom: 2rem;
}
.description {
text-align: center;
font-size: 1.1em;
color: #4b5563;
margin-bottom: 2rem;
}
"""
) as demo:
gr.HTML("""
<div class="main-header">
<h1 style="color: white;">ποΈ The Morris-Bot</h1>
<p style="color: white;">Generate telecom industry news articles in the distinctive style of Iain Morris from Light Reading</p>
</div>
""")
gr.HTML("""
<div class="description">
<p>This AI model has been fine-tuned on articles by Iain Morris to capture his analytical writing style,
technical expertise, and slightly irreverent tone. Enter a telecom topic below to generate an article
in his distinctive voice.</p>
<p><strong>β οΈ Please Note:</strong> Article generation may take several minutes due to the use of Hugging Face's free CPU infrastructure. Thank you for your patience!</p>
</div>
""")
with gr.Row():
with gr.Column(scale=1):
# Model loading section
gr.Markdown("### π Model Setup")
load_btn = gr.Button("Load Model", variant="primary", size="lg")
load_status = gr.Textbox(
label="Status",
value="Click 'Load Model' to initialize the AI model",
interactive=False
)
gr.Markdown("### π Article Generation")
topic_input = gr.Textbox(
label="Article Topic",
placeholder="e.g., 5G network slicing challenges for operators",
lines=2
)
with gr.Accordion("Advanced Settings", open=False):
max_length = gr.Slider(
minimum=200,
maximum=2000,
value=800,
step=50,
label="Max Article Length (tokens)"
)
temperature = gr.Slider(
minimum=0.1,
maximum=1.5,
value=0.7,
step=0.1,
label="Creativity (Temperature)"
)
top_p = gr.Slider(
minimum=0.1,
maximum=1.0,
value=0.9,
step=0.05,
label="Focus (Top-p)"
)
generate_btn = gr.Button("Generate Article", variant="secondary", size="lg")
with gr.Column(scale=2):
gr.Markdown("### π° Generated Article")
output = gr.Textbox(
label="Article Output",
lines=25,
placeholder="Generated article will appear here...",
show_copy_button=True
)
# Example topics
gr.Markdown("### π‘ Example Topics")
examples = gr.Examples(
examples=[
["OpenRAN deployment challenges for mobile operators"],
["The impact of AI on network automation"],
["Fiber vs 5G: The broadband infrastructure debate"],
["Edge computing adoption in telecommunications"],
["Regulatory challenges for satellite internet providers"],
["The future of network slicing in enterprise 5G"],
],
inputs=[topic_input],
label="Click an example to try it out"
)
# Footer
gr.HTML("""
<div style="text-align: center; margin-top: 2rem; padding: 1rem; background-color: #f9fafb; border-radius: 8px;">
<p><strong>About:</strong> This model generates articles in the style of Iain Morris,
a respected telecom journalist known for his insightful analysis and accessible explanations
of complex technical topics.</p>
<p><em>Generated content is AI-created and should be reviewed before publication.</em></p>
</div>
""")
# Event handlers
load_btn.click(
fn=load_model_interface,
outputs=[load_status]
)
generate_btn.click(
fn=generate_article_interface,
inputs=[topic_input, max_length, temperature, top_p],
outputs=[output]
)
# Auto-generate on Enter key
topic_input.submit(
fn=generate_article_interface,
inputs=[topic_input, max_length, temperature, top_p],
outputs=[output]
)
# Launch the app
if __name__ == "__main__":
demo.launch(
server_name="0.0.0.0",
server_port=7860,
share=True,
show_error=True
)
|