Update app.py
Browse files
app.py
CHANGED
|
@@ -171,7 +171,8 @@ import requests
|
|
| 171 |
import re
|
| 172 |
from datetime import datetime
|
| 173 |
from dotenv import load_dotenv
|
| 174 |
-
from huggingface_hub import HfApi,
|
|
|
|
| 175 |
|
| 176 |
load_dotenv()
|
| 177 |
|
|
@@ -180,21 +181,38 @@ class BlogCreator:
|
|
| 180 |
self.hf_token = os.getenv("HF_TOKEN")
|
| 181 |
self.api = HfApi(token=self.hf_token)
|
| 182 |
self.headers = {"Authorization": f"Bearer {self.hf_token}"}
|
|
|
|
| 183 |
|
| 184 |
def generate_blog(self, topic):
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 195 |
|
| 196 |
-
def
|
| 197 |
-
|
|
|
|
| 198 |
"https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.2",
|
| 199 |
headers=self.headers,
|
| 200 |
json={
|
|
@@ -202,116 +220,147 @@ class BlogCreator:
|
|
| 202 |
"parameters": {"max_length": 2000}
|
| 203 |
}
|
| 204 |
)
|
| 205 |
-
|
| 206 |
|
| 207 |
-
|
| 208 |
-
image_prompts = re.findall(r'\[IMAGE\d+\]', content)
|
| 209 |
image_urls = []
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
response = requests.post(
|
| 213 |
"https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-xl-base-1.0",
|
| 214 |
headers=self.headers,
|
| 215 |
-
json={"inputs": f"
|
| 216 |
)
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
path_or_fileobj=
|
| 220 |
-
path_in_repo=
|
| 221 |
-
repo_id=
|
| 222 |
-
repo_type="
|
| 223 |
)
|
| 224 |
-
image_urls.append(
|
| 225 |
time.sleep(1)
|
| 226 |
-
|
| 227 |
-
return image_urls
|
| 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 |
path_or_fileobj=html_content.encode(),
|
| 263 |
path_in_repo="index.html",
|
| 264 |
-
repo_id=
|
| 265 |
repo_type="space"
|
| 266 |
)
|
| 267 |
-
|
| 268 |
-
return f"https://huggingface.co/spaces/username/{repo_name}"
|
| 269 |
-
|
| 270 |
-
def _format_content(self, content, image_urls):
|
| 271 |
-
# Replace image placeholders with actual URLs
|
| 272 |
-
for idx, url in enumerate(image_urls):
|
| 273 |
-
content = content.replace(f"[IMAGE{idx+1}]", f'<img src="{url}" alt="Blog Image {idx+1}">')
|
| 274 |
-
|
| 275 |
-
# Convert markdown to HTML
|
| 276 |
-
content = content.replace("\n", "<br>")
|
| 277 |
-
content = re.sub(r"### (.*?)", r"<h3>\1</h3>", content)
|
| 278 |
-
content = re.sub(r"## (.*?)", r"<h2>\1</h2>", content)
|
| 279 |
-
content = re.sub(r"\*\*(.*?)\*\*", r"<strong>\1</strong>", content)
|
| 280 |
-
|
| 281 |
-
return content
|
| 282 |
-
|
| 283 |
-
# Initialize blog creator
|
| 284 |
-
creator = BlogCreator()
|
| 285 |
|
| 286 |
with gr.Blocks(theme=gr.themes.Soft()) as app:
|
| 287 |
-
gr.Markdown("#
|
| 288 |
|
| 289 |
with gr.Row():
|
| 290 |
with gr.Column():
|
| 291 |
topic_input = gr.Textbox(label="Blog Topic", placeholder="Enter your topic...")
|
| 292 |
-
generate_btn = gr.Button("
|
| 293 |
|
| 294 |
with gr.Column():
|
| 295 |
-
gr.Markdown("##
|
| 296 |
-
|
| 297 |
-
|
| 298 |
status = gr.Textbox(label="Status")
|
| 299 |
|
| 300 |
def process_request(topic):
|
|
|
|
| 301 |
try:
|
| 302 |
-
|
| 303 |
return (
|
| 304 |
-
|
| 305 |
-
f"
|
| 306 |
-
"✅ Blog
|
| 307 |
)
|
| 308 |
except Exception as e:
|
| 309 |
-
return "
|
| 310 |
|
| 311 |
generate_btn.click(
|
| 312 |
fn=process_request,
|
| 313 |
inputs=topic_input,
|
| 314 |
-
outputs=[
|
| 315 |
)
|
| 316 |
|
| 317 |
if __name__ == "__main__":
|
|
|
|
| 171 |
import re
|
| 172 |
from datetime import datetime
|
| 173 |
from dotenv import load_dotenv
|
| 174 |
+
from huggingface_hub import HfApi, upload_file
|
| 175 |
+
import uuid
|
| 176 |
|
| 177 |
load_dotenv()
|
| 178 |
|
|
|
|
| 181 |
self.hf_token = os.getenv("HF_TOKEN")
|
| 182 |
self.api = HfApi(token=self.hf_token)
|
| 183 |
self.headers = {"Authorization": f"Bearer {self.hf_token}"}
|
| 184 |
+
self.username = "jsakshi" # Replace with your HF username
|
| 185 |
|
| 186 |
def generate_blog(self, topic):
|
| 187 |
+
try:
|
| 188 |
+
# Generate unique space name
|
| 189 |
+
space_name = f"{self.username}/blog-{uuid.uuid4().hex[:8]}"
|
| 190 |
+
|
| 191 |
+
# Create blog space
|
| 192 |
+
self.api.create_repo(
|
| 193 |
+
repo_id=space_name,
|
| 194 |
+
repo_type="space",
|
| 195 |
+
space_sdk="static",
|
| 196 |
+
private=False
|
| 197 |
+
)
|
| 198 |
+
|
| 199 |
+
# Generate content
|
| 200 |
+
blog_text, image_urls = self._generate_content(topic, space_name)
|
| 201 |
+
|
| 202 |
+
# Create HTML with design
|
| 203 |
+
html_content = self._create_design(blog_text, image_urls, topic)
|
| 204 |
+
|
| 205 |
+
# Upload files to space
|
| 206 |
+
self._upload_to_space(space_name, html_content, image_urls)
|
| 207 |
+
|
| 208 |
+
return f"https://huggingface.co/spaces/{space_name}", blog_text
|
| 209 |
+
|
| 210 |
+
except Exception as e:
|
| 211 |
+
return f"Error: {str(e)}", ""
|
| 212 |
|
| 213 |
+
def _generate_content(self, topic, space_name):
|
| 214 |
+
# Generate text
|
| 215 |
+
text_response = requests.post(
|
| 216 |
"https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.2",
|
| 217 |
headers=self.headers,
|
| 218 |
json={
|
|
|
|
| 220 |
"parameters": {"max_length": 2000}
|
| 221 |
}
|
| 222 |
)
|
| 223 |
+
blog_text = text_response.json()[0]['generated_text']
|
| 224 |
|
| 225 |
+
# Generate and upload images
|
|
|
|
| 226 |
image_urls = []
|
| 227 |
+
for idx in range(3):
|
| 228 |
+
img_response = requests.post(
|
|
|
|
| 229 |
"https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-xl-base-1.0",
|
| 230 |
headers=self.headers,
|
| 231 |
+
json={"inputs": f"Colorful modern digital art about {topic}, vibrant colors, abstract patterns, 4k resolution"}
|
| 232 |
)
|
| 233 |
+
image_path = f"image_{idx}.png"
|
| 234 |
+
upload_file(
|
| 235 |
+
path_or_fileobj=img_response.content,
|
| 236 |
+
path_in_repo=image_path,
|
| 237 |
+
repo_id=space_name,
|
| 238 |
+
repo_type="space"
|
| 239 |
)
|
| 240 |
+
image_urls.append(image_path)
|
| 241 |
time.sleep(1)
|
|
|
|
|
|
|
| 242 |
|
| 243 |
+
return blog_text, image_urls
|
| 244 |
+
|
| 245 |
+
def _create_design(self, content, image_urls, topic):
|
| 246 |
+
# Replace image placeholders
|
| 247 |
+
for idx, url in enumerate(image_urls):
|
| 248 |
+
content = content.replace(f"[IMAGE{idx+1}]", f'')
|
| 249 |
+
|
| 250 |
+
# Create styled HTML
|
| 251 |
+
return f"""
|
| 252 |
+
<!DOCTYPE html>
|
| 253 |
+
<html>
|
| 254 |
+
<head>
|
| 255 |
+
<title>{topic}</title>
|
| 256 |
+
<style>
|
| 257 |
+
:root {{
|
| 258 |
+
--primary: #ff6b6b;
|
| 259 |
+
--secondary: #4ecdc4;
|
| 260 |
+
--accent: #ffe66d;
|
| 261 |
+
}}
|
| 262 |
+
body {{
|
| 263 |
+
max-width: 800px;
|
| 264 |
+
margin: 0 auto;
|
| 265 |
+
padding: 40px;
|
| 266 |
+
font-family: 'Segoe UI', sans-serif;
|
| 267 |
+
background: linear-gradient(45deg, #f8f9fa, #ffffff);
|
| 268 |
+
line-height: 1.6;
|
| 269 |
+
}}
|
| 270 |
+
.header {{
|
| 271 |
+
text-align: center;
|
| 272 |
+
padding: 40px 0;
|
| 273 |
+
background: var(--primary);
|
| 274 |
+
color: white;
|
| 275 |
+
border-radius: 15px;
|
| 276 |
+
margin-bottom: 40px;
|
| 277 |
+
box-shadow: 0 4px 6px rgba(0,0,0,0.1);
|
| 278 |
+
}}
|
| 279 |
+
img {{
|
| 280 |
+
width: 100%;
|
| 281 |
+
border-radius: 15px;
|
| 282 |
+
margin: 25px 0;
|
| 283 |
+
border: 3px solid var(--secondary);
|
| 284 |
+
transition: transform 0.3s ease;
|
| 285 |
+
}}
|
| 286 |
+
img:hover {{
|
| 287 |
+
transform: scale(1.02);
|
| 288 |
+
}}
|
| 289 |
+
h1, h2, h3 {{
|
| 290 |
+
color: var(--primary);
|
| 291 |
+
margin-top: 30px;
|
| 292 |
+
}}
|
| 293 |
+
blockquote {{
|
| 294 |
+
border-left: 4px solid var(--accent);
|
| 295 |
+
padding: 15px 30px;
|
| 296 |
+
margin: 20px 0;
|
| 297 |
+
background: #fff9e6;
|
| 298 |
+
border-radius: 4px;
|
| 299 |
+
}}
|
| 300 |
+
a {{
|
| 301 |
+
color: var(--secondary);
|
| 302 |
+
text-decoration: none;
|
| 303 |
+
font-weight: bold;
|
| 304 |
+
}}
|
| 305 |
+
.content {{
|
| 306 |
+
background: white;
|
| 307 |
+
padding: 30px;
|
| 308 |
+
border-radius: 15px;
|
| 309 |
+
box-shadow: 0 2px 15px rgba(0,0,0,0.1);
|
| 310 |
+
}}
|
| 311 |
+
</style>
|
| 312 |
+
</head>
|
| 313 |
+
<body>
|
| 314 |
+
<div class="header">
|
| 315 |
+
<h1>{topic}</h1>
|
| 316 |
+
<p>Generated on {datetime.now().strftime("%B %d, %Y")}</p>
|
| 317 |
+
</div>
|
| 318 |
+
<div class="content">
|
| 319 |
+
{markdown.markdown(content)}
|
| 320 |
+
</div>
|
| 321 |
+
</body>
|
| 322 |
+
</html>
|
| 323 |
+
"""
|
| 324 |
+
|
| 325 |
+
def _upload_to_space(self, space_name, html_content, image_urls):
|
| 326 |
+
# Upload HTML
|
| 327 |
+
upload_file(
|
| 328 |
path_or_fileobj=html_content.encode(),
|
| 329 |
path_in_repo="index.html",
|
| 330 |
+
repo_id=space_name,
|
| 331 |
repo_type="space"
|
| 332 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 333 |
|
| 334 |
with gr.Blocks(theme=gr.themes.Soft()) as app:
|
| 335 |
+
gr.Markdown("# 🎨 AI Blog Designer")
|
| 336 |
|
| 337 |
with gr.Row():
|
| 338 |
with gr.Column():
|
| 339 |
topic_input = gr.Textbox(label="Blog Topic", placeholder="Enter your topic...")
|
| 340 |
+
generate_btn = gr.Button("Create Beautiful Blog", variant="primary")
|
| 341 |
|
| 342 |
with gr.Column():
|
| 343 |
+
gr.Markdown("## Your Blog Link")
|
| 344 |
+
blog_link = gr.Markdown("Waiting for generation...")
|
| 345 |
+
blog_preview = gr.HTML()
|
| 346 |
status = gr.Textbox(label="Status")
|
| 347 |
|
| 348 |
def process_request(topic):
|
| 349 |
+
creator = BlogCreator()
|
| 350 |
try:
|
| 351 |
+
url, content = creator.generate_blog(topic)
|
| 352 |
return (
|
| 353 |
+
f"[View Beautiful Blog]({url})",
|
| 354 |
+
f"<iframe src='{url}' style='width:100%; height:600px; border:none;'></iframe>",
|
| 355 |
+
"✅ Blog created successfully!"
|
| 356 |
)
|
| 357 |
except Exception as e:
|
| 358 |
+
return "❌ Error", "", str(e)
|
| 359 |
|
| 360 |
generate_btn.click(
|
| 361 |
fn=process_request,
|
| 362 |
inputs=topic_input,
|
| 363 |
+
outputs=[blog_link, blog_preview, status]
|
| 364 |
)
|
| 365 |
|
| 366 |
if __name__ == "__main__":
|