Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
from groq import Groq
|
| 4 |
+
|
| 5 |
+
# Initialize Groq client
|
| 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 |
+
# Function to interact with chatbot
|
| 13 |
+
def chat_with_bot_stream(user_input, temperature, max_tokens, top_p):
|
| 14 |
+
global conversation_history
|
| 15 |
+
conversation_history.append({"role": "user", "content": user_input})
|
| 16 |
+
|
| 17 |
+
if len(conversation_history) == 1:
|
| 18 |
+
conversation_history.insert(0, {
|
| 19 |
+
"role": "system",
|
| 20 |
+
"content": "You are an expert in storyboarding. Provide structured and insightful responses to queries about creating and refining storyboards."
|
| 21 |
+
})
|
| 22 |
+
|
| 23 |
+
completion = client.chat.completions.create(
|
| 24 |
+
model="llama3-70b-8192",
|
| 25 |
+
messages=conversation_history,
|
| 26 |
+
temperature=temperature,
|
| 27 |
+
max_tokens=max_tokens,
|
| 28 |
+
top_p=top_p,
|
| 29 |
+
stream=True,
|
| 30 |
+
stop=None,
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
response_content = ""
|
| 34 |
+
for chunk in completion:
|
| 35 |
+
response_content += chunk.choices[0].delta.content or ""
|
| 36 |
+
|
| 37 |
+
conversation_history.append({"role": "assistant", "content": response_content})
|
| 38 |
+
|
| 39 |
+
return [(msg["content"] if msg["role"] == "user" else None,
|
| 40 |
+
msg["content"] if msg["role"] == "assistant" else None)
|
| 41 |
+
for msg in conversation_history]
|
| 42 |
+
|
| 43 |
+
# Function to generate a storyboard
|
| 44 |
+
def generate_storyboard(scenario):
|
| 45 |
+
if not scenario.strip():
|
| 46 |
+
return "Please provide a scenario to generate the storyboard."
|
| 47 |
+
|
| 48 |
+
messages = [
|
| 49 |
+
{"role": "system", "content": """You are an AI storyteller. Generate a storyboard in a structured table with six scenes. For each scene you provide:
|
| 50 |
+
1) A Scenario text describing what problem a persona is trying to resolve and by using what product or feature.
|
| 51 |
+
2) Storyline text for each scene, descriptive visual information, and the purpose of the scene.
|
| 52 |
+
You must provide the output in a structured format like a table.
|
| 53 |
+
"""},
|
| 54 |
+
{"role": "user", "content": f"Generate a 6-scene storyboard for: {scenario}"}
|
| 55 |
+
]
|
| 56 |
+
|
| 57 |
+
completion = client.chat.completions.create(
|
| 58 |
+
model="llama3-70b-8192",
|
| 59 |
+
messages=messages,
|
| 60 |
+
temperature=1,
|
| 61 |
+
max_tokens=1024,
|
| 62 |
+
top_p=1,
|
| 63 |
+
stream=False,
|
| 64 |
+
stop=None,
|
| 65 |
+
)
|
| 66 |
+
return completion.choices[0].message.content
|
| 67 |
+
|
| 68 |
+
TITLE = """
|
| 69 |
+
<style>
|
| 70 |
+
h1 { text-align: center; font-size: 24px; margin-bottom: 10px; }
|
| 71 |
+
</style>
|
| 72 |
+
<h1>📖 Storyboard Assistant</h1>
|
| 73 |
+
"""
|
| 74 |
+
|
| 75 |
+
with gr.Blocks(theme=gr.themes.Glass(primary_hue="violet", secondary_hue="violet", neutral_hue="stone")) as demo:
|
| 76 |
+
with gr.Tabs():
|
| 77 |
+
with gr.TabItem("💬Chat"):
|
| 78 |
+
gr.HTML(TITLE)
|
| 79 |
+
chatbot = gr.Chatbot(label="Storyboard Chatbot")
|
| 80 |
+
|
| 81 |
+
with gr.Row():
|
| 82 |
+
user_input = gr.Textbox(
|
| 83 |
+
label="Your Message",
|
| 84 |
+
placeholder="Type your question here...",
|
| 85 |
+
lines=1
|
| 86 |
+
)
|
| 87 |
+
send_button = gr.Button("✋Ask Question")
|
| 88 |
+
|
| 89 |
+
# Example questions
|
| 90 |
+
gr.Markdown("### Example Questions:")
|
| 91 |
+
with gr.Row():
|
| 92 |
+
example_q1 = gr.Button("How do I structure a storyboard?")
|
| 93 |
+
example_q2 = gr.Button("What are the key elements of a storyboard?")
|
| 94 |
+
example_q3 = gr.Button("Can you generate an example storyboard?")
|
| 95 |
+
example_q4 = gr.Button("How does storyboarding help in UX design?")
|
| 96 |
+
|
| 97 |
+
# Parameters for model control
|
| 98 |
+
gr.Markdown("### Model Parameters:")
|
| 99 |
+
with gr.Row():
|
| 100 |
+
temperature = gr.Slider(0.0, 2.0, value=1.0, label="Temperature", step=0.1)
|
| 101 |
+
max_tokens = gr.Slider(256, 2048, value=1024, label="Max Tokens", step=256)
|
| 102 |
+
top_p = gr.Slider(0.0, 1.0, value=1.0, label="Top P", step=0.1)
|
| 103 |
+
|
| 104 |
+
# Chatbot functionality
|
| 105 |
+
send_button.click(
|
| 106 |
+
fn=chat_with_bot_stream,
|
| 107 |
+
inputs=[user_input, temperature, max_tokens, top_p],
|
| 108 |
+
outputs=chatbot,
|
| 109 |
+
queue=True
|
| 110 |
+
).then(
|
| 111 |
+
fn=lambda: "",
|
| 112 |
+
inputs=None,
|
| 113 |
+
outputs=user_input
|
| 114 |
+
)
|
| 115 |
+
|
| 116 |
+
# Example question functionality
|
| 117 |
+
example_q1.click(lambda: chat_with_bot_stream("How do I structure a storyboard?", temperature.value, max_tokens.value, top_p.value),
|
| 118 |
+
inputs=[],
|
| 119 |
+
outputs=chatbot)
|
| 120 |
+
example_q2.click(lambda: chat_with_bot_stream("What are the key elements of a storyboard?", temperature.value, max_tokens.value, top_p.value),
|
| 121 |
+
inputs=[],
|
| 122 |
+
outputs=chatbot)
|
| 123 |
+
example_q3.click(lambda: chat_with_bot_stream("Can you generate an example storyboard?", temperature.value, max_tokens.value, top_p.value),
|
| 124 |
+
inputs=[],
|
| 125 |
+
outputs=chatbot)
|
| 126 |
+
example_q4.click(lambda: chat_with_bot_stream("How does storyboarding help in UX design?", temperature.value, max_tokens.value, top_p.value),
|
| 127 |
+
inputs=[],
|
| 128 |
+
outputs=chatbot)
|
| 129 |
+
|
| 130 |
+
with gr.TabItem("📖 Generate Storyboard"):
|
| 131 |
+
gr.Markdown("## Generate a Storyboard")
|
| 132 |
+
scenario_input = gr.Textbox(label="Enter your scenario")
|
| 133 |
+
generate_btn = gr.Button("Generate Storyboard")
|
| 134 |
+
storyboard_output = gr.Textbox(label="Generated Storyboard", interactive=False)
|
| 135 |
+
generate_btn.click(generate_storyboard, inputs=scenario_input, outputs=storyboard_output)
|
| 136 |
+
|
| 137 |
+
demo.launch()
|