|
import datetime |
|
import gradio as gr |
|
import cv2 |
|
import uuid |
|
|
|
import subprocess |
|
|
|
def commit_and_push(file_path: str): |
|
|
|
subprocess.call(['git', 'add', "."]) |
|
|
|
|
|
subprocess.call(['git', 'commit', '-m', f'New blog post {unique_id}']) |
|
|
|
|
|
subprocess.call(['git', 'push']) |
|
|
|
return "Markdown post committed and pushed successfully!" |
|
|
|
|
|
def generate_markdown_post(title: str, content: str, image: gr.Image): |
|
|
|
now = datetime.datetime.now() |
|
|
|
|
|
date_string = now.strftime("%Y-%m-%d %H:%M:%S %z") |
|
subdate_string = now.strftime("%Y-%m-%d") |
|
|
|
|
|
global unique_id |
|
unique_id = str(uuid.uuid4().hex)[:6] |
|
|
|
|
|
|
|
filename = f"_posts/{subdate_string}-{unique_id}.markdown" |
|
|
|
try: |
|
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) |
|
cv2.imwrite(f"blog_assets/{unique_id}.jpg",image) |
|
src = f'<img src="/blog_assets/{unique_id}.jpg">' |
|
except: |
|
src = "" |
|
|
|
|
|
markdown_content = f"""--- |
|
layout: post |
|
title: "{title}" |
|
date: {date_string}+200 |
|
categories: jekyll update |
|
--- |
|
{src} |
|
|
|
{content} |
|
""" |
|
|
|
|
|
with open(filename, "w") as f: |
|
f.write(markdown_content) |
|
|
|
commit_and_push(filename) |
|
|
|
return f"New Blog: {title}" |
|
|
|
title = "Markdown Blog Post Generator" |
|
description = "This app generates a Markdown blog post with the given title, content, and image." |
|
inputs = [ |
|
gr.Textbox(label="Enter the blog post title:"), |
|
gr.Textbox(label="Enter the content:"), |
|
gr.Image(label="Upload an image") |
|
] |
|
outputs = gr.Textbox() |
|
gr.Interface(fn=generate_markdown_post, inputs=inputs, outputs=outputs, title=title, description=description).launch() |
|
|