Spaces:
Running
Running
Upload slide_generator.py with huggingface_hub
Browse files- slide_generator.py +9 -9
slide_generator.py
CHANGED
|
@@ -3,13 +3,11 @@ 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."
|
|
@@ -17,34 +15,33 @@ SYSTEM_PROMPT = (
|
|
| 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,
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
| 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
|
| 48 |
{{
|
| 49 |
"title": "Main presentation title",
|
| 50 |
"subtitle": "A compelling subtitle",
|
|
@@ -54,6 +51,7 @@ Return a JSON object with this exact structure:
|
|
| 54 |
"type": "title",
|
| 55 |
"title": "Presentation Title",
|
| 56 |
"subtitle": "Subtitle or tagline",
|
|
|
|
| 57 |
"speaker_notes": "Opening notes for the presenter"
|
| 58 |
}},
|
| 59 |
{{
|
|
@@ -61,6 +59,7 @@ Return a JSON object with this exact structure:
|
|
| 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 |
]
|
|
@@ -69,6 +68,7 @@ Return a JSON object with this exact structure:
|
|
| 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
|
|
|
|
| 3 |
import re
|
| 4 |
from huggingface_hub import InferenceClient
|
| 5 |
|
|
|
|
| 6 |
MODEL_NAME = "Qwen/Qwen2.5-7B-Instruct"
|
| 7 |
|
| 8 |
_api_key = os.environ.get("GROQ_API_KEY") or os.environ.get("HF_TOKEN")
|
| 9 |
client = InferenceClient(api_key=_api_key)
|
| 10 |
|
|
|
|
| 11 |
SYSTEM_PROMPT = (
|
| 12 |
"You are an expert presentation designer. "
|
| 13 |
"Return only valid JSON — no markdown fences, no extra text."
|
|
|
|
| 15 |
|
| 16 |
|
| 17 |
def _extract_json(text: str) -> dict:
|
|
|
|
|
|
|
| 18 |
try:
|
| 19 |
return json.loads(text.strip())
|
| 20 |
except json.JSONDecodeError:
|
| 21 |
pass
|
|
|
|
| 22 |
cleaned = re.sub(r"^```[a-z]*\n?", "", text.strip(), flags=re.MULTILINE)
|
| 23 |
cleaned = re.sub(r"\n?```$", "", cleaned.strip(), flags=re.MULTILINE)
|
| 24 |
try:
|
| 25 |
return json.loads(cleaned.strip())
|
| 26 |
except json.JSONDecodeError:
|
| 27 |
pass
|
|
|
|
| 28 |
match = re.search(r"\{.*\}", text, re.DOTALL)
|
| 29 |
if match:
|
| 30 |
return json.loads(match.group())
|
| 31 |
raise ValueError("Could not extract JSON from model response.")
|
| 32 |
|
| 33 |
|
| 34 |
+
def generate_presentation(topic: str, style: str, num_slides: int,
|
| 35 |
+
audience: str, key_points: str) -> dict:
|
| 36 |
+
key_points_section = (
|
| 37 |
+
f"\nKey points to include: {key_points}" if key_points.strip() else ""
|
| 38 |
+
)
|
| 39 |
|
| 40 |
user_prompt = f"""Create a {style.lower()} presentation about: "{topic}"
|
| 41 |
Target audience: {audience}
|
| 42 |
Number of slides: {num_slides} (including title slide){key_points_section}
|
| 43 |
|
| 44 |
+
Return a JSON object with this EXACT structure:
|
| 45 |
{{
|
| 46 |
"title": "Main presentation title",
|
| 47 |
"subtitle": "A compelling subtitle",
|
|
|
|
| 51 |
"type": "title",
|
| 52 |
"title": "Presentation Title",
|
| 53 |
"subtitle": "Subtitle or tagline",
|
| 54 |
+
"image_keyword": "relevant photo keyword for background",
|
| 55 |
"speaker_notes": "Opening notes for the presenter"
|
| 56 |
}},
|
| 57 |
{{
|
|
|
|
| 59 |
"type": "content",
|
| 60 |
"title": "Slide Title",
|
| 61 |
"bullets": ["Point one", "Point two", "Point three"],
|
| 62 |
+
"image_keyword": "2-3 word photo search term relevant to this slide",
|
| 63 |
"speaker_notes": "What to say during this slide"
|
| 64 |
}}
|
| 65 |
]
|
|
|
|
| 68 |
Rules:
|
| 69 |
- First slide must be type "title" with subtitle field
|
| 70 |
- All other slides type "content" with bullets array (3-5 items, max 12 words each)
|
| 71 |
+
- image_keyword: 2-3 words describing a real-world photo relevant to the slide (e.g. "solar panels field", "team meeting office", "brain neuroscience")
|
| 72 |
- Speaker notes: 2-3 sentences
|
| 73 |
- Total slides: exactly {num_slides}
|
| 74 |
- Return only the JSON object, nothing else
|