adityanamburi07 commited on
Commit
89127b5
·
verified ·
1 Parent(s): 2182907

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +108 -0
app.py ADDED
@@ -0,0 +1,108 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import gradio as gr
3
+ from huggingface_hub import hf_hub_download
4
+ from llama_cpp import Llama
5
+
6
+ # ---------------------------
7
+ # Model settings (HF Spaces compatible)
8
+ # ---------------------------
9
+ REPO_ID = os.getenv("REPO_ID", "bartowski/Falcon3-1B-Instruct-GGUF")
10
+ MODEL_FILE = os.getenv("MODEL_FILE", "Falcon3-1B-Instruct-Q4_K_M.gguf")
11
+
12
+ N_CTX = int(os.getenv("N_CTX", "4096"))
13
+ N_THREADS = int(os.getenv("N_THREADS", str(max(2, (os.cpu_count() or 4) - 1))))
14
+ N_GPU_LAYERS = int(os.getenv("N_GPU_LAYERS", "0")) # CPU Space -> 0
15
+
16
+ _llm = None
17
+
18
+
19
+ def get_llm() -> Llama:
20
+ global _llm
21
+ if _llm is None:
22
+ model_path = hf_hub_download(repo_id=REPO_ID, filename=MODEL_FILE)
23
+ _llm = Llama(
24
+ model_path=model_path,
25
+ n_ctx=N_CTX,
26
+ n_threads=N_THREADS,
27
+ n_gpu_layers=N_GPU_LAYERS,
28
+ verbose=False,
29
+ )
30
+ return _llm
31
+
32
+
33
+ def build_prompt(topic: str, n_slides: int) -> str:
34
+ system = (
35
+ "You are a professional presentation writer. "
36
+ "Create a slide-by-slide PowerPoint script that is clear and easy to present. "
37
+ "Follow the output format exactly."
38
+ )
39
+
40
+ user = f"""
41
+ Topic: {topic}
42
+ Number of slides: {n_slides}
43
+
44
+ Output format (Markdown):
45
+ - Start with: ## Title
46
+ - Then for each slide from 1 to {n_slides}, output exactly:
47
+
48
+ ### Slide X: <short slide title>
49
+ **On-slide bullets (max 5):**
50
+ - ...
51
+ **Speaker notes (45–75 seconds):**
52
+ <spoken script, conversational, with a transition to the next slide>
53
+
54
+ Rules:
55
+ - No extra sections besides the title and slides.
56
+ - Keep bullets concise.
57
+ - Speaker notes should sound natural when spoken aloud.
58
+ """
59
+
60
+ # Falcon3 chat template
61
+ return f"<|system|>\n{system}\n<|user|>\n{user.strip()}\n<|assistant|>\n"
62
+
63
+
64
+ def estimate_max_tokens(n_slides: int) -> int:
65
+ # Rough heuristic for bullets + 45–75s notes per slide
66
+ # Keep it bounded to avoid long CPU runtimes.
67
+ return min(1800, max(600, n_slides * 180))
68
+
69
+
70
+ def generate_ppt_script(topic: str, n_slides: int) -> str:
71
+ topic = (topic or "").strip()
72
+ if not topic:
73
+ return "Please enter a topic."
74
+
75
+ n_slides = int(n_slides)
76
+ if n_slides < 1:
77
+ return "Number of slides must be at least 1."
78
+
79
+ llm = get_llm()
80
+ prompt = build_prompt(topic, n_slides)
81
+
82
+ result = llm(
83
+ prompt,
84
+ max_tokens=estimate_max_tokens(n_slides),
85
+ temperature=0.6,
86
+ top_p=0.9,
87
+ repeat_penalty=1.1,
88
+ stop=["<|system|>", "<|user|>"],
89
+ )
90
+
91
+ return result["choices"][0]["text"].strip()
92
+
93
+
94
+ # ---------------------------
95
+ # Gradio UI
96
+ # ---------------------------
97
+ with gr.Blocks(title="Falcon3 PPT Script Writer (GGUF)") as demo:
98
+ gr.Markdown("# 🧠 Falcon3 PPT Script Writer (GGUF)\nGenerate a slide-by-slide PPT script from a topic.")
99
+
100
+ topic_in = gr.Textbox(label="Topic", placeholder="e.g., Agentic AI for Contact Centers", lines=2)
101
+ slides_in = gr.Slider(minimum=1, maximum=15, value=6, step=1, label="Number of slides")
102
+
103
+ generate_btn = gr.Button("Generate")
104
+ output_md = gr.Markdown(label="Output")
105
+
106
+ generate_btn.click(fn=generate_ppt_script, inputs=[topic_in, slides_in], outputs=output_md)
107
+
108
+ demo.launch(server_name="0.0.0.0", server_port=int(os.getenv("PORT", "7860")))