Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
|
2 |
import gradio as gr
|
3 |
import os
|
4 |
import time
|
@@ -161,4 +161,198 @@ with gr.Blocks(title="AI Blog Generator", theme=gr.themes.Soft()) as app:
|
|
161 |
)
|
162 |
|
163 |
if __name__ == "__main__":
|
164 |
-
app.launch(share=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
'''
|
2 |
import gradio as gr
|
3 |
import os
|
4 |
import time
|
|
|
161 |
)
|
162 |
|
163 |
if __name__ == "__main__":
|
164 |
+
app.launch(share=False)'''
|
165 |
+
|
166 |
+
import gradio as gr
|
167 |
+
import os
|
168 |
+
import time
|
169 |
+
import requests
|
170 |
+
import re
|
171 |
+
from datetime import datetime
|
172 |
+
from dotenv import load_dotenv
|
173 |
+
from huggingface_hub import HfApi, upload_file
|
174 |
+
import uuid
|
175 |
+
import markdown
|
176 |
+
|
177 |
+
load_dotenv()
|
178 |
+
|
179 |
+
# Hugging Face configuration
|
180 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
181 |
+
HEADERS = {"Authorization": f"Bearer {HF_TOKEN}"}
|
182 |
+
HF_USERNAME = "your_hf_username" # Replace with your Hugging Face username
|
183 |
+
|
184 |
+
def generate_blog_content(topic, tone="professional"):
|
185 |
+
try:
|
186 |
+
prompt = f"""<s>[INST] Write a {tone} blog post about {topic} with:
|
187 |
+
- Catchy title with emojis
|
188 |
+
- 3 sections with subsections
|
189 |
+
- 2 image placeholders [IMAGE1][IMAGE2]
|
190 |
+
- Bullet points and statistics
|
191 |
+
- Conclusion with call-to-action
|
192 |
+
Use markdown formatting [/INST]</s>"""
|
193 |
+
|
194 |
+
response = requests.post(
|
195 |
+
"https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.2",
|
196 |
+
headers=HEADERS,
|
197 |
+
json={
|
198 |
+
"inputs": prompt,
|
199 |
+
"parameters": {"max_length": 2000, "temperature": 0.7}
|
200 |
+
}
|
201 |
+
)
|
202 |
+
|
203 |
+
if response.status_code == 503:
|
204 |
+
estimate = response.json().get('estimated_time', 30)
|
205 |
+
time.sleep(estimate)
|
206 |
+
response = requests.post(TEXT_API_URL, headers=HEADERS, json=payload)
|
207 |
+
|
208 |
+
response.raise_for_status()
|
209 |
+
return response.json()[0]['generated_text']
|
210 |
+
|
211 |
+
except Exception as e:
|
212 |
+
return f"Error generating content: {str(e)}"
|
213 |
+
|
214 |
+
def generate_and_host_blog(topic, tone):
|
215 |
+
try:
|
216 |
+
# Generate unique space name
|
217 |
+
space_id = f"blog-{uuid.uuid4().hex[:8]}"
|
218 |
+
space_name = f"{HF_USERNAME}/{space_id}"
|
219 |
+
|
220 |
+
# Create Hugging Face Space
|
221 |
+
api = HfApi(token=HF_TOKEN)
|
222 |
+
api.create_repo(
|
223 |
+
repo_id=space_name,
|
224 |
+
repo_type="space",
|
225 |
+
space_sdk="static",
|
226 |
+
private=False
|
227 |
+
)
|
228 |
+
|
229 |
+
# Generate content
|
230 |
+
content = generate_blog_content(topic, tone)
|
231 |
+
image_urls = []
|
232 |
+
|
233 |
+
# Generate and upload images
|
234 |
+
for idx in range(2):
|
235 |
+
response = requests.post(
|
236 |
+
"https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-xl-base-1.0",
|
237 |
+
headers=HEADERS,
|
238 |
+
json={"inputs": f"Colorful digital art about {topic}, vibrant colors, abstract, 4k"}
|
239 |
+
)
|
240 |
+
upload_file(
|
241 |
+
path_or_fileobj=response.content,
|
242 |
+
path_in_repo=f"image_{idx}.png",
|
243 |
+
repo_id=space_name,
|
244 |
+
repo_type="space"
|
245 |
+
)
|
246 |
+
image_urls.append(f"image_{idx}.png")
|
247 |
+
time.sleep(1)
|
248 |
+
|
249 |
+
# Create styled HTML
|
250 |
+
html_content = f"""
|
251 |
+
<!DOCTYPE html>
|
252 |
+
<html>
|
253 |
+
<head>
|
254 |
+
<title>{topic}</title>
|
255 |
+
<style>
|
256 |
+
:root {{
|
257 |
+
--primary: #ff6b6b;
|
258 |
+
--secondary: #4ecdc4;
|
259 |
+
--accent: #ffe66d;
|
260 |
+
}}
|
261 |
+
body {{
|
262 |
+
max-width: 800px;
|
263 |
+
margin: 0 auto;
|
264 |
+
padding: 40px;
|
265 |
+
font-family: 'Segoe UI', sans-serif;
|
266 |
+
background: linear-gradient(135deg, #f8f9fa 0%, #ffffff 100%);
|
267 |
+
line-height: 1.6;
|
268 |
+
}}
|
269 |
+
.header {{
|
270 |
+
text-align: center;
|
271 |
+
padding: 40px;
|
272 |
+
background: var(--primary);
|
273 |
+
color: white;
|
274 |
+
border-radius: 20px;
|
275 |
+
margin-bottom: 40px;
|
276 |
+
box-shadow: 0 4px 15px rgba(0,0,0,0.1);
|
277 |
+
}}
|
278 |
+
img {{
|
279 |
+
width: 100%;
|
280 |
+
border-radius: 15px;
|
281 |
+
margin: 25px 0;
|
282 |
+
border: 3px solid var(--secondary);
|
283 |
+
transition: transform 0.3s ease;
|
284 |
+
}}
|
285 |
+
img:hover {{
|
286 |
+
transform: scale(1.02);
|
287 |
+
}}
|
288 |
+
h1 {{
|
289 |
+
font-size: 2.5em;
|
290 |
+
margin-bottom: 10px;
|
291 |
+
}}
|
292 |
+
.content {{
|
293 |
+
background: white;
|
294 |
+
padding: 30px;
|
295 |
+
border-radius: 20px;
|
296 |
+
box-shadow: 0 2px 20px rgba(0,0,0,0.08);
|
297 |
+
}}
|
298 |
+
</style>
|
299 |
+
</head>
|
300 |
+
<body>
|
301 |
+
<div class="header">
|
302 |
+
<h1>{topic} π</h1>
|
303 |
+
<p>Generated on {datetime.now().strftime("%B %d, %Y")}</p>
|
304 |
+
</div>
|
305 |
+
<div class="content">
|
306 |
+
{markdown.markdown(content.replace("[IMAGE1]", f'').replace("[IMAGE2]", f''))}
|
307 |
+
</div>
|
308 |
+
</body>
|
309 |
+
</html>
|
310 |
+
"""
|
311 |
+
|
312 |
+
# Upload HTML
|
313 |
+
upload_file(
|
314 |
+
path_or_fileobj=html_content.encode(),
|
315 |
+
path_in_repo="index.html",
|
316 |
+
repo_id=space_name,
|
317 |
+
repo_type="space"
|
318 |
+
)
|
319 |
+
|
320 |
+
return f"https://huggingface.co/spaces/{space_name}", content
|
321 |
+
|
322 |
+
except Exception as e:
|
323 |
+
return f"Error: {str(e)}", ""
|
324 |
+
|
325 |
+
# Colorful Gradio interface
|
326 |
+
with gr.Blocks(theme=gr.themes.Glass(primary_hue="purple", secondary_hue="pink")) as app:
|
327 |
+
gr.Markdown("# π¨ **AI Blog Designer**")
|
328 |
+
|
329 |
+
with gr.Row(variant="panel"):
|
330 |
+
with gr.Column(scale=1):
|
331 |
+
gr.Markdown("## π οΈ Configuration")
|
332 |
+
topic_input = gr.Textbox(label="Blog Topic", placeholder="Enter your topic...")
|
333 |
+
tone_input = gr.Dropdown(
|
334 |
+
["professional", "casual", "technical", "storytelling"],
|
335 |
+
label="Writing Style",
|
336 |
+
value="professional",
|
337 |
+
interactive=True
|
338 |
+
)
|
339 |
+
generate_btn = gr.Button("β¨ Generate Blog", variant="primary")
|
340 |
+
|
341 |
+
with gr.Column(scale=2):
|
342 |
+
gr.Markdown("## π Live Preview")
|
343 |
+
with gr.Tabs():
|
344 |
+
with gr.TabItem("π Content"):
|
345 |
+
blog_output = gr.Markdown()
|
346 |
+
with gr.TabItem("π Blog Link"):
|
347 |
+
blog_link = gr.Markdown("Your blog link will appear here...")
|
348 |
+
with gr.Accordion("π¦ Status", open=False):
|
349 |
+
status_output = gr.Textbox(label="Generation Status")
|
350 |
+
|
351 |
+
generate_btn.click(
|
352 |
+
fn=generate_and_host_blog,
|
353 |
+
inputs=[topic_input, tone_input],
|
354 |
+
outputs=[blog_link, blog_output],
|
355 |
+
)
|
356 |
+
|
357 |
+
if __name__ == "__main__":
|
358 |
+
app.launch(share=True)
|