Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -5,57 +5,95 @@ from pptx.util import Inches
|
|
5 |
import subprocess
|
6 |
import os
|
7 |
|
8 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
def generate_content(prompt):
|
10 |
-
tokenizer
|
11 |
-
|
12 |
inputs = tokenizer.encode(prompt + tokenizer.eos_token, return_tensors='pt')
|
13 |
-
outputs = model.generate(inputs, max_length=
|
14 |
text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
15 |
return text
|
16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
# Slide Design Function
|
18 |
-
def create_presentation(
|
19 |
prs = Presentation()
|
20 |
-
# Create
|
21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
prs.save(output_file)
|
|
|
23 |
|
24 |
# Output Conversion Function
|
25 |
def convert_to_pdf(pptx_file, pdf_file):
|
26 |
-
|
|
|
|
|
|
|
27 |
|
28 |
# Main Function
|
29 |
-
def main(title, subtitle,
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
pdf_file = "output.pdf"
|
42 |
convert_to_pdf(pptx_file, pdf_file)
|
|
|
43 |
return pptx_file, pdf_file
|
44 |
|
45 |
# Gradio Interface
|
46 |
with gr.Blocks() as demo:
|
47 |
-
gr.Markdown("# Presentation Generator")
|
48 |
-
title = gr.Textbox(label="Presentation Title")
|
49 |
-
subtitle = gr.Textbox(label="Subtitle")
|
50 |
-
|
51 |
-
slide_prompts = gr.Textbox(label="Slide Prompts (one per line)", lines=5)
|
52 |
generate_button = gr.Button("Generate Presentation")
|
53 |
output_pptx = gr.File(label="Download PPTX")
|
54 |
output_pdf = gr.File(label="Download PDF")
|
55 |
|
56 |
generate_button.click(
|
57 |
main,
|
58 |
-
inputs=[title, subtitle,
|
59 |
outputs=[output_pptx, output_pdf]
|
60 |
)
|
61 |
|
|
|
5 |
import subprocess
|
6 |
import os
|
7 |
|
8 |
+
# Specify the DeepSeek model name
|
9 |
+
DEEPSEEK_MODEL_NAME = "deepseek-ai/deepseek-llm-67b-chat" # Replace with the correct model name
|
10 |
+
|
11 |
+
# Initialize tokenizer and model globally
|
12 |
+
try:
|
13 |
+
tokenizer = AutoTokenizer.from_pretrained(DEEPSEEK_MODEL_NAME, padding_side='left')
|
14 |
+
model = AutoModelForCausalLM.from_pretrained(DEEPSEEK_MODEL_NAME)
|
15 |
+
except Exception as e:
|
16 |
+
print(f"Error loading model: {e}")
|
17 |
+
|
18 |
+
# Content Generation Function using DeepSeek
|
19 |
def generate_content(prompt):
|
20 |
+
if not tokenizer or not model:
|
21 |
+
return "Model not loaded properly."
|
22 |
inputs = tokenizer.encode(prompt + tokenizer.eos_token, return_tensors='pt')
|
23 |
+
outputs = model.generate(inputs, max_length=200, do_sample=True, temperature=0.7)
|
24 |
text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
25 |
return text
|
26 |
|
27 |
+
# Generate Slide Titles and Content from Description
|
28 |
+
def generate_slides_from_description(title, subtitle, description):
|
29 |
+
if not description:
|
30 |
+
return []
|
31 |
+
# Generate slide titles
|
32 |
+
slide_titles_prompt = f"Generate 3 slide titles for a presentation titled '{title}' about '{description}'. Return the titles as a comma-separated list."
|
33 |
+
slide_titles = generate_content(slide_titles_prompt).split(",")
|
34 |
+
# Generate slide content for each title
|
35 |
+
slides = []
|
36 |
+
for title_slide in slide_titles:
|
37 |
+
content_prompt = f"Generate detailed content for a slide titled '{title_slide.strip()}' about '{description}'."
|
38 |
+
content = generate_content(content_prompt)
|
39 |
+
slides.append({"title": title_slide.strip(), "content": content.strip()})
|
40 |
+
return slides
|
41 |
+
|
42 |
# Slide Design Function
|
43 |
+
def create_presentation(title, subtitle, slides, output_dir):
|
44 |
prs = Presentation()
|
45 |
+
# Create title slide
|
46 |
+
title_slide_layout = prs.slide_layouts[0]
|
47 |
+
slide = prs.slides.add_slide(title_slide_layout)
|
48 |
+
slide.shapes.title.text = title
|
49 |
+
slide.placeholders[1].text = subtitle
|
50 |
+
# Create content slides
|
51 |
+
for slide_data in slides:
|
52 |
+
content_slide_layout = prs.slide_layouts[1]
|
53 |
+
slide = prs.slides.add_slide(content_slide_layout)
|
54 |
+
slide.shapes.title.text = slide_data["title"]
|
55 |
+
slide.placeholders[1].text = slide_data["content"]
|
56 |
+
output_file = os.path.join(output_dir, "output.pptx")
|
57 |
prs.save(output_file)
|
58 |
+
return output_file
|
59 |
|
60 |
# Output Conversion Function
|
61 |
def convert_to_pdf(pptx_file, pdf_file):
|
62 |
+
try:
|
63 |
+
subprocess.run(['soffice', '--headless', '--convert-to', 'pdf', pptx_file, '--outdir', os.path.dirname(pdf_file)])
|
64 |
+
except Exception as e:
|
65 |
+
print(f"Error converting to PDF: {e}")
|
66 |
|
67 |
# Main Function
|
68 |
+
def main(title, subtitle, description):
|
69 |
+
if not title or not description:
|
70 |
+
return "Please provide a title and description.", "Please provide a title and description."
|
71 |
+
# Generate slides from description
|
72 |
+
slides = generate_slides_from_description(title, subtitle, description)
|
73 |
+
if not slides:
|
74 |
+
return "No slides generated.", "No slides generated."
|
75 |
+
# Create presentation
|
76 |
+
output_dir = os.getcwd()
|
77 |
+
pptx_file = create_presentation(title, subtitle, slides, output_dir)
|
78 |
+
# Convert to PDF
|
79 |
+
pdf_file = os.path.join(output_dir, "output.pdf")
|
|
|
80 |
convert_to_pdf(pptx_file, pdf_file)
|
81 |
+
# Return file paths for download
|
82 |
return pptx_file, pdf_file
|
83 |
|
84 |
# Gradio Interface
|
85 |
with gr.Blocks() as demo:
|
86 |
+
gr.Markdown("# Presentation Generator with DeepSeek")
|
87 |
+
title = gr.Textbox(label="Presentation Title", placeholder="Enter the title of your presentation")
|
88 |
+
subtitle = gr.Textbox(label="Subtitle", placeholder="Enter a subtitle (optional)")
|
89 |
+
description = gr.Textbox(label="Presentation Description", placeholder="Describe the purpose and content of your presentation")
|
|
|
90 |
generate_button = gr.Button("Generate Presentation")
|
91 |
output_pptx = gr.File(label="Download PPTX")
|
92 |
output_pdf = gr.File(label="Download PDF")
|
93 |
|
94 |
generate_button.click(
|
95 |
main,
|
96 |
+
inputs=[title, subtitle, description],
|
97 |
outputs=[output_pptx, output_pdf]
|
98 |
)
|
99 |
|