Spaces:
Runtime error
Runtime error
import gradio as gr | |
from transformers import AutoTokenizer, AutoModelForCausalLM | |
from pptx import Presentation | |
from pptx.util import Inches | |
import subprocess | |
import os | |
# Specify the DeepSeek model name | |
DEEPSEEK_MODEL_NAME = "deepseek-ai/deepseek-llm-67b-chat" # Replace with the correct model name | |
# Initialize tokenizer and model globally | |
try: | |
tokenizer = AutoTokenizer.from_pretrained(DEEPSEEK_MODEL_NAME, padding_side='left') | |
model = AutoModelForCausalLM.from_pretrained(DEEPSEEK_MODEL_NAME) | |
except Exception as e: | |
print(f"Error loading model: {e}") | |
# Content Generation Function using DeepSeek | |
def generate_content(prompt): | |
if not tokenizer or not model: | |
return "Model not loaded properly." | |
inputs = tokenizer.encode(prompt + tokenizer.eos_token, return_tensors='pt') | |
outputs = model.generate(inputs, max_length=200, do_sample=True, temperature=0.7) | |
text = tokenizer.decode(outputs[0], skip_special_tokens=True) | |
return text | |
# Generate Slide Titles and Content from Description | |
def generate_slides_from_description(title, subtitle, description): | |
if not description: | |
return [] | |
# Generate slide titles | |
slide_titles_prompt = f"Generate 3 slide titles for a presentation titled '{title}' about '{description}'. Return the titles as a comma-separated list." | |
slide_titles = generate_content(slide_titles_prompt).split(",") | |
# Generate slide content for each title | |
slides = [] | |
for title_slide in slide_titles: | |
content_prompt = f"Generate detailed content for a slide titled '{title_slide.strip()}' about '{description}'." | |
content = generate_content(content_prompt) | |
slides.append({"title": title_slide.strip(), "content": content.strip()}) | |
return slides | |
# Slide Design Function | |
def create_presentation(title, subtitle, slides, output_dir): | |
prs = Presentation() | |
# Create title slide | |
title_slide_layout = prs.slide_layouts[0] | |
slide = prs.slides.add_slide(title_slide_layout) | |
slide.shapes.title.text = title | |
slide.placeholders[1].text = subtitle | |
# Create content slides | |
for slide_data in slides: | |
content_slide_layout = prs.slide_layouts[1] | |
slide = prs.slides.add_slide(content_slide_layout) | |
slide.shapes.title.text = slide_data["title"] | |
slide.placeholders[1].text = slide_data["content"] | |
output_file = os.path.join(output_dir, "output.pptx") | |
prs.save(output_file) | |
return output_file | |
# Output Conversion Function | |
def convert_to_pdf(pptx_file, pdf_file): | |
try: | |
subprocess.run(['soffice', '--headless', '--convert-to', 'pdf', pptx_file, '--outdir', os.path.dirname(pdf_file)]) | |
except Exception as e: | |
print(f"Error converting to PDF: {e}") | |
# Main Function | |
def main(title, subtitle, description): | |
if not title or not description: | |
return "Please provide a title and description.", "Please provide a title and description." | |
# Generate slides from description | |
slides = generate_slides_from_description(title, subtitle, description) | |
if not slides: | |
return "No slides generated.", "No slides generated." | |
# Create presentation | |
output_dir = os.getcwd() | |
pptx_file = create_presentation(title, subtitle, slides, output_dir) | |
# Convert to PDF | |
pdf_file = os.path.join(output_dir, "output.pdf") | |
convert_to_pdf(pptx_file, pdf_file) | |
# Return file paths for download | |
return pptx_file, pdf_file | |
# Gradio Interface | |
with gr.Blocks() as demo: | |
gr.Markdown("# Presentation Generator with DeepSeek") | |
title = gr.Textbox(label="Presentation Title", placeholder="Enter the title of your presentation") | |
subtitle = gr.Textbox(label="Subtitle", placeholder="Enter a subtitle (optional)") | |
description = gr.Textbox(label="Presentation Description", placeholder="Describe the purpose and content of your presentation") | |
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, description], | |
outputs=[output_pptx, output_pdf] | |
) | |
demo.launch() |