Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,63 +1,214 @@
|
|
| 1 |
-
import os
|
| 2 |
import gradio as gr
|
| 3 |
from groq import Groq
|
|
|
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
| 7 |
-
# -------------------------------------------------
|
| 8 |
-
client = Groq(api_key=os.getenv("GROQ_API_KEY"))
|
| 9 |
-
|
| 10 |
-
# -------------------------------------------------
|
| 11 |
-
# Chat streaming generator (Groq correct usage)
|
| 12 |
-
# -------------------------------------------------
|
| 13 |
-
def chat_stream(message, history):
|
| 14 |
-
messages = [
|
| 15 |
-
{
|
| 16 |
-
"role": "system",
|
| 17 |
-
"content": "act like a storyboard generator"
|
| 18 |
-
}
|
| 19 |
-
]
|
| 20 |
-
|
| 21 |
-
# rebuild conversation from Gradio history
|
| 22 |
-
for user, assistant in history:
|
| 23 |
-
messages.append({"role": "user", "content": user})
|
| 24 |
-
messages.append({"role": "assistant", "content": assistant})
|
| 25 |
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
-
response = ""
|
| 37 |
-
for chunk in completion:
|
| 38 |
-
delta = chunk.choices[0].delta.content
|
| 39 |
-
if delta:
|
| 40 |
-
response += delta
|
| 41 |
-
yield history + [(message, response)]
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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groq
|
| 3 |
+
import os
|
| 4 |
|
| 5 |
+
# Initialize Groq client
|
| 6 |
+
client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
# System prompt for storyboard generation
|
| 9 |
+
SYSTEM_PROMPT = """You are an expert storyboard generator assistant. You help users create detailed, professional storyboards for:
|
| 10 |
+
- Films and Television productions
|
| 11 |
+
- Animation projects
|
| 12 |
+
- Advertisements and commercials
|
| 13 |
+
- Video games
|
| 14 |
+
- Comics and Graphic Novels
|
| 15 |
+
- Music videos
|
| 16 |
+
- Educational content
|
| 17 |
|
| 18 |
+
When generating storyboards, ALWAYS include:
|
| 19 |
+
1. **Shot Number** - Sequential numbering
|
| 20 |
+
2. **Visual Description** - Detailed scene description
|
| 21 |
+
3. **Camera Angle** - (Wide, Medium, Close-up, POV, etc.)
|
| 22 |
+
4. **Camera Movement** - (Pan, Tilt, Zoom, Dolly, Static, etc.)
|
| 23 |
+
5. **Action/Movement** - What's happening in the scene
|
| 24 |
+
6. **Dialogue** - Any spoken lines
|
| 25 |
+
7. **Sound Effects/Music** - Audio elements
|
| 26 |
+
8. **Duration** - Estimated time for each shot
|
| 27 |
+
9. **Mood/Lighting** - Atmosphere description
|
| 28 |
+
10. **Transitions** - How shots connect (Cut, Fade, Dissolve, etc.)
|
| 29 |
+
|
| 30 |
+
Format storyboards as organized markdown tables. Be creative, detailed, and cinematic in your descriptions.
|
| 31 |
+
Ask clarifying questions about genre, tone, target audience, and style when needed."""
|
| 32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
+
def generate_response(message, history):
|
| 35 |
+
"""Generate streaming response from Groq API"""
|
| 36 |
+
|
| 37 |
+
# Build messages list from history
|
| 38 |
+
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
| 39 |
+
|
| 40 |
+
# Add conversation history
|
| 41 |
+
for user_msg, assistant_msg in history:
|
| 42 |
+
if user_msg:
|
| 43 |
+
messages.append({"role": "user", "content": user_msg})
|
| 44 |
+
if assistant_msg:
|
| 45 |
+
messages.append({"role": "assistant", "content": assistant_msg})
|
| 46 |
+
|
| 47 |
+
# Add current message
|
| 48 |
+
messages.append({"role": "user", "content": message})
|
| 49 |
+
|
| 50 |
+
# Create streaming completion
|
| 51 |
+
try:
|
| 52 |
+
completion = client.chat.completions.create(
|
| 53 |
+
model="llama-3.3-70b-versatile",
|
| 54 |
+
messages=messages,
|
| 55 |
+
temperature=0.9,
|
| 56 |
+
max_completion_tokens=2048,
|
| 57 |
+
top_p=1,
|
| 58 |
+
stream=True,
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
# Stream the response
|
| 62 |
+
response = ""
|
| 63 |
+
for chunk in completion:
|
| 64 |
+
content = chunk.choices[0].delta.content or ""
|
| 65 |
+
response += content
|
| 66 |
+
yield response
|
| 67 |
+
|
| 68 |
+
except Exception as e:
|
| 69 |
+
yield f"❌ Error: {str(e)}. Please check your API key and try again."
|
| 70 |
|
|
|
|
| 71 |
|
| 72 |
+
# Example prompts for users
|
| 73 |
+
EXAMPLES = [
|
| 74 |
+
"Create a storyboard for a 30-second coffee commercial",
|
| 75 |
+
"Generate a storyboard for the opening scene of a horror movie in a haunted house",
|
| 76 |
+
"Design a storyboard for a 2-minute animated short about a robot learning to dance",
|
| 77 |
+
"Create a storyboard for a car chase scene in an action movie",
|
| 78 |
+
"Generate a storyboard for a romantic comedy meet-cute scene at a bookstore",
|
| 79 |
+
"Design a storyboard for a video game cinematic trailer for a fantasy RPG",
|
| 80 |
+
"Create a storyboard for a music video with a dreamy, surreal aesthetic",
|
| 81 |
+
"Generate a 6-panel comic storyboard about a superhero's origin story",
|
| 82 |
+
]
|
| 83 |
|
| 84 |
+
# Custom CSS for better appearance
|
| 85 |
+
CUSTOM_CSS = """
|
| 86 |
+
.gradio-container {
|
| 87 |
+
font-family: 'Inter', sans-serif !important;
|
| 88 |
+
}
|
| 89 |
+
.chat-message {
|
| 90 |
+
padding: 1rem;
|
| 91 |
+
border-radius: 8px;
|
| 92 |
+
}
|
| 93 |
+
footer {
|
| 94 |
+
display: none !important;
|
| 95 |
+
}
|
| 96 |
+
.example-btn {
|
| 97 |
+
font-size: 0.85rem !important;
|
| 98 |
+
}
|
| 99 |
+
"""
|
| 100 |
|
| 101 |
+
# Create Gradio interface
|
| 102 |
+
with gr.Blocks(css=CUSTOM_CSS, theme=gr.themes.Soft(
|
| 103 |
+
primary_hue="indigo",
|
| 104 |
+
secondary_hue="purple",
|
| 105 |
+
)) as demo:
|
| 106 |
+
|
| 107 |
+
# Header
|
| 108 |
+
gr.Markdown("""
|
| 109 |
+
# 🎬 Storyboard Generator AI
|
| 110 |
+
|
| 111 |
+
**Create professional storyboards for films, animations, ads, games, and more!**
|
| 112 |
+
|
| 113 |
+
Simply describe your project or scene, and I'll generate a detailed storyboard with:
|
| 114 |
+
- Shot compositions & camera angles
|
| 115 |
+
- Visual descriptions & actions
|
| 116 |
+
- Dialogue & sound effects
|
| 117 |
+
- Timing & transitions
|
| 118 |
+
""")
|
| 119 |
+
|
| 120 |
+
# Main chatbot interface
|
| 121 |
+
chatbot = gr.Chatbot(
|
| 122 |
+
label="Storyboard Assistant",
|
| 123 |
+
height=500,
|
| 124 |
+
show_copy_button=True,
|
| 125 |
+
avatar_images=(None, "https://em-content.zobj.net/source/twitter/376/clapper-board_1f3ac.png"),
|
| 126 |
+
bubble_full_width=False,
|
| 127 |
+
)
|
| 128 |
+
|
| 129 |
+
# Input area
|
| 130 |
+
with gr.Row():
|
| 131 |
+
msg = gr.Textbox(
|
| 132 |
+
label="Your Message",
|
| 133 |
+
placeholder="Describe the scene or project you want to storyboard...",
|
| 134 |
+
lines=2,
|
| 135 |
+
scale=4,
|
| 136 |
+
)
|
| 137 |
+
submit_btn = gr.Button("🎬 Generate", variant="primary", scale=1)
|
| 138 |
+
|
| 139 |
+
# Clear button
|
| 140 |
+
clear_btn = gr.Button("🗑️ Clear Chat", variant="secondary")
|
| 141 |
+
|
| 142 |
+
# Examples section
|
| 143 |
+
gr.Markdown("### 💡 Try these examples:")
|
| 144 |
+
gr.Examples(
|
| 145 |
+
examples=EXAMPLES,
|
| 146 |
+
inputs=msg,
|
| 147 |
+
label="",
|
| 148 |
+
)
|
| 149 |
+
|
| 150 |
+
# Tips section
|
| 151 |
+
with gr.Accordion("📖 Tips for Better Storyboards", open=False):
|
| 152 |
+
gr.Markdown("""
|
| 153 |
+
**For best results, include:**
|
| 154 |
+
- **Genre/Style**: Horror, comedy, documentary, anime, etc.
|
| 155 |
+
- **Duration**: How long is the final video/scene?
|
| 156 |
+
- **Target Audience**: Who is this for?
|
| 157 |
+
- **Mood/Tone**: Dark, uplifting, suspenseful, whimsical?
|
| 158 |
+
- **Key Elements**: Must-have shots or moments
|
| 159 |
+
- **Reference**: Any films/shows with similar style
|
| 160 |
+
|
| 161 |
+
**Example detailed prompt:**
|
| 162 |
+
> "Create a storyboard for a 60-second environmental awareness ad targeting young adults.
|
| 163 |
+
> Style: Documentary with emotional impact. Show the contrast between polluted and clean environments.
|
| 164 |
+
> End with a hopeful call-to-action. Inspired by Apple's visual storytelling."
|
| 165 |
+
""")
|
| 166 |
+
|
| 167 |
+
# Event handlers
|
| 168 |
+
def user_submit(user_message, history):
|
| 169 |
+
return "", history + [[user_message, None]]
|
| 170 |
+
|
| 171 |
+
def bot_response(history):
|
| 172 |
+
user_message = history[-1][0]
|
| 173 |
+
history[-1][1] = ""
|
| 174 |
+
for chunk in generate_response(user_message, history[:-1]):
|
| 175 |
+
history[-1][1] = chunk
|
| 176 |
+
yield history
|
| 177 |
+
|
| 178 |
+
# Submit on button click
|
| 179 |
+
submit_btn.click(
|
| 180 |
+
user_submit,
|
| 181 |
+
[msg, chatbot],
|
| 182 |
+
[msg, chatbot],
|
| 183 |
+
queue=False
|
| 184 |
+
).then(
|
| 185 |
+
bot_response,
|
| 186 |
+
chatbot,
|
| 187 |
+
chatbot
|
| 188 |
+
)
|
| 189 |
+
|
| 190 |
+
# Submit on Enter key
|
| 191 |
+
msg.submit(
|
| 192 |
+
user_submit,
|
| 193 |
+
[msg, chatbot],
|
| 194 |
+
[msg, chatbot],
|
| 195 |
+
queue=False
|
| 196 |
+
).then(
|
| 197 |
+
bot_response,
|
| 198 |
+
chatbot,
|
| 199 |
+
chatbot
|
| 200 |
)
|
| 201 |
+
|
| 202 |
+
# Clear chat
|
| 203 |
+
clear_btn.click(lambda: [], None, chatbot, queue=False)
|
| 204 |
+
|
| 205 |
+
# Footer
|
| 206 |
+
gr.Markdown("""
|
| 207 |
+
---
|
| 208 |
+
*Powered by Llama 3.3 70B via Groq* | *Built with Gradio*
|
| 209 |
+
""")
|
| 210 |
|
| 211 |
+
# Launch the app
|
| 212 |
+
if __name__ == "__main__":
|
| 213 |
+
demo.queue()
|
| 214 |
+
demo.launch()
|