import sys
from generate_mindmap import generate_mindmap
import gradio as gr
import subprocess

def generate(mindmap_markdown):
    mindmap_svg, mindmap_pdf = generate_mindmap(mindmap_markdown)
    with open(mindmap_svg, 'rb') as f:
        svg_mindmap = f.read()
    with open(mindmap_pdf, 'rb') as f:
        pdf_mindmap = f.read()
    return str(svg_mindmap), str(pdf_mindmap)

theme = gr.themes.Soft(
    primary_hue="purple",
    secondary_hue="cyan",
    neutral_hue="slate",
    font=[gr.themes.GoogleFont('Syne'), gr.themes.GoogleFont('Poppins')],
)

with gr.Blocks(theme=theme, title="Binary Biology") as app:
    with gr.Row():
        with gr.Column():
            mindmap_markdown = gr.Textbox(label="Mindmap Markdown", placeholder="Enter the markdown for the mindmap", lines=10)
            btn_generate_mindmap = gr.Button(value="Generate Mindmap")  # Renamed button here
        with gr.Column():
            svg_mindmap = gr.TextArea(label="SVG Mindmap", placeholder="The SVG representation of the mindmap", lines=10)
            pdf_mindmap = gr.TextArea(label="PDF Mindmap", placeholder="The PDF representation of the mindmap", lines=10)
    btn_generate_mindmap.click(generate, inputs=[mindmap_markdown], outputs=[svg_mindmap, pdf_mindmap])

if __name__ == "__main__":
    subprocess.run(['apt-get', 'update'])
    subprocess.run(['apt-get', 'install', '-y', 'graphviz'], check=True)
    print("Graphviz installed successfully")
    print("Graphviz loaded successfully")
    app.queue(default_concurrency_limit=25).launch(show_error=True)