Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,163 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from pptx import Presentation
|
3 |
+
from pptx.util import Inches, Pt
|
4 |
+
from pptx.dml.color import RGBColor
|
5 |
+
import io, tempfile, os, re
|
6 |
+
from transformers import pipeline
|
7 |
+
|
8 |
+
# ββ CONFIG βββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
9 |
+
st.set_page_config(page_title="PPTX Smart Enhancer", layout="wide")
|
10 |
+
MODEL_ID = "Faisalkhany/newfinetune" # β your HF model
|
11 |
+
pipe = pipeline("text2text-generation", model=MODEL_ID)
|
12 |
+
|
13 |
+
TEMPLATES = {
|
14 |
+
"Office Default": {
|
15 |
+
"font": "Calibri",
|
16 |
+
"colors": {"primary":"#2B579A","secondary":"#5B9BD5","text":"#000000","background":"#FFFFFF"},
|
17 |
+
"sizes": {"title":44,"heading":32,"body":18}, "background":True
|
18 |
+
},
|
19 |
+
"Modern Office": {
|
20 |
+
"font": "Segoe UI",
|
21 |
+
"colors": {"primary":"#404040","secondary":"#A5A5A5","text":"#FFFFFF","background":"#121212"},
|
22 |
+
"sizes": {"title":36,"heading":24,"body":16}, "background":True
|
23 |
+
},
|
24 |
+
"Corporate": {
|
25 |
+
"font":"Arial",
|
26 |
+
"colors":{"primary":"#1976D2","secondary":"#BBDEFB","text":"#212121","background":"#FAFAFA"},
|
27 |
+
"sizes":{"title":40,"heading":28,"body":18}, "background":False
|
28 |
+
}
|
29 |
+
}
|
30 |
+
FONT_CHOICES = {f:f for f in ["Arial","Calibri","Times New Roman","Segoe UI","Verdana","Georgia"]}
|
31 |
+
|
32 |
+
# ββ HELPERS ββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
33 |
+
def chunk_slide_text(text, max_words=200):
|
34 |
+
words = text.split()
|
35 |
+
return [" ".join(words[i:i+max_words]) for i in range(0, len(words), max_words)]
|
36 |
+
|
37 |
+
def get_ai_response(prompt: str) -> str:
|
38 |
+
out = pipe(prompt, max_length=512, truncation=True)
|
39 |
+
return out[0]["generated_text"]
|
40 |
+
|
41 |
+
def extract_slide_text(slide) -> str:
|
42 |
+
parts = []
|
43 |
+
for shp in slide.shapes:
|
44 |
+
if hasattr(shp, "text_frame"):
|
45 |
+
for p in shp.text_frame.paragraphs:
|
46 |
+
if p.text.strip():
|
47 |
+
parts.append(p.text.strip())
|
48 |
+
return "\n".join(parts)
|
49 |
+
|
50 |
+
def split_formatted_runs(text):
|
51 |
+
segments, cur, bold, italic = [], [], False, False
|
52 |
+
parts = re.split(r'(\*|_)', text)
|
53 |
+
for tok in parts:
|
54 |
+
if tok in ("*","_"):
|
55 |
+
if cur:
|
56 |
+
segments.append({"text":"".join(cur),"bold":bold,"italic":italic})
|
57 |
+
cur=[]
|
58 |
+
if tok=="*": bold=not bold
|
59 |
+
else: italic=not italic
|
60 |
+
else:
|
61 |
+
cur.append(tok)
|
62 |
+
if cur:
|
63 |
+
segments.append({"text":"".join(cur),"bold":bold,"italic":italic})
|
64 |
+
return segments
|
65 |
+
|
66 |
+
def apply_formatting(tf, content, ds):
|
67 |
+
tf.clear()
|
68 |
+
for line in [l for l in content.split("\n") if l.strip()]:
|
69 |
+
clean = re.sub(r'^#+\s*','', line).strip()
|
70 |
+
if re.match(r'^(What is|Understanding|Key|Conclusion)', clean, re.IGNORECASE):
|
71 |
+
p = tf.add_paragraph(); p.text=clean
|
72 |
+
p.font.size=Pt(ds["heading_size"]); p.font.bold=True; p.space_after=Pt(12)
|
73 |
+
continue
|
74 |
+
if re.match(r'^[\-\*\β’] ', clean):
|
75 |
+
p=tf.add_paragraph(); p.level=0
|
76 |
+
txt = clean[2:].strip()
|
77 |
+
for seg in split_formatted_runs(txt):
|
78 |
+
r=p.add_run(); r.text=seg["text"]; r.font.bold=seg["bold"]; r.font.italic=seg["italic"]
|
79 |
+
p.font.size=Pt(ds["body_size"]); continue
|
80 |
+
p=tf.add_paragraph()
|
81 |
+
for seg in split_formatted_runs(clean):
|
82 |
+
r=p.add_run(); r.text=seg["text"]; r.font.bold=seg["bold"]; r.font.italic=seg["italic"]
|
83 |
+
p.font.size=Pt(ds["body_size"]); p.space_after=Pt(6)
|
84 |
+
|
85 |
+
def apply_design(slide, ds):
|
86 |
+
cols = ds["colors"]
|
87 |
+
for shp in slide.shapes:
|
88 |
+
if hasattr(shp, "text_frame"):
|
89 |
+
for p in shp.text_frame.paragraphs:
|
90 |
+
for r in p.runs:
|
91 |
+
r.font.name = ds["font"]
|
92 |
+
r.font.color.rgb = RGBColor.from_string(cols["text"][1:])
|
93 |
+
if shp == getattr(slide.shapes, "title", None):
|
94 |
+
r.font.color.rgb = RGBColor.from_string(cols["primary"][1:])
|
95 |
+
r.font.size = Pt(ds["title_size"]); r.font.bold=True
|
96 |
+
if ds["set_background"]:
|
97 |
+
slide.background.fill.solid();
|
98 |
+
slide.background.fill.fore_color.rgb = RGBColor.from_string(cols["background"][1:])
|
99 |
+
|
100 |
+
def enhance_slide(slide, prompt, ds):
|
101 |
+
orig = extract_slide_text(slide)
|
102 |
+
if not orig: return
|
103 |
+
frags = chunk_slide_text(orig, max_words=200)
|
104 |
+
enhanced_frags = []
|
105 |
+
for f in frags:
|
106 |
+
enhanced_frags.append(get_ai_response(f"\nImprove this:\n{f}\n\nInstructions: {prompt}"))
|
107 |
+
final = "\n".join(enhanced_frags)
|
108 |
+
title_sp = getattr(slide.shapes, "title", None)
|
109 |
+
for shp in list(slide.shapes):
|
110 |
+
if shp is not title_sp:
|
111 |
+
shp._element.getparent().remove(shp._element)
|
112 |
+
left, top = Inches(1), Inches(1.5 if title_sp else 0.5)
|
113 |
+
tb = slide.shapes.add_textbox(left, top, Inches(8), Inches(5))
|
114 |
+
tf = tb.text_frame; tf.word_wrap=True
|
115 |
+
apply_formatting(tf, final, ds)
|
116 |
+
apply_design(slide, ds)
|
117 |
+
|
118 |
+
def process_presentation(uploaded, prompt, ds):
|
119 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".pptx") as tmp:
|
120 |
+
tmp.write(uploaded.read()); tmp_path=tmp.name
|
121 |
+
prs = Presentation(tmp_path)
|
122 |
+
for s in prs.slides:
|
123 |
+
enhance_slide(s, prompt, ds)
|
124 |
+
os.unlink(tmp_path)
|
125 |
+
return prs
|
126 |
+
|
127 |
+
# ββ UI βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
128 |
+
st.title("Professional PPTX Enhancer")
|
129 |
+
|
130 |
+
uploaded = st.file_uploader("Upload PPTX", type=["pptx"])
|
131 |
+
user_prompt = st.text_area("Enhancement Instructions", placeholder="E.g. Improve clarity, add bullets", height=100)
|
132 |
+
|
133 |
+
with st.expander("π¨ Design Settings", True):
|
134 |
+
tpl = st.selectbox("Template", list(TEMPLATES.keys()))
|
135 |
+
if tpl=="Custom":
|
136 |
+
font = st.selectbox("Font", list(FONT_CHOICES))
|
137 |
+
ts = st.slider("Title size",12,60,32)
|
138 |
+
hs = st.slider("Heading size",12,48,24)
|
139 |
+
bs = st.slider("Body size",8,36,18)
|
140 |
+
sb = st.checkbox("Apply Background",True)
|
141 |
+
cols = {k:st.color_picker(k.capitalize(),c) for k,c in TEMPLATES["Office Default"]["colors"].items()}
|
142 |
+
else:
|
143 |
+
t = TEMPLATES[tpl]
|
144 |
+
font, ts, hs, bs, sb = t["font"], *t["sizes"].values(), t["background"]
|
145 |
+
cols = t["colors"]
|
146 |
+
|
147 |
+
ds = {
|
148 |
+
"font": FONT_CHOICES[font],
|
149 |
+
"colors": cols,
|
150 |
+
"title_size": ts,
|
151 |
+
"heading_size": hs,
|
152 |
+
"body_size": bs,
|
153 |
+
"set_background": sb
|
154 |
+
}
|
155 |
+
|
156 |
+
if st.button("Enhance Presentation") and uploaded and user_prompt:
|
157 |
+
out_prs = process_presentation(uploaded, user_prompt, ds)
|
158 |
+
buf = io.BytesIO(); out_prs.save(buf); buf.seek(0)
|
159 |
+
st.success("Done!")
|
160 |
+
st.download_button("Download Enhanced PPTX",
|
161 |
+
data=buf,
|
162 |
+
file_name="enhanced.pptx",
|
163 |
+
mime="application/vnd.openxmlformats-officedocument.presentationml.presentation")
|