Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
|
| 4 |
+
from llama_cpp import Llama
|
| 5 |
+
|
| 6 |
+
# -------- Model config --------
|
| 7 |
+
REPO_ID = os.getenv("GGUF_REPO_ID", "tiiuae/Falcon3-1B-Instruct-GGUF")
|
| 8 |
+
FILENAME = os.getenv("GGUF_FILENAME", "Falcon3-1B-Instruct-q4_k_m.gguf") # good CPU balance :contentReference[oaicite:1]{index=1}
|
| 9 |
+
|
| 10 |
+
# Lazy singleton so the model loads only once per Space runtime
|
| 11 |
+
_LLM = None
|
| 12 |
+
|
| 13 |
+
def get_llm():
|
| 14 |
+
global _LLM
|
| 15 |
+
if _LLM is None:
|
| 16 |
+
# llama-cpp-python supports downloading GGUFs from Hugging Face directly via from_pretrained :contentReference[oaicite:2]{index=2}
|
| 17 |
+
_LLM = Llama.from_pretrained(
|
| 18 |
+
repo_id=REPO_ID,
|
| 19 |
+
filename=FILENAME,
|
| 20 |
+
verbose=False,
|
| 21 |
+
# Tweak for CPU Spaces
|
| 22 |
+
n_ctx=4096,
|
| 23 |
+
n_threads=int(os.getenv("OMP_NUM_THREADS", "4")),
|
| 24 |
+
)
|
| 25 |
+
return _LLM
|
| 26 |
+
|
| 27 |
+
def build_prompt(topic: str, audience: str, num_slides: int, tone: str, time_minutes: int):
|
| 28 |
+
# Simple instruct-style format that works broadly with GGUF instruct models.
|
| 29 |
+
return f"""
|
| 30 |
+
You are a senior presentation writer and speaking coach.
|
| 31 |
+
|
| 32 |
+
Task: Write a PowerPoint script for the topic below.
|
| 33 |
+
|
| 34 |
+
Topic: {topic}
|
| 35 |
+
Audience: {audience}
|
| 36 |
+
Tone: {tone}
|
| 37 |
+
Total time: {time_minutes} minutes
|
| 38 |
+
Number of slides: {num_slides}
|
| 39 |
+
|
| 40 |
+
Requirements:
|
| 41 |
+
- Output EXACTLY {num_slides} slides.
|
| 42 |
+
- For each slide include:
|
| 43 |
+
1) Slide Title
|
| 44 |
+
2) 3–6 bullet points (concise, slide-friendly)
|
| 45 |
+
3) Speaker Notes (what to say, 80–140 words)
|
| 46 |
+
- Include a strong opening hook and a clear closing with call-to-action.
|
| 47 |
+
- Avoid fluff. Use concrete examples where possible.
|
| 48 |
+
- Format strictly like:
|
| 49 |
+
|
| 50 |
+
SLIDE 1: <title>
|
| 51 |
+
Bullets:
|
| 52 |
+
- ...
|
| 53 |
+
- ...
|
| 54 |
+
Speaker Notes:
|
| 55 |
+
...
|
| 56 |
+
|
| 57 |
+
SLIDE 2: ...
|
| 58 |
+
""".strip()
|
| 59 |
+
|
| 60 |
+
def generate_ppt_script(topic, audience, num_slides, tone, time_minutes, temperature, max_tokens):
|
| 61 |
+
if not topic or not topic.strip():
|
| 62 |
+
return "Please enter a topic."
|
| 63 |
+
|
| 64 |
+
llm = get_llm()
|
| 65 |
+
prompt = build_prompt(topic.strip(), audience.strip(), int(num_slides), tone, int(time_minutes))
|
| 66 |
+
|
| 67 |
+
# Generate
|
| 68 |
+
out = llm(
|
| 69 |
+
prompt,
|
| 70 |
+
max_tokens=int(max_tokens),
|
| 71 |
+
temperature=float(temperature),
|
| 72 |
+
top_p=0.95,
|
| 73 |
+
stop=["</s>", "SLIDE 999:"], # simple safety stop
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
+
text = out["choices"][0]["text"].strip()
|
| 77 |
+
return text
|
| 78 |
+
|
| 79 |
+
with gr.Blocks(title="Falcon3 PPT Script Writer (GGUF)") as demo:
|
| 80 |
+
gr.Markdown(
|
| 81 |
+
"""
|
| 82 |
+
# Falcon3-1B-Instruct (GGUF) — PPT Script Writer
|
| 83 |
+
Enter a topic and get a **slide-by-slide deck script** with **speaker notes**.
|
| 84 |
+
"""
|
| 85 |
+
)
|
| 86 |
+
|
| 87 |
+
with gr.Row():
|
| 88 |
+
topic = gr.Textbox(label="Topic", placeholder="e.g., Agentic AI in SRE: reducing incident MTTR", lines=2)
|
| 89 |
+
audience = gr.Textbox(label="Audience", placeholder="e.g., SRE + platform engineering leaders", lines=2)
|
| 90 |
+
|
| 91 |
+
with gr.Row():
|
| 92 |
+
num_slides = gr.Slider(5, 20, value=10, step=1, label="Number of slides")
|
| 93 |
+
time_minutes = gr.Slider(5, 60, value=15, step=1, label="Total talk time (minutes)")
|
| 94 |
+
|
| 95 |
+
tone = gr.Dropdown(
|
| 96 |
+
["Professional", "Conversational", "Persuasive", "Technical Deep Dive", "Executive Summary"],
|
| 97 |
+
value="Professional",
|
| 98 |
+
label="Tone",
|
| 99 |
+
)
|
| 100 |
+
|
| 101 |
+
with gr.Accordion("Generation settings", open=False):
|
| 102 |
+
temperature = gr.Slider(0.0, 1.2, value=0.6, step=0.05, label="Temperature")
|
| 103 |
+
max_tokens = gr.Slider(256, 2048, value=1200, step=64, label="Max output tokens")
|
| 104 |
+
|
| 105 |
+
run_btn = gr.Button("Generate PPT Script")
|
| 106 |
+
output = gr.Textbox(label="PPT Script Output", lines=28)
|
| 107 |
+
|
| 108 |
+
run_btn.click(
|
| 109 |
+
fn=generate_ppt_script,
|
| 110 |
+
inputs=[topic, audience, num_slides, tone, time_minutes, temperature, max_tokens],
|
| 111 |
+
outputs=output,
|
| 112 |
+
)
|
| 113 |
+
|
| 114 |
+
demo.queue(default_concurrency_limit=1).launch()
|