Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,31 +6,47 @@ from groq import Groq
|
|
| 6 |
api_key = os.getenv("GROQ_API_KEY")
|
| 7 |
client = Groq(api_key=api_key)
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
|
|
|
| 18 |
|
| 19 |
completion = client.chat.completions.create(
|
| 20 |
model="llama3-70b-8192",
|
| 21 |
-
messages=
|
| 22 |
temperature=1,
|
| 23 |
max_tokens=1024,
|
| 24 |
top_p=1,
|
| 25 |
-
stream=
|
| 26 |
stop=None,
|
| 27 |
)
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
#
|
| 31 |
-
def
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
]
|
| 35 |
|
| 36 |
completion = client.chat.completions.create(
|
|
@@ -39,60 +55,48 @@ def chatbot_stream(user_input, chat_history):
|
|
| 39 |
temperature=1,
|
| 40 |
max_tokens=1024,
|
| 41 |
top_p=1,
|
| 42 |
-
stream=
|
| 43 |
stop=None,
|
| 44 |
)
|
| 45 |
-
|
| 46 |
-
response = ""
|
| 47 |
-
for chunk in completion:
|
| 48 |
-
text_chunk = chunk.choices[0].delta.content or ""
|
| 49 |
-
response += text_chunk
|
| 50 |
-
yield chat_history + [{"role": "assistant", "content": response}]
|
| 51 |
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
chatbot = gr.Chatbot(label="Storyboard Chatbot", type="messages")
|
| 59 |
-
with gr.Row():
|
| 60 |
-
chat_input = gr.Textbox(
|
| 61 |
-
label="Your Message",
|
| 62 |
-
placeholder="Type your question here...",
|
| 63 |
-
lines=1
|
| 64 |
-
)
|
| 65 |
-
send_button = gr.Button("✋Ask Question")
|
| 66 |
-
|
| 67 |
-
# Quick question examples
|
| 68 |
-
quick_questions = ["How do I structure a storyboard?", "What makes a good visual scene?", "How to add depth to a story?"]
|
| 69 |
-
with gr.Row():
|
| 70 |
-
for question in quick_questions:
|
| 71 |
-
gr.Button(question, variant="secondary").click(
|
| 72 |
-
fn=lambda q, h=[]: chatbot_stream(q, h),
|
| 73 |
-
inputs=[gr.State(), chatbot],
|
| 74 |
-
outputs=chatbot
|
| 75 |
-
)
|
| 76 |
-
|
| 77 |
-
# Chatbot functionality
|
| 78 |
-
send_button.click(
|
| 79 |
-
fn=chatbot_stream,
|
| 80 |
-
inputs=[chat_input, chatbot],
|
| 81 |
-
outputs=chatbot,
|
| 82 |
-
queue=True
|
| 83 |
-
).then(
|
| 84 |
-
fn=lambda: "", # Clear the input box after sending
|
| 85 |
-
inputs=None,
|
| 86 |
-
outputs=chat_input
|
| 87 |
-
)
|
| 88 |
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
|
| 98 |
-
|
|
|
|
| 6 |
api_key = os.getenv("GROQ_API_KEY")
|
| 7 |
client = Groq(api_key=api_key)
|
| 8 |
|
| 9 |
+
# Initialize conversation history
|
| 10 |
+
conversation_history = []
|
| 11 |
+
|
| 12 |
+
def chat_with_bot_stream(user_input):
|
| 13 |
+
global conversation_history
|
| 14 |
+
conversation_history.append({"role": "user", "content": user_input})
|
| 15 |
|
| 16 |
+
if len(conversation_history) == 1:
|
| 17 |
+
conversation_history.insert(0, {
|
| 18 |
+
"role": "system",
|
| 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="llama3-70b-8192",
|
| 24 |
+
messages=conversation_history,
|
| 25 |
temperature=1,
|
| 26 |
max_tokens=1024,
|
| 27 |
top_p=1,
|
| 28 |
+
stream=True,
|
| 29 |
stop=None,
|
| 30 |
)
|
| 31 |
+
|
| 32 |
+
response_content = ""
|
| 33 |
+
for chunk in completion:
|
| 34 |
+
response_content += chunk.choices[0].delta.content or ""
|
| 35 |
+
|
| 36 |
+
conversation_history.append({"role": "assistant", "content": response_content})
|
| 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 to generate the storyboard."
|
| 46 |
+
|
| 47 |
+
messages = [
|
| 48 |
+
{"role": "system", "content": "You are an AI storyteller. Generate a storyboard in a structured table with six scenes."},
|
| 49 |
+
{"role": "user", "content": f"Generate a 6-scene storyboard for: {scenario}"}
|
| 50 |
]
|
| 51 |
|
| 52 |
completion = client.chat.completions.create(
|
|
|
|
| 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(primary_hue="violet", secondary_hue="emerald", neutral_hue="stone")) as demo:
|
| 71 |
+
with gr.Tabs():
|
| 72 |
+
with gr.TabItem("💬Chat"):
|
| 73 |
+
gr.HTML(TITLE)
|
| 74 |
+
chatbot = gr.Chatbot(label="Storyboard Chatbot")
|
| 75 |
+
with gr.Row():
|
| 76 |
+
user_input = gr.Textbox(
|
| 77 |
+
label="Your Message",
|
| 78 |
+
placeholder="Type your question here...",
|
| 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 |
+
with gr.TabItem("📖 Generate Storyboard"):
|
| 96 |
+
gr.Markdown("## Generate a Storyboard")
|
| 97 |
+
scenario_input = gr.Textbox(label="Enter your scenario")
|
| 98 |
+
generate_btn = gr.Button("Generate Storyboard")
|
| 99 |
+
storyboard_output = gr.Textbox(label="Generated Storyboard", interactive=False)
|
| 100 |
+
generate_btn.click(generate_storyboard, inputs=scenario_input, outputs=storyboard_output)
|
| 101 |
|
| 102 |
+
demo.launch()
|