Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,100 +3,82 @@ import os
|
|
| 3 |
from groq import Groq
|
| 4 |
|
| 5 |
# Initialize Groq client
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
})
|
| 21 |
-
|
| 22 |
completion = client.chat.completions.create(
|
| 23 |
model="llama-3.3-70b-versatile",
|
| 24 |
-
messages=
|
| 25 |
temperature=1,
|
| 26 |
max_tokens=1024,
|
| 27 |
top_p=1,
|
| 28 |
-
stream=
|
| 29 |
-
stop=None,
|
| 30 |
)
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
return [(msg["content"] if msg["role"] == "user" else None,
|
| 39 |
-
msg["content"] if msg["role"] == "assistant" else None)
|
| 40 |
-
for msg in conversation_history]
|
| 41 |
-
|
| 42 |
-
# Function to generate a storyboard
|
| 43 |
def generate_storyboard(scenario):
|
| 44 |
if not scenario.strip():
|
| 45 |
-
return "Please provide a scenario
|
| 46 |
-
|
| 47 |
messages = [
|
| 48 |
-
{"role": "system", "content": "
|
| 49 |
-
{"role": "user", "content":
|
| 50 |
]
|
| 51 |
-
|
| 52 |
completion = client.chat.completions.create(
|
| 53 |
model="llama-3.3-70b-versatile",
|
| 54 |
messages=messages,
|
| 55 |
temperature=1,
|
| 56 |
max_tokens=1024,
|
| 57 |
-
top_p=1,
|
| 58 |
-
stream=False,
|
| 59 |
-
stop=None,
|
| 60 |
)
|
|
|
|
| 61 |
return completion.choices[0].message.content
|
| 62 |
|
|
|
|
| 63 |
TITLE = """
|
| 64 |
-
<style>
|
| 65 |
-
h1 { text-align: center; font-size: 24px; margin-bottom: 10px; }
|
| 66 |
-
</style>
|
| 67 |
-
<h1>📖 Storyboard Assistant</h1>
|
| 68 |
"""
|
| 69 |
|
| 70 |
-
with gr.Blocks(theme=gr.themes.Glass(
|
|
|
|
|
|
|
| 71 |
with gr.Tabs():
|
| 72 |
-
with gr.
|
| 73 |
-
gr.
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
lines=1
|
| 80 |
-
)
|
| 81 |
-
send_button = gr.Button("✋Ask Question")
|
| 82 |
-
|
| 83 |
-
# Chatbot functionality
|
| 84 |
-
send_button.click(
|
| 85 |
-
fn=chat_with_bot_stream,
|
| 86 |
-
inputs=user_input,
|
| 87 |
-
outputs=chatbot,
|
| 88 |
-
queue=True
|
| 89 |
-
).then(
|
| 90 |
-
fn=lambda: "",
|
| 91 |
-
inputs=None,
|
| 92 |
-
outputs=user_input
|
| 93 |
)
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
|
| 102 |
demo.launch()
|
|
|
|
| 3 |
from groq import Groq
|
| 4 |
|
| 5 |
# Initialize Groq client
|
| 6 |
+
client = Groq(api_key=os.getenv("GROQ_API_KEY"))
|
| 7 |
+
|
| 8 |
+
# Conversation memory
|
| 9 |
+
conversation_history = [
|
| 10 |
+
{
|
| 11 |
+
"role": "system",
|
| 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 |
+
history = history or conversation_history
|
| 18 |
+
history.append({"role": "user", "content": user_input})
|
| 19 |
+
|
|
|
|
|
|
|
| 20 |
completion = client.chat.completions.create(
|
| 21 |
model="llama-3.3-70b-versatile",
|
| 22 |
+
messages=history,
|
| 23 |
temperature=1,
|
| 24 |
max_tokens=1024,
|
| 25 |
top_p=1,
|
| 26 |
+
stream=False,
|
|
|
|
| 27 |
)
|
| 28 |
+
|
| 29 |
+
assistant_msg = completion.choices[0].message.content
|
| 30 |
+
history.append({"role": "assistant", "content": assistant_msg})
|
| 31 |
+
|
| 32 |
+
return history, history
|
| 33 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
def generate_storyboard(scenario):
|
| 35 |
if not scenario.strip():
|
| 36 |
+
return "Please provide a scenario."
|
| 37 |
+
|
| 38 |
messages = [
|
| 39 |
+
{"role": "system", "content": "Generate a 6-scene storyboard in table form."},
|
| 40 |
+
{"role": "user", "content": scenario}
|
| 41 |
]
|
| 42 |
+
|
| 43 |
completion = client.chat.completions.create(
|
| 44 |
model="llama-3.3-70b-versatile",
|
| 45 |
messages=messages,
|
| 46 |
temperature=1,
|
| 47 |
max_tokens=1024,
|
|
|
|
|
|
|
|
|
|
| 48 |
)
|
| 49 |
+
|
| 50 |
return completion.choices[0].message.content
|
| 51 |
|
| 52 |
+
|
| 53 |
TITLE = """
|
| 54 |
+
<h1 style="text-align:center;">📖 Storyboard Assistant</h1>
|
|
|
|
|
|
|
|
|
|
| 55 |
"""
|
| 56 |
|
| 57 |
+
with gr.Blocks(theme=gr.themes.Glass()) as demo:
|
| 58 |
+
gr.HTML(TITLE)
|
| 59 |
+
|
| 60 |
with gr.Tabs():
|
| 61 |
+
with gr.Tab("💬 Chat"):
|
| 62 |
+
chatbot = gr.Chatbot(type="messages", label="Storyboard 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 |
+
send.click(
|
| 72 |
+
chat_with_bot,
|
| 73 |
+
inputs=[user_input, state],
|
| 74 |
+
outputs=[chatbot, state]
|
| 75 |
+
)
|
| 76 |
+
|
| 77 |
+
with gr.Tab("📖 Generate Storyboard"):
|
| 78 |
+
scenario = gr.Textbox(label="Scenario")
|
| 79 |
+
generate = gr.Button("Generate")
|
| 80 |
+
output = gr.Textbox(label="Storyboard", lines=12)
|
| 81 |
+
|
| 82 |
+
generate.click(generate_storyboard, scenario, output)
|
| 83 |
|
| 84 |
demo.launch()
|