kshitij10000 commited on
Commit
2bd3978
·
1 Parent(s): b3ddf2e

Upload gui.py

Browse files
Files changed (1) hide show
  1. gui.py +87 -0
gui.py ADDED
@@ -0,0 +1,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import openai
2
+ import json
3
+ import base64
4
+ from pptx import Presentation
5
+ import streamlit as st
6
+
7
+ # Set your OpenAI API key
8
+ openai.api_key = "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"
9
+
10
+ # Streamlit app
11
+ st.title("PowerPoint Presentation Generator")
12
+
13
+ # Input for presentation topic
14
+ presentation_title = st.text_input("Enter your presentation topic:")
15
+ no_of_pages = st.number_input("number of slides you want")
16
+ least_c = st.number_input("minimum points in each slide")
17
+ max_c = st.number_input("maximum points in each slide")
18
+ if st.button("Generate Presentation"):
19
+ # Check if the user has entered a topic
20
+ if presentation_title:
21
+ question = (
22
+ f"generate a {no_of_pages} slide presentation for the topic {presentation_title}."
23
+ f" Each slide should have {{header}}, {{content}}, should have at least {least_c} points and max {max_c} points in content. Return as JSON"
24
+ )
25
+
26
+ query_json = {
27
+ "input_text": question,
28
+ "output_format": "json",
29
+ "json_structure": {"slides": "{{presentation_slides}}"},
30
+ }
31
+
32
+ # Send the query to OpenAI
33
+ completion = openai.ChatCompletion.create(
34
+ model="gpt-3.5-turbo",
35
+ messages=[{"role": "user", "content": json.dumps(query_json)}],
36
+ )
37
+
38
+ try:
39
+ response = json.loads(completion.choices[0].message.content)
40
+
41
+ slide_data = response["slides"]
42
+
43
+ prs = Presentation()
44
+
45
+ from pptx.util import Pt # Import the Pt class from pptx.util
46
+ from pptx.enum.text import PP_ALIGN # Import text alignment options
47
+
48
+ # Iterate through slide data and populate the presentation
49
+ for slide in slide_data:
50
+ slide_layout = prs.slide_layouts[1]
51
+ new_slide = prs.slides.add_slide(slide_layout)
52
+
53
+ if slide["header"]:
54
+ title = new_slide.shapes.title
55
+ title.text = slide["header"]
56
+
57
+ if slide["content"]:
58
+ shapes = new_slide.shapes
59
+ body_shape = shapes.placeholders[1]
60
+ tf = body_shape.text_frame
61
+
62
+ # Join the list of content into a single string with line breaks
63
+ content_text = "\n".join(slide["content"])
64
+
65
+ p = tf.add_paragraph()
66
+ p.text = content_text
67
+
68
+ # Set the spacing between lines using Pt (point size)
69
+ p.space_after = Pt(14) # Adjust the spacing between lines
70
+
71
+ # Set text direction to horizontal (left-to-right)
72
+ p.alignment = PP_ALIGN.LEFT
73
+
74
+ # Save the presentation in PPTX format with the topic name as the file name
75
+ presentation_filename = f"{presentation_title}.pptx"
76
+ prs.save(presentation_filename)
77
+
78
+ # Provide a direct download link
79
+ with open(presentation_filename, "rb") as file:
80
+ st.markdown(
81
+ f"### [Download {presentation_title}]"
82
+ f"(data:application/octet-stream;base64,{base64.b64encode(file.read()).decode()})"
83
+ )
84
+ except json.JSONDecodeError as e:
85
+ st.error("Error parsing JSON response from OpenAI.")
86
+ else:
87
+ st.warning("Please enter a presentation topic.")