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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
AI-Powered Data Generator
|
| 3 |
+
|
| 4 |
+
Uses AI models to generate realistic text content.
|
| 5 |
+
"""
|
| 6 |
+
|
| 7 |
+
import random
|
| 8 |
+
from typing import Any, Dict, List, Optional
|
| 9 |
+
from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM
|
| 10 |
+
import torch
|
| 11 |
+
from .base_generator import BaseGenerator
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
class AIGenerator(BaseGenerator):
|
| 15 |
+
"""Generator using AI models for realistic text generation."""
|
| 16 |
+
|
| 17 |
+
def __init__(self, seed: Optional[int] = None, model_name: str = "gpt2"):
|
| 18 |
+
super().__init__(seed)
|
| 19 |
+
self.model_name = model_name
|
| 20 |
+
self.text_generator = None
|
| 21 |
+
self._load_model()
|
| 22 |
+
|
| 23 |
+
def _load_model(self):
|
| 24 |
+
"""Load the AI model for text generation."""
|
| 25 |
+
try:
|
| 26 |
+
# Use a smaller model for Hugging Face Spaces
|
| 27 |
+
if self.model_name == "gpt2":
|
| 28 |
+
self.text_generator = pipeline(
|
| 29 |
+
"text-generation",
|
| 30 |
+
model="gpt2",
|
| 31 |
+
tokenizer="gpt2",
|
| 32 |
+
max_length=100,
|
| 33 |
+
do_sample=True,
|
| 34 |
+
temperature=0.7,
|
| 35 |
+
pad_token_id=50256
|
| 36 |
+
)
|
| 37 |
+
else:
|
| 38 |
+
# Fallback to a smaller model
|
| 39 |
+
self.text_generator = pipeline(
|
| 40 |
+
"text-generation",
|
| 41 |
+
model="distilgpt2",
|
| 42 |
+
max_length=50,
|
| 43 |
+
do_sample=True,
|
| 44 |
+
temperature=0.7
|
| 45 |
+
)
|
| 46 |
+
except Exception as e:
|
| 47 |
+
print(f"Warning: Could not load AI model: {e}")
|
| 48 |
+
self.text_generator = None
|
| 49 |
+
|
| 50 |
+
def generate(self, count: int, prompt: str = "", text_type: str = "description", **kwargs) -> List[str]:
|
| 51 |
+
"""Generate AI-powered text content."""
|
| 52 |
+
if not self.text_generator:
|
| 53 |
+
# Fallback to basic text generation
|
| 54 |
+
return [f"AI Generated Text {i+1}" for i in range(count)]
|
| 55 |
+
|
| 56 |
+
data = []
|
| 57 |
+
|
| 58 |
+
for i in range(count):
|
| 59 |
+
try:
|
| 60 |
+
if text_type == "description":
|
| 61 |
+
generated_text = self._generate_description(prompt, **kwargs)
|
| 62 |
+
elif text_type == "product_name":
|
| 63 |
+
generated_text = self._generate_product_name(prompt, **kwargs)
|
| 64 |
+
elif text_type == "review":
|
| 65 |
+
generated_text = self._generate_review(prompt, **kwargs)
|
| 66 |
+
elif text_type == "email":
|
| 67 |
+
generated_text = self._generate_email_content(prompt, **kwargs)
|
| 68 |
+
else:
|
| 69 |
+
generated_text = self._generate_generic_text(prompt, **kwargs)
|
| 70 |
+
|
| 71 |
+
data.append(generated_text)
|
| 72 |
+
except Exception as e:
|
| 73 |
+
# Fallback to basic text
|
| 74 |
+
data.append(f"Generated Content {i+1}")
|
| 75 |
+
|
| 76 |
+
return data
|
| 77 |
+
|
| 78 |
+
def _generate_description(self, prompt: str = "", **kwargs) -> str:
|
| 79 |
+
"""Generate product descriptions."""
|
| 80 |
+
if not prompt:
|
| 81 |
+
prompts = [
|
| 82 |
+
"This product is",
|
| 83 |
+
"The features include",
|
| 84 |
+
"This innovative solution",
|
| 85 |
+
"Our latest offering",
|
| 86 |
+
"This high-quality item"
|
| 87 |
+
]
|
| 88 |
+
prompt = random.choice(prompts)
|
| 89 |
+
|
| 90 |
+
try:
|
| 91 |
+
result = self.text_generator(
|
| 92 |
+
prompt,
|
| 93 |
+
max_length=len(prompt.split()) + 20,
|
| 94 |
+
num_return_sequences=1,
|
| 95 |
+
temperature=0.7,
|
| 96 |
+
do_sample=True
|
| 97 |
+
)
|
| 98 |
+
return result[0]['generated_text'].strip()
|
| 99 |
+
except:
|
| 100 |
+
return f"{prompt} designed to meet your needs with excellent quality and performance."
|
| 101 |
+
|
| 102 |
+
def _generate_product_name(self, prompt: str = "", **kwargs) -> str:
|
| 103 |
+
"""Generate product names."""
|
| 104 |
+
if not prompt:
|
| 105 |
+
prompts = [
|
| 106 |
+
"Smart",
|
| 107 |
+
"Pro",
|
| 108 |
+
"Ultra",
|
| 109 |
+
"Advanced",
|
| 110 |
+
"Premium"
|
| 111 |
+
]
|
| 112 |
+
prompt = random.choice(prompts)
|
| 113 |
+
|
| 114 |
+
try:
|
| 115 |
+
result = self.text_generator(
|
| 116 |
+
prompt,
|
| 117 |
+
max_length=len(prompt.split()) + 5,
|
| 118 |
+
num_return_sequences=1,
|
| 119 |
+
temperature=0.8,
|
| 120 |
+
do_sample=True
|
| 121 |
+
)
|
| 122 |
+
return result[0]['generated_text'].strip()
|
| 123 |
+
except:
|
| 124 |
+
return f"{prompt} Product {random.randint(100, 999)}"
|
| 125 |
+
|
| 126 |
+
def _generate_review(self, prompt: str = "", **kwargs) -> str:
|
| 127 |
+
"""Generate product reviews."""
|
| 128 |
+
if not prompt:
|
| 129 |
+
prompts = [
|
| 130 |
+
"This product is amazing",
|
| 131 |
+
"I love this item",
|
| 132 |
+
"Great quality and",
|
| 133 |
+
"Highly recommend this",
|
| 134 |
+
"Excellent value for"
|
| 135 |
+
]
|
| 136 |
+
prompt = random.choice(prompts)
|
| 137 |
+
|
| 138 |
+
try:
|
| 139 |
+
result = self.text_generator(
|
| 140 |
+
prompt,
|
| 141 |
+
max_length=len(prompt.split()) + 15,
|
| 142 |
+
num_return_sequences=1,
|
| 143 |
+
temperature=0.7,
|
| 144 |
+
do_sample=True
|
| 145 |
+
)
|
| 146 |
+
return result[0]['generated_text'].strip()
|
| 147 |
+
except:
|
| 148 |
+
return f"{prompt} and I would definitely buy it again!"
|
| 149 |
+
|
| 150 |
+
def _generate_email_content(self, prompt: str = "", **kwargs) -> str:
|
| 151 |
+
"""Generate email content."""
|
| 152 |
+
if not prompt:
|
| 153 |
+
prompts = [
|
| 154 |
+
"Dear customer,",
|
| 155 |
+
"Thank you for your",
|
| 156 |
+
"We are pleased to",
|
| 157 |
+
"Your order has been",
|
| 158 |
+
"We would like to"
|
| 159 |
+
]
|
| 160 |
+
prompt = random.choice(prompts)
|
| 161 |
+
|
| 162 |
+
try:
|
| 163 |
+
result = self.text_generator(
|
| 164 |
+
prompt,
|
| 165 |
+
max_length=len(prompt.split()) + 25,
|
| 166 |
+
num_return_sequences=1,
|
| 167 |
+
temperature=0.6,
|
| 168 |
+
do_sample=True
|
| 169 |
+
)
|
| 170 |
+
return result[0]['generated_text'].strip()
|
| 171 |
+
except:
|
| 172 |
+
return f"{prompt} and we appreciate your business."
|
| 173 |
+
|
| 174 |
+
def _generate_generic_text(self, prompt: str = "", **kwargs) -> str:
|
| 175 |
+
"""Generate generic text content."""
|
| 176 |
+
if not prompt:
|
| 177 |
+
prompt = "The following information"
|
| 178 |
+
|
| 179 |
+
try:
|
| 180 |
+
result = self.text_generator(
|
| 181 |
+
prompt,
|
| 182 |
+
max_length=len(prompt.split()) + 10,
|
| 183 |
+
num_return_sequences=1,
|
| 184 |
+
temperature=0.7,
|
| 185 |
+
do_sample=True
|
| 186 |
+
)
|
| 187 |
+
return result[0]['generated_text'].strip()
|
| 188 |
+
except:
|
| 189 |
+
return f"{prompt} is important for your understanding."
|