antiquesordo's picture
Update app.py
67a7445 verified
raw
history blame
2.09 kB
import gradio as gr
from transformers import AutoTokenizer, AutoModelForCausalLM
from pptx import Presentation
from pptx.util import Inches
import subprocess
import os
# Content Generation Function
def generate_content(prompt):
tokenizer = AutoTokenizer.from_pretrained("gpt2")
model = AutoModelForCausalLM.from_pretrained("gpt2")
inputs = tokenizer.encode(prompt + tokenizer.eos_token, return_tensors='pt')
outputs = model.generate(inputs, max_length=100, do_sample=True)
text = tokenizer.decode(outputs[0], skip_special_tokens=True)
return text
# Slide Design Function
def create_presentation(content_dict, output_file):
prs = Presentation()
# Create slides based on content_dict
# ...
prs.save(output_file)
# Output Conversion Function
def convert_to_pdf(pptx_file, pdf_file):
subprocess.run(['soffice', '--headless', '--convert-to', 'pdf', pptx_file, '--outdir', os.path.dirname(pdf_file)])
# Main Function
def main(title, subtitle, num_slides, slide_prompts):
slides = []
for prompt in slide_prompts:
content = generate_content(prompt)
slides.append({'title': content, 'content': content})
content_dict = {
'title': title,
'subtitle': subtitle,
'slides': slides
}
pptx_file = "output.pptx"
create_presentation(content_dict, pptx_file)
pdf_file = "output.pdf"
convert_to_pdf(pptx_file, pdf_file)
return pptx_file, pdf_file
# Gradio Interface
with gr.Blocks() as demo:
gr.Markdown("# Presentation Generator")
title = gr.Textbox(label="Presentation Title")
subtitle = gr.Textbox(label="Subtitle")
num_slides = gr.Number(label="Number of Slides", value=1)
slide_prompts = gr.Textbox(label="Slide Prompts (one per line)", lines=5)
generate_button = gr.Button("Generate Presentation")
output_pptx = gr.File(label="Download PPTX")
output_pdf = gr.File(label="Download PDF")
generate_button.click(
main,
inputs=[title, subtitle, num_slides, slide_prompts],
outputs=[output_pptx, output_pdf]
)
demo.launch()