Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,84 +3,72 @@ 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 |
-
#
|
| 12 |
# -------------------------------
|
| 13 |
-
|
| 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 |
-
#
|
| 26 |
# -------------------------------
|
| 27 |
-
def chat_with_bot(user_input):
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
completion = client.chat.completions.create(
|
| 35 |
model="llama-3.3-70b-versatile",
|
| 36 |
-
messages=
|
| 37 |
temperature=1,
|
| 38 |
-
max_completion_tokens=
|
| 39 |
top_p=1,
|
| 40 |
-
stream=
|
| 41 |
)
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 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
|
| 68 |
|
| 69 |
messages = [
|
| 70 |
{
|
| 71 |
"role": "system",
|
| 72 |
"content": (
|
| 73 |
-
"
|
| 74 |
-
"
|
| 75 |
-
"
|
| 76 |
-
"
|
| 77 |
-
"
|
| 78 |
-
"
|
| 79 |
)
|
| 80 |
},
|
| 81 |
{
|
| 82 |
"role": "user",
|
| 83 |
-
"content":
|
| 84 |
}
|
| 85 |
]
|
| 86 |
|
|
@@ -88,7 +76,7 @@ def generate_storyboard(scenario):
|
|
| 88 |
model="llama-3.3-70b-versatile",
|
| 89 |
messages=messages,
|
| 90 |
temperature=1,
|
| 91 |
-
max_completion_tokens=
|
| 92 |
top_p=1,
|
| 93 |
stream=False,
|
| 94 |
)
|
|
@@ -98,37 +86,38 @@ def generate_storyboard(scenario):
|
|
| 98 |
# -------------------------------
|
| 99 |
# UI
|
| 100 |
# -------------------------------
|
| 101 |
-
with gr.Blocks(
|
| 102 |
gr.Markdown("# 📖 Storyboard Assistant")
|
| 103 |
|
| 104 |
with gr.Tabs():
|
|
|
|
|
|
|
| 105 |
with gr.TabItem("💬 Chat"):
|
| 106 |
chatbot = gr.Chatbot(height=400)
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
)
|
| 112 |
-
send_btn = gr.Button("Ask")
|
| 113 |
|
| 114 |
send_btn.click(
|
| 115 |
-
|
| 116 |
-
inputs=user_input,
|
| 117 |
-
outputs=chatbot,
|
| 118 |
-
)
|
| 119 |
|
|
|
|
| 120 |
with gr.TabItem("📖 Generate Storyboard"):
|
| 121 |
scenario_input = gr.Textbox(
|
| 122 |
label="Scenario",
|
| 123 |
-
placeholder="
|
| 124 |
)
|
| 125 |
-
generate_btn = gr.Button("Generate
|
| 126 |
-
|
| 127 |
|
| 128 |
generate_btn.click(
|
| 129 |
generate_storyboard,
|
| 130 |
inputs=scenario_input,
|
| 131 |
-
outputs=
|
| 132 |
)
|
| 133 |
|
| 134 |
demo.launch()
|
|
|
|
| 3 |
from groq import Groq
|
| 4 |
|
| 5 |
# -------------------------------
|
| 6 |
+
# Groq Client (HF-safe)
|
| 7 |
# -------------------------------
|
| 8 |
client = Groq(api_key=os.getenv("GROQ_API_KEY"))
|
| 9 |
|
| 10 |
# -------------------------------
|
| 11 |
+
# In-memory chat history
|
| 12 |
# -------------------------------
|
| 13 |
+
chat_history = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
# -------------------------------
|
| 16 |
+
# Chat function (NON-streaming)
|
| 17 |
# -------------------------------
|
| 18 |
+
def chat_with_bot(user_input, history):
|
| 19 |
+
messages = [
|
| 20 |
+
{
|
| 21 |
+
"role": "system",
|
| 22 |
+
"content": (
|
| 23 |
+
"You are an expert in storyboarding. "
|
| 24 |
+
"Provide clear, structured, and visual guidance."
|
| 25 |
+
)
|
| 26 |
+
}
|
| 27 |
+
]
|
| 28 |
|
| 29 |
+
for user, assistant in history:
|
| 30 |
+
if user:
|
| 31 |
+
messages.append({"role": "user", "content": user})
|
| 32 |
+
if assistant:
|
| 33 |
+
messages.append({"role": "assistant", "content": assistant})
|
| 34 |
+
|
| 35 |
+
messages.append({"role": "user", "content": user_input})
|
| 36 |
|
| 37 |
completion = client.chat.completions.create(
|
| 38 |
model="llama-3.3-70b-versatile",
|
| 39 |
+
messages=messages,
|
| 40 |
temperature=1,
|
| 41 |
+
max_completion_tokens=512,
|
| 42 |
top_p=1,
|
| 43 |
+
stream=False,
|
| 44 |
)
|
| 45 |
|
| 46 |
+
reply = completion.choices[0].message.content
|
| 47 |
+
history.append((user_input, reply))
|
| 48 |
+
return history, ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
# -------------------------------
|
| 51 |
# Storyboard Generator
|
| 52 |
# -------------------------------
|
| 53 |
def generate_storyboard(scenario):
|
| 54 |
if not scenario.strip():
|
| 55 |
+
return "Please enter a scenario."
|
| 56 |
|
| 57 |
messages = [
|
| 58 |
{
|
| 59 |
"role": "system",
|
| 60 |
"content": (
|
| 61 |
+
"Generate a storyboard as a markdown table with EXACTLY six scenes.\n\n"
|
| 62 |
+
"Columns:\n"
|
| 63 |
+
"- Scene\n"
|
| 64 |
+
"- Scenario (problem + product/feature)\n"
|
| 65 |
+
"- Storyline (visual description + purpose)\n\n"
|
| 66 |
+
"Return ONLY the table."
|
| 67 |
)
|
| 68 |
},
|
| 69 |
{
|
| 70 |
"role": "user",
|
| 71 |
+
"content": scenario
|
| 72 |
}
|
| 73 |
]
|
| 74 |
|
|
|
|
| 76 |
model="llama-3.3-70b-versatile",
|
| 77 |
messages=messages,
|
| 78 |
temperature=1,
|
| 79 |
+
max_completion_tokens=700,
|
| 80 |
top_p=1,
|
| 81 |
stream=False,
|
| 82 |
)
|
|
|
|
| 86 |
# -------------------------------
|
| 87 |
# UI
|
| 88 |
# -------------------------------
|
| 89 |
+
with gr.Blocks() as demo:
|
| 90 |
gr.Markdown("# 📖 Storyboard Assistant")
|
| 91 |
|
| 92 |
with gr.Tabs():
|
| 93 |
+
|
| 94 |
+
# ---- Chat Tab ----
|
| 95 |
with gr.TabItem("💬 Chat"):
|
| 96 |
chatbot = gr.Chatbot(height=400)
|
| 97 |
+
user_input = gr.Textbox(
|
| 98 |
+
placeholder="Ask a storyboarding question..."
|
| 99 |
+
)
|
| 100 |
+
send_btn = gr.Button("Send")
|
|
|
|
|
|
|
| 101 |
|
| 102 |
send_btn.click(
|
| 103 |
+
chat_with_bot,
|
| 104 |
+
inputs=[user_input, chatbot],
|
| 105 |
+
outputs=[chatbot, user_input]
|
| 106 |
+
)
|
| 107 |
|
| 108 |
+
# ---- Storyboard Tab ----
|
| 109 |
with gr.TabItem("📖 Generate Storyboard"):
|
| 110 |
scenario_input = gr.Textbox(
|
| 111 |
label="Scenario",
|
| 112 |
+
placeholder="A student using an AI planner to manage exams"
|
| 113 |
)
|
| 114 |
+
generate_btn = gr.Button("Generate")
|
| 115 |
+
output = gr.Markdown()
|
| 116 |
|
| 117 |
generate_btn.click(
|
| 118 |
generate_storyboard,
|
| 119 |
inputs=scenario_input,
|
| 120 |
+
outputs=output
|
| 121 |
)
|
| 122 |
|
| 123 |
demo.launch()
|