Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,20 +1,28 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
import os
|
|
|
|
| 3 |
from groq import Groq
|
| 4 |
|
| 5 |
-
#
|
|
|
|
|
|
|
| 6 |
client = Groq(api_key=os.getenv("GROQ_API_KEY"))
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
"content": "You are an expert in storyboarding. Provide structured and insightful responses."
|
| 13 |
-
}
|
| 14 |
-
]
|
| 15 |
|
|
|
|
|
|
|
|
|
|
| 16 |
def chat_with_bot(user_input, history):
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
history.append({"role": "user", "content": user_input})
|
| 19 |
|
| 20 |
completion = client.chat.completions.create(
|
|
@@ -22,22 +30,30 @@ def chat_with_bot(user_input, history):
|
|
| 22 |
messages=history,
|
| 23 |
temperature=1,
|
| 24 |
max_tokens=1024,
|
| 25 |
-
top_p=1,
|
| 26 |
-
stream=False,
|
| 27 |
)
|
| 28 |
|
| 29 |
-
|
| 30 |
-
history.append({"role": "assistant", "content":
|
| 31 |
|
| 32 |
return history, history
|
| 33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
def generate_storyboard(scenario):
|
| 35 |
if not scenario.strip():
|
| 36 |
-
return "Please
|
| 37 |
|
| 38 |
messages = [
|
| 39 |
-
{
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
]
|
| 42 |
|
| 43 |
completion = client.chat.completions.create(
|
|
@@ -50,35 +66,65 @@ def generate_storyboard(scenario):
|
|
| 50 |
return completion.choices[0].message.content
|
| 51 |
|
| 52 |
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
-
|
| 58 |
-
|
|
|
|
| 59 |
|
| 60 |
with gr.Tabs():
|
|
|
|
|
|
|
| 61 |
with gr.Tab("💬 Chat"):
|
| 62 |
-
chatbot = gr.Chatbot(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
state = gr.State([])
|
| 64 |
|
| 65 |
user_input = gr.Textbox(
|
| 66 |
-
placeholder="Ask about storyboards...",
|
| 67 |
label="Your Message"
|
| 68 |
)
|
| 69 |
-
send = gr.Button("Ask")
|
| 70 |
|
| 71 |
-
|
| 72 |
-
|
|
|
|
|
|
|
| 73 |
inputs=[user_input, state],
|
| 74 |
-
outputs=[chatbot, state]
|
| 75 |
)
|
| 76 |
|
|
|
|
| 77 |
with gr.Tab("📖 Generate Storyboard"):
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
-
generate.click(generate_storyboard, scenario, output)
|
| 83 |
|
| 84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
from groq import Groq
|
| 4 |
|
| 5 |
+
# -----------------------------
|
| 6 |
+
# 1. Initialize Groq client
|
| 7 |
+
# -----------------------------
|
| 8 |
client = Groq(api_key=os.getenv("GROQ_API_KEY"))
|
| 9 |
|
| 10 |
+
SYSTEM_PROMPT = (
|
| 11 |
+
"You are an expert in storyboarding. "
|
| 12 |
+
"Provide clear, structured, and creative responses."
|
| 13 |
+
)
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
# -----------------------------
|
| 16 |
+
# 2. Chat function
|
| 17 |
+
# -----------------------------
|
| 18 |
def chat_with_bot(user_input, history):
|
| 19 |
+
if history is None:
|
| 20 |
+
history = []
|
| 21 |
+
|
| 22 |
+
# Add system message once
|
| 23 |
+
if not history:
|
| 24 |
+
history.append({"role": "system", "content": SYSTEM_PROMPT})
|
| 25 |
+
|
| 26 |
history.append({"role": "user", "content": user_input})
|
| 27 |
|
| 28 |
completion = client.chat.completions.create(
|
|
|
|
| 30 |
messages=history,
|
| 31 |
temperature=1,
|
| 32 |
max_tokens=1024,
|
|
|
|
|
|
|
| 33 |
)
|
| 34 |
|
| 35 |
+
assistant_reply = completion.choices[0].message.content
|
| 36 |
+
history.append({"role": "assistant", "content": assistant_reply})
|
| 37 |
|
| 38 |
return history, history
|
| 39 |
|
| 40 |
+
|
| 41 |
+
# -----------------------------
|
| 42 |
+
# 3. Storyboard generator
|
| 43 |
+
# -----------------------------
|
| 44 |
def generate_storyboard(scenario):
|
| 45 |
if not scenario.strip():
|
| 46 |
+
return "❗ Please enter a scenario."
|
| 47 |
|
| 48 |
messages = [
|
| 49 |
+
{
|
| 50 |
+
"role": "system",
|
| 51 |
+
"content": "Generate a 6-scene storyboard in a table format."
|
| 52 |
+
},
|
| 53 |
+
{
|
| 54 |
+
"role": "user",
|
| 55 |
+
"content": scenario
|
| 56 |
+
}
|
| 57 |
]
|
| 58 |
|
| 59 |
completion = client.chat.completions.create(
|
|
|
|
| 66 |
return completion.choices[0].message.content
|
| 67 |
|
| 68 |
|
| 69 |
+
# -----------------------------
|
| 70 |
+
# 4. Gradio UI
|
| 71 |
+
# -----------------------------
|
| 72 |
+
with gr.Blocks(
|
| 73 |
+
theme=gr.themes.Glass(),
|
| 74 |
+
title="Storyboard Assistant"
|
| 75 |
+
) as demo:
|
| 76 |
|
| 77 |
+
gr.Markdown(
|
| 78 |
+
"<h1 style='text-align:center;'>📖 Storyboard Assistant</h1>"
|
| 79 |
+
)
|
| 80 |
|
| 81 |
with gr.Tabs():
|
| 82 |
+
|
| 83 |
+
# -------- Chat Tab --------
|
| 84 |
with gr.Tab("💬 Chat"):
|
| 85 |
+
chatbot = gr.Chatbot(
|
| 86 |
+
label="Storyboard Chatbot",
|
| 87 |
+
type="messages"
|
| 88 |
+
)
|
| 89 |
+
|
| 90 |
state = gr.State([])
|
| 91 |
|
| 92 |
user_input = gr.Textbox(
|
| 93 |
+
placeholder="Ask about storyboards, scenes, pacing...",
|
| 94 |
label="Your Message"
|
| 95 |
)
|
|
|
|
| 96 |
|
| 97 |
+
send_btn = gr.Button("Ask")
|
| 98 |
+
|
| 99 |
+
send_btn.click(
|
| 100 |
+
fn=chat_with_bot,
|
| 101 |
inputs=[user_input, state],
|
| 102 |
+
outputs=[chatbot, state],
|
| 103 |
)
|
| 104 |
|
| 105 |
+
# -------- Storyboard Tab --------
|
| 106 |
with gr.Tab("📖 Generate Storyboard"):
|
| 107 |
+
scenario_input = gr.Textbox(
|
| 108 |
+
label="Scenario",
|
| 109 |
+
placeholder="A child discovers a hidden robot in the attic..."
|
| 110 |
+
)
|
| 111 |
+
|
| 112 |
+
generate_btn = gr.Button("Generate Storyboard")
|
| 113 |
+
|
| 114 |
+
storyboard_output = gr.Textbox(
|
| 115 |
+
label="Generated Storyboard",
|
| 116 |
+
lines=14
|
| 117 |
+
)
|
| 118 |
+
|
| 119 |
+
generate_btn.click(
|
| 120 |
+
fn=generate_storyboard,
|
| 121 |
+
inputs=scenario_input,
|
| 122 |
+
outputs=storyboard_output
|
| 123 |
+
)
|
| 124 |
|
|
|
|
| 125 |
|
| 126 |
+
# -----------------------------
|
| 127 |
+
# 5. Launch app
|
| 128 |
+
# -----------------------------
|
| 129 |
+
if __name__ == "__main__":
|
| 130 |
+
demo.launch()
|