Spaces:
Sleeping
Sleeping
Upload slide_generator.py with huggingface_hub
Browse files- slide_generator.py +88 -0
slide_generator.py
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import json
|
| 3 |
+
import re
|
| 4 |
+
from huggingface_hub import InferenceClient
|
| 5 |
+
|
| 6 |
+
# Model: Qwen/Qwen2.5-7B-Instruct — 7B parameters (total used: 7B ≤ 32B limit)
|
| 7 |
+
MODEL_NAME = "Qwen/Qwen2.5-7B-Instruct"
|
| 8 |
+
|
| 9 |
+
_api_key = os.environ.get("GROQ_API_KEY") or os.environ.get("HF_TOKEN")
|
| 10 |
+
client = InferenceClient(api_key=_api_key)
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
SYSTEM_PROMPT = (
|
| 14 |
+
"You are an expert presentation designer. "
|
| 15 |
+
"Return only valid JSON — no markdown fences, no extra text."
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
def _extract_json(text: str) -> dict:
|
| 20 |
+
"""Robustly pull a JSON object out of the model response."""
|
| 21 |
+
# Try direct parse first
|
| 22 |
+
try:
|
| 23 |
+
return json.loads(text.strip())
|
| 24 |
+
except json.JSONDecodeError:
|
| 25 |
+
pass
|
| 26 |
+
# Strip markdown fences if present
|
| 27 |
+
cleaned = re.sub(r"^```[a-z]*\n?", "", text.strip(), flags=re.MULTILINE)
|
| 28 |
+
cleaned = re.sub(r"\n?```$", "", cleaned.strip(), flags=re.MULTILINE)
|
| 29 |
+
try:
|
| 30 |
+
return json.loads(cleaned.strip())
|
| 31 |
+
except json.JSONDecodeError:
|
| 32 |
+
pass
|
| 33 |
+
# Find first { ... } block
|
| 34 |
+
match = re.search(r"\{.*\}", text, re.DOTALL)
|
| 35 |
+
if match:
|
| 36 |
+
return json.loads(match.group())
|
| 37 |
+
raise ValueError("Could not extract JSON from model response.")
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
def generate_presentation(topic: str, style: str, num_slides: int, audience: str, key_points: str) -> dict:
|
| 41 |
+
key_points_section = f"\nKey points to include: {key_points}" if key_points.strip() else ""
|
| 42 |
+
|
| 43 |
+
user_prompt = f"""Create a {style.lower()} presentation about: "{topic}"
|
| 44 |
+
Target audience: {audience}
|
| 45 |
+
Number of slides: {num_slides} (including title slide){key_points_section}
|
| 46 |
+
|
| 47 |
+
Return a JSON object with this exact structure:
|
| 48 |
+
{{
|
| 49 |
+
"title": "Main presentation title",
|
| 50 |
+
"subtitle": "A compelling subtitle",
|
| 51 |
+
"slides": [
|
| 52 |
+
{{
|
| 53 |
+
"slide_number": 1,
|
| 54 |
+
"type": "title",
|
| 55 |
+
"title": "Presentation Title",
|
| 56 |
+
"subtitle": "Subtitle or tagline",
|
| 57 |
+
"speaker_notes": "Opening notes for the presenter"
|
| 58 |
+
}},
|
| 59 |
+
{{
|
| 60 |
+
"slide_number": 2,
|
| 61 |
+
"type": "content",
|
| 62 |
+
"title": "Slide Title",
|
| 63 |
+
"bullets": ["Point one", "Point two", "Point three"],
|
| 64 |
+
"speaker_notes": "What to say during this slide"
|
| 65 |
+
}}
|
| 66 |
+
]
|
| 67 |
+
}}
|
| 68 |
+
|
| 69 |
+
Rules:
|
| 70 |
+
- First slide must be type "title" with subtitle field
|
| 71 |
+
- All other slides type "content" with bullets array (3-5 items, max 12 words each)
|
| 72 |
+
- Speaker notes: 2-3 sentences
|
| 73 |
+
- Total slides: exactly {num_slides}
|
| 74 |
+
- Return only the JSON object, nothing else
|
| 75 |
+
"""
|
| 76 |
+
|
| 77 |
+
response = client.chat.completions.create(
|
| 78 |
+
model=MODEL_NAME,
|
| 79 |
+
messages=[
|
| 80 |
+
{"role": "system", "content": SYSTEM_PROMPT},
|
| 81 |
+
{"role": "user", "content": user_prompt},
|
| 82 |
+
],
|
| 83 |
+
temperature=0.7,
|
| 84 |
+
max_tokens=4096,
|
| 85 |
+
)
|
| 86 |
+
|
| 87 |
+
raw = response.choices[0].message.content
|
| 88 |
+
return _extract_json(raw)
|