Update app.py
Browse files
app.py
CHANGED
|
@@ -162,121 +162,3 @@ with gr.Blocks(title="AI Blog Generator", theme=gr.themes.Soft()) as app:
|
|
| 162 |
|
| 163 |
if __name__ == "__main__":
|
| 164 |
app.launch(share=False)'''
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
import gradio as gr
|
| 168 |
-
import os
|
| 169 |
-
import time
|
| 170 |
-
import tempfile
|
| 171 |
-
import requests
|
| 172 |
-
from PIL import Image
|
| 173 |
-
from io import BytesIO
|
| 174 |
-
import re
|
| 175 |
-
from datetime import datetime
|
| 176 |
-
from dotenv import load_dotenv
|
| 177 |
-
|
| 178 |
-
# Load environment variables
|
| 179 |
-
load_dotenv()
|
| 180 |
-
|
| 181 |
-
# Hugging Face configuration
|
| 182 |
-
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 183 |
-
TEXT_API_URL = "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.2"
|
| 184 |
-
IMAGE_API_URL = "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-xl-base-1.0"
|
| 185 |
-
HEADERS = {"Authorization": f"Bearer {HF_TOKEN}"}
|
| 186 |
-
|
| 187 |
-
# Generate blog content
|
| 188 |
-
def generate_blog_content(topic, tone="professional", length="long"):
|
| 189 |
-
try:
|
| 190 |
-
current_date = datetime.now().strftime("%B %d, %Y")
|
| 191 |
-
prompt = f"""<s>[INST] Write a {tone} blog post about {topic} with:
|
| 192 |
-
- Title and subtitle
|
| 193 |
-
- Introduction with statistics
|
| 194 |
-
- 3-4 detailed sections with subheadings
|
| 195 |
-
- A conclusion with key takeaways
|
| 196 |
-
- Markdown formatting with colorful HTML & CSS
|
| 197 |
-
- Published date: {current_date}
|
| 198 |
-
- At least 1500 words [/INST]</s>"""
|
| 199 |
-
|
| 200 |
-
payload = {
|
| 201 |
-
"inputs": prompt,
|
| 202 |
-
"parameters": {"max_new_tokens": 2048, "temperature": 0.7, "return_full_text": False}
|
| 203 |
-
}
|
| 204 |
-
|
| 205 |
-
response = requests.post(TEXT_API_URL, headers=HEADERS, json=payload)
|
| 206 |
-
response.raise_for_status()
|
| 207 |
-
return response.json()[0]['generated_text']
|
| 208 |
-
except Exception as e:
|
| 209 |
-
return f"Error generating content: {str(e)}"
|
| 210 |
-
|
| 211 |
-
# Generate feature image
|
| 212 |
-
def generate_featured_image(topic):
|
| 213 |
-
try:
|
| 214 |
-
prompt = f"A vibrant, eye-catching blog cover image about {topic}, ultra HD, artistic"
|
| 215 |
-
payload = {"inputs": prompt, "parameters": {"height": 1024, "width": 1024, "num_inference_steps": 30}}
|
| 216 |
-
response = requests.post(IMAGE_API_URL, headers=HEADERS, json=payload)
|
| 217 |
-
response.raise_for_status()
|
| 218 |
-
image = Image.open(BytesIO(response.content))
|
| 219 |
-
temp_img = tempfile.NamedTemporaryFile(delete=False, suffix=".png")
|
| 220 |
-
image.save(temp_img.name)
|
| 221 |
-
return temp_img.name
|
| 222 |
-
except Exception as e:
|
| 223 |
-
return None
|
| 224 |
-
|
| 225 |
-
# Convert blog into a colorful web page
|
| 226 |
-
def generate_webpage(blog_content, title, author):
|
| 227 |
-
html_template = f"""
|
| 228 |
-
<html>
|
| 229 |
-
<head>
|
| 230 |
-
<style>
|
| 231 |
-
body {{ font-family: Arial, sans-serif; background-color: #f5f5f5; padding: 20px; }}
|
| 232 |
-
.container {{ max-width: 800px; margin: auto; background: white; padding: 20px; border-radius: 10px; }}
|
| 233 |
-
h1 {{ color: #2c3e50; }}
|
| 234 |
-
h2, h3 {{ color: #2980b9; }}
|
| 235 |
-
p {{ color: #333; line-height: 1.6; }}
|
| 236 |
-
</style>
|
| 237 |
-
</head>
|
| 238 |
-
<body>
|
| 239 |
-
<div class='container'>
|
| 240 |
-
<h1>{title}</h1>
|
| 241 |
-
<h3>by {author}</h3>
|
| 242 |
-
{blog_content.replace("\n", "<br>")}
|
| 243 |
-
</div>
|
| 244 |
-
</body>
|
| 245 |
-
</html>
|
| 246 |
-
"""
|
| 247 |
-
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".html", mode="w", encoding="utf-8")
|
| 248 |
-
temp_file.write(html_template)
|
| 249 |
-
temp_file.close()
|
| 250 |
-
return temp_file.name
|
| 251 |
-
|
| 252 |
-
# Generate blog
|
| 253 |
-
|
| 254 |
-
def generate_blog(topic, tone, author_name):
|
| 255 |
-
status_updates = ["🚀 Generating blog..."]
|
| 256 |
-
blog_content = generate_blog_content(topic, tone)
|
| 257 |
-
title = re.search(r'^#\s+(.+)$', blog_content, re.MULTILINE).group(1).strip() if re.search(r'^#\s+(.+)$', blog_content, re.MULTILINE) else topic
|
| 258 |
-
status_updates.append("🖼️ Generating featured image...")
|
| 259 |
-
image_path = generate_featured_image(topic)
|
| 260 |
-
status_updates.append("🌍 Generating website...")
|
| 261 |
-
web_page = generate_webpage(blog_content, title, author_name)
|
| 262 |
-
status_updates.append("✅ Blog Ready!")
|
| 263 |
-
return blog_content, title, "\n".join(status_updates), web_page
|
| 264 |
-
|
| 265 |
-
# Gradio UI
|
| 266 |
-
with gr.Blocks() as app:
|
| 267 |
-
gr.Markdown("# 🌟 AI Blog Generator & Web Designer")
|
| 268 |
-
with gr.Row():
|
| 269 |
-
with gr.Column():
|
| 270 |
-
topic_input = gr.Textbox(label="Blog Topic")
|
| 271 |
-
tone_input = gr.Dropdown(["professional", "casual", "technical", "storytelling"], label="Writing Style", value="professional")
|
| 272 |
-
author_input = gr.Textbox(label="Author Name")
|
| 273 |
-
generate_btn = gr.Button("Generate Blog")
|
| 274 |
-
with gr.Column():
|
| 275 |
-
title_output = gr.Textbox(label="Generated Title")
|
| 276 |
-
blog_output = gr.Markdown()
|
| 277 |
-
status_output = gr.Textbox(label="Status")
|
| 278 |
-
web_page_output = gr.File(label="Download Website")
|
| 279 |
-
generate_btn.click(generate_blog, inputs=[topic_input, tone_input, author_input], outputs=[blog_output, title_output, status_output, web_page_output])
|
| 280 |
-
|
| 281 |
-
if __name__ == "__main__":
|
| 282 |
-
app.launch(share=True)
|
|
|
|
| 162 |
|
| 163 |
if __name__ == "__main__":
|
| 164 |
app.launch(share=False)'''
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|