Markdown-Studio / app.py
Nymbo's picture
max tokens again
ee88838 verified
raw
history blame
4.84 kB
import gradio as gr
import markdown
from markdown.extensions.tables import TableExtension
from markdown.extensions.fenced_code import FencedCodeExtension
from markdown.extensions.toc import TocExtension
from markdown.extensions.attr_list import AttrListExtension
from markdown.extensions.codehilite import CodeHiliteExtension
from transformers import pipeline
# Function to render markdown to HTML with extensions
def render_markdown(md_text):
print("[DEBUG] render_markdown called with input:", md_text) # Debug log for input
# Convert the input markdown text to HTML using various extensions for additional functionality
rendered_html = markdown.markdown(
md_text,
extensions=[
TableExtension(), # Enables rendering of tables in markdown
FencedCodeExtension(), # Supports fenced code blocks
TocExtension(baselevel=2), # Generates a table of contents starting at level 2
AttrListExtension(), # Allows adding attributes to markdown elements
CodeHiliteExtension(linenums=False, css_class="highlight"), # Syntax highlighting for code blocks
],
)
print("[DEBUG] Rendered HTML output:", rendered_html) # Debug log for output
return rendered_html
# Load the JinaAI ReaderLM-v2 model
model_name = "jinaai/ReaderLM-v2"
print("[DEBUG] Loading model:", model_name) # Debug log for model loading
html_converter = pipeline("text-generation", model=model_name) # Initialize the text-generation pipeline with the specified model
# Function to convert HTML to Markdown or JSON
def convert_html(html_input, output_format):
print("[DEBUG] convert_html called with inputs:", html_input, output_format) # Debug log for inputs
# Prepare the prompt for the model, specifying the desired output format (Markdown or JSON)
prompt = f"Convert the following HTML into {output_format}:\n\n{html_input}"
print("[DEBUG] Generated prompt:", prompt) # Debug log for the prompt
# Use the model to generate the conversion output
response = html_converter(prompt, max_length=99999, num_return_sequences=1)
print("[DEBUG] Model response:", response) # Debug log for model response
converted_output = response[0]['generated_text'] # Extract the generated text from the model response
# Remove the prompt text from the generated output and clean up the result
converted_output = converted_output.replace(prompt, "").strip()
print("[DEBUG] Converted output:", converted_output) # Debug log for the final output
return converted_output
# Creating the Gradio Interface
with gr.Blocks(theme="Nymbo/Nymbo_Theme") as demo:
# Tab for the Markdown live preview feature
with gr.Tab("Live Preview"):
gr.Markdown("# Markdown Suite") # Header for the tab
with gr.Row():
with gr.Column():
# Input textbox for entering Markdown text
md_input = gr.Textbox(
lines=20,
placeholder="Write your markdown here...",
label="Markdown Input",
elem_classes=["gr-textbox"]
)
with gr.Column():
# Output area to display the rendered HTML from the Markdown input
md_output = gr.HTML(label="Rendered Output", elem_classes=["gr-html"])
# Define the interaction: Update the HTML preview whenever the Markdown input changes
md_input.change(render_markdown, inputs=md_input, outputs=md_output)
# Tab for HTML to Markdown/JSON conversion feature
with gr.Tab("HTML to Markdown/JSON"):
gr.Markdown("# HTML to Markdown/JSON Converter") # Header for the tab
with gr.Row():
# Input textbox for raw HTML input
html_input = gr.Textbox(
lines=10,
placeholder="Paste your raw HTML here...",
label="Raw HTML Input"
)
# Radio buttons to select the output format (Markdown or JSON)
output_format = gr.Radio([
"Markdown",
"JSON"
], label="Output Format", value="Markdown")
# Output textbox to display the converted Markdown or JSON
converted_output = gr.Textbox(
lines=10,
label="Converted Output"
)
# Define the interaction: Convert HTML when the "Convert" button is clicked
convert_button = gr.Button("Convert")
convert_button.click(
convert_html, # Function to handle conversion
inputs=[html_input, output_format], # Inputs: Raw HTML and desired output format
outputs=converted_output # Output: Converted text
)
# Launch the app
print("[DEBUG] Launching the app") # Debug log for app launch
demo.launch()