Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,106 +1,134 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
import os
|
|
|
|
| 3 |
from groq import Groq
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
| 7 |
-
|
|
|
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
| 13 |
global conversation_history
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
"content": "You are an expert in storyboarding. Provide structured and insightful responses to queries about creating and refining storyboards."
|
| 20 |
-
})
|
| 21 |
-
|
| 22 |
completion = client.chat.completions.create(
|
| 23 |
-
model="
|
| 24 |
messages=conversation_history,
|
| 25 |
temperature=1,
|
| 26 |
-
|
| 27 |
top_p=1,
|
| 28 |
stream=True,
|
| 29 |
-
stop=None,
|
| 30 |
)
|
| 31 |
-
|
| 32 |
-
|
| 33 |
for chunk in completion:
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
def generate_storyboard(scenario):
|
| 44 |
if not scenario.strip():
|
| 45 |
-
return "Please provide a scenario
|
| 46 |
-
|
| 47 |
messages = [
|
| 48 |
-
{
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
]
|
| 55 |
-
|
| 56 |
completion = client.chat.completions.create(
|
| 57 |
-
model="
|
| 58 |
messages=messages,
|
| 59 |
temperature=1,
|
| 60 |
-
|
| 61 |
top_p=1,
|
| 62 |
stream=False,
|
| 63 |
-
stop=None,
|
| 64 |
)
|
|
|
|
| 65 |
return completion.choices[0].message.content
|
| 66 |
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
"""
|
| 73 |
|
| 74 |
-
with gr.Blocks(theme=gr.themes.Glass(primary_hue="violet", secondary_hue="violet", neutral_hue="stone")) as demo:
|
| 75 |
with gr.Tabs():
|
| 76 |
-
with gr.TabItem("💬Chat"):
|
| 77 |
-
gr.
|
| 78 |
-
chatbot = gr.Chatbot(label="Storyboard Chatbot")
|
| 79 |
with gr.Row():
|
| 80 |
user_input = gr.Textbox(
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
lines=1
|
| 84 |
)
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
fn=chat_with_bot_stream,
|
| 90 |
inputs=user_input,
|
| 91 |
outputs=chatbot,
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
fn=lambda: "",
|
| 95 |
-
inputs=None,
|
| 96 |
-
outputs=user_input
|
| 97 |
-
)
|
| 98 |
-
|
| 99 |
with gr.TabItem("📖 Generate Storyboard"):
|
| 100 |
-
gr.
|
| 101 |
-
|
|
|
|
|
|
|
| 102 |
generate_btn = gr.Button("Generate Storyboard")
|
| 103 |
-
storyboard_output = gr.
|
| 104 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
|
| 106 |
demo.launch()
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
from groq import Groq
|
| 4 |
|
| 5 |
+
# -------------------------------
|
| 6 |
+
# Initialize Groq Client
|
| 7 |
+
# -------------------------------
|
| 8 |
+
client = Groq(api_key=os.getenv("GROQ_API_KEY"))
|
| 9 |
|
| 10 |
+
# -------------------------------
|
| 11 |
+
# Conversation Memory
|
| 12 |
+
# -------------------------------
|
| 13 |
+
conversation_history = [
|
| 14 |
+
{
|
| 15 |
+
"role": "system",
|
| 16 |
+
"content": (
|
| 17 |
+
"You are an expert in storyboarding. "
|
| 18 |
+
"Provide structured, visual, and insightful guidance "
|
| 19 |
+
"for creating and refining storyboards."
|
| 20 |
+
)
|
| 21 |
+
}
|
| 22 |
+
]
|
| 23 |
|
| 24 |
+
# -------------------------------
|
| 25 |
+
# Chatbot (Streaming)
|
| 26 |
+
# -------------------------------
|
| 27 |
+
def chat_with_bot(user_input):
|
| 28 |
global conversation_history
|
| 29 |
+
|
| 30 |
+
conversation_history.append(
|
| 31 |
+
{"role": "user", "content": user_input}
|
| 32 |
+
)
|
| 33 |
+
|
|
|
|
|
|
|
|
|
|
| 34 |
completion = client.chat.completions.create(
|
| 35 |
+
model="llama-3.3-70b-versatile",
|
| 36 |
messages=conversation_history,
|
| 37 |
temperature=1,
|
| 38 |
+
max_completion_tokens=1024,
|
| 39 |
top_p=1,
|
| 40 |
stream=True,
|
|
|
|
| 41 |
)
|
| 42 |
+
|
| 43 |
+
response = ""
|
| 44 |
for chunk in completion:
|
| 45 |
+
delta = chunk.choices[0].delta.content
|
| 46 |
+
if delta:
|
| 47 |
+
response += delta
|
| 48 |
+
|
| 49 |
+
conversation_history.append(
|
| 50 |
+
{"role": "assistant", "content": response}
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
return [
|
| 54 |
+
(
|
| 55 |
+
msg["content"] if msg["role"] == "user" else None,
|
| 56 |
+
msg["content"] if msg["role"] == "assistant" else None
|
| 57 |
+
)
|
| 58 |
+
for msg in conversation_history
|
| 59 |
+
if msg["role"] != "system"
|
| 60 |
+
]
|
| 61 |
+
|
| 62 |
+
# -------------------------------
|
| 63 |
+
# Storyboard Generator
|
| 64 |
+
# -------------------------------
|
| 65 |
def generate_storyboard(scenario):
|
| 66 |
if not scenario.strip():
|
| 67 |
+
return "Please provide a scenario."
|
| 68 |
+
|
| 69 |
messages = [
|
| 70 |
+
{
|
| 71 |
+
"role": "system",
|
| 72 |
+
"content": (
|
| 73 |
+
"You are an AI storyteller. Generate a storyboard "
|
| 74 |
+
"as a table with exactly six scenes.\n\n"
|
| 75 |
+
"Each scene must include:\n"
|
| 76 |
+
"1) Scenario: the user problem and product/feature used\n"
|
| 77 |
+
"2) Storyline: visual description and purpose\n\n"
|
| 78 |
+
"Output ONLY a markdown table."
|
| 79 |
+
)
|
| 80 |
+
},
|
| 81 |
+
{
|
| 82 |
+
"role": "user",
|
| 83 |
+
"content": f"Generate a 6-scene storyboard for: {scenario}"
|
| 84 |
+
}
|
| 85 |
]
|
| 86 |
+
|
| 87 |
completion = client.chat.completions.create(
|
| 88 |
+
model="llama-3.3-70b-versatile",
|
| 89 |
messages=messages,
|
| 90 |
temperature=1,
|
| 91 |
+
max_completion_tokens=1024,
|
| 92 |
top_p=1,
|
| 93 |
stream=False,
|
|
|
|
| 94 |
)
|
| 95 |
+
|
| 96 |
return completion.choices[0].message.content
|
| 97 |
|
| 98 |
+
# -------------------------------
|
| 99 |
+
# UI
|
| 100 |
+
# -------------------------------
|
| 101 |
+
with gr.Blocks(theme=gr.themes.Glass()) as demo:
|
| 102 |
+
gr.Markdown("# 📖 Storyboard Assistant")
|
|
|
|
| 103 |
|
|
|
|
| 104 |
with gr.Tabs():
|
| 105 |
+
with gr.TabItem("💬 Chat"):
|
| 106 |
+
chatbot = gr.Chatbot(height=400)
|
|
|
|
| 107 |
with gr.Row():
|
| 108 |
user_input = gr.Textbox(
|
| 109 |
+
placeholder="Ask about storyboarding...",
|
| 110 |
+
scale=4
|
|
|
|
| 111 |
)
|
| 112 |
+
send_btn = gr.Button("Ask")
|
| 113 |
+
|
| 114 |
+
send_btn.click(
|
| 115 |
+
fn=chat_with_bot,
|
|
|
|
| 116 |
inputs=user_input,
|
| 117 |
outputs=chatbot,
|
| 118 |
+
).then(lambda: "", outputs=user_input)
|
| 119 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
with gr.TabItem("📖 Generate Storyboard"):
|
| 121 |
+
scenario_input = gr.Textbox(
|
| 122 |
+
label="Scenario",
|
| 123 |
+
placeholder="e.g., A student using an AI planner app"
|
| 124 |
+
)
|
| 125 |
generate_btn = gr.Button("Generate Storyboard")
|
| 126 |
+
storyboard_output = gr.Markdown()
|
| 127 |
+
|
| 128 |
+
generate_btn.click(
|
| 129 |
+
generate_storyboard,
|
| 130 |
+
inputs=scenario_input,
|
| 131 |
+
outputs=storyboard_output
|
| 132 |
+
)
|
| 133 |
|
| 134 |
demo.launch()
|