Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,148 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
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, temperature, top_p):
|
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(
|
53 |
+
model="llama3-70b-8192",
|
54 |
+
messages=messages,
|
55 |
+
temperature=temperature,
|
56 |
+
top_p=top_p,
|
57 |
+
max_tokens=1024,
|
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 |
+
example_scenarios = [
|
71 |
+
"A futuristic cityscape under AI governance.",
|
72 |
+
"A detective solving a mystery in a cyberpunk world.",
|
73 |
+
"A young explorer discovering an ancient civilization.",
|
74 |
+
"A spaceship crew encountering an unknown planet.",
|
75 |
+
"A medieval knight navigating political intrigue."
|
76 |
+
]
|
77 |
+
|
78 |
+
temperature_component = gr.Slider(
|
79 |
+
minimum=0,
|
80 |
+
maximum=1,
|
81 |
+
value=1,
|
82 |
+
step=0.01,
|
83 |
+
label="Temperature",
|
84 |
+
info="Controls randomness. Lower values make responses more deterministic."
|
85 |
+
)
|
86 |
+
|
87 |
+
top_p_component = gr.Slider(
|
88 |
+
minimum=0,
|
89 |
+
maximum=1,
|
90 |
+
value=1,
|
91 |
+
step=0.01,
|
92 |
+
label="Top-P Sampling",
|
93 |
+
info="Limits token selection to tokens with a cumulative probability up to P."
|
94 |
+
)
|
95 |
+
|
96 |
+
with gr.Blocks(theme=gr.themes.Glass(primary_hue="violet", secondary_hue="emerald", neutral_hue="stone")) as demo:
|
97 |
+
with gr.Tabs():
|
98 |
+
with gr.TabItem("💬Chat"):
|
99 |
+
gr.HTML(TITLE)
|
100 |
+
chatbot = gr.Chatbot(label="Storyboard Chatbot")
|
101 |
+
with gr.Row():
|
102 |
+
user_input = gr.Textbox(
|
103 |
+
label="Your Message",
|
104 |
+
placeholder="Type your question here...",
|
105 |
+
lines=1
|
106 |
+
)
|
107 |
+
send_button = gr.Button("✋Ask Question")
|
108 |
+
|
109 |
+
# Chatbot functionality
|
110 |
+
send_button.click(
|
111 |
+
fn=chat_with_bot_stream,
|
112 |
+
inputs=user_input,
|
113 |
+
outputs=chatbot,
|
114 |
+
queue=True
|
115 |
+
).then(
|
116 |
+
fn=lambda: "",
|
117 |
+
inputs=None,
|
118 |
+
outputs=user_input
|
119 |
+
)
|
120 |
+
|
121 |
+
with gr.TabItem("📖 Generate Storyboard"):
|
122 |
+
gr.Markdown("## Generate a Storyboard")
|
123 |
+
scenario_input = gr.Textbox(label="Enter your scenario")
|
124 |
+
example_radio = gr.Radio(
|
125 |
+
choices=example_scenarios,
|
126 |
+
label="Example Scenarios",
|
127 |
+
info="Select an example scenario or enter your own."
|
128 |
+
)
|
129 |
+
generate_btn = gr.Button("Generate Storyboard")
|
130 |
+
storyboard_output = gr.Textbox(label="Generated Storyboard", interactive=False)
|
131 |
+
|
132 |
+
with gr.Accordion("🛠️ Customize", open=False):
|
133 |
+
temperature_component.render()
|
134 |
+
top_p_component.render()
|
135 |
+
|
136 |
+
generate_btn.click(
|
137 |
+
generate_storyboard,
|
138 |
+
inputs=[scenario_input, temperature_component, top_p_component],
|
139 |
+
outputs=storyboard_output
|
140 |
+
)
|
141 |
+
|
142 |
+
example_radio.change(
|
143 |
+
fn=lambda scenario: scenario if scenario else "No scenario selected.",
|
144 |
+
inputs=[example_radio],
|
145 |
+
outputs=[scenario_input]
|
146 |
+
)
|
147 |
+
|
148 |
+
demo.launch()
|