Rahatara commited on
Commit
2bf3d20
·
verified ·
1 Parent(s): e664e0a

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +170 -0
app.py ADDED
@@ -0,0 +1,170 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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, temperature, top_p):
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=temperature,
26
+ top_p=top_p,
27
+ max_tokens=1024,
28
+ stream=True,
29
+ stop=None,
30
+ )
31
+
32
+ response_content = ""
33
+ for chunk in completion:
34
+ text_chunk = chunk.choices[0].delta.content or ""
35
+ response_content += text_chunk
36
+ yield [(msg["content"] if msg["role"] == "user" else None,
37
+ msg["content"] if msg["role"] == "assistant" else None)
38
+ for msg in conversation_history] + [(user_input, response_content)]
39
+
40
+ conversation_history.append({"role": "assistant", "content": response_content})
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
+ example_questions = [
79
+ "How do I create a compelling storyboard?",
80
+ "What are the key elements of a good visual story?",
81
+ "How can AI help with storyboarding?",
82
+ "What are common mistakes in storyboarding?",
83
+ "How do I structure a scene effectively?"
84
+ ]
85
+
86
+ temperature_component = gr.Slider(
87
+ minimum=0,
88
+ maximum=1,
89
+ value=1,
90
+ step=0.01,
91
+ label="Temperature",
92
+ info="Controls randomness. Lower values make responses more deterministic."
93
+ )
94
+
95
+ top_p_component = gr.Slider(
96
+ minimum=0,
97
+ maximum=1,
98
+ value=1,
99
+ step=0.01,
100
+ label="Top-P Sampling",
101
+ info="Limits token selection to tokens with a cumulative probability up to P."
102
+ )
103
+
104
+ with gr.Blocks(theme=gr.themes.Glass(primary_hue="violet", secondary_hue="emerald", neutral_hue="stone")) as demo:
105
+ with gr.Tabs():
106
+ with gr.TabItem("💬Chat"):
107
+ gr.HTML(TITLE)
108
+ chatbot = gr.Chatbot(label="Storyboard Chatbot", type="messages")
109
+ with gr.Row():
110
+ user_input = gr.Textbox(
111
+ label="Your Message",
112
+ placeholder="Type your question here...",
113
+ lines=1
114
+ )
115
+ send_button = gr.Button("✋Ask Question")
116
+
117
+ with gr.Accordion("🛠️ Customize Chatbot", open=False):
118
+ gr.Row([temperature_component, top_p_component])
119
+
120
+ with gr.Accordion("🧪 Example Questions", open=False):
121
+ example_radio = gr.Radio(
122
+ choices=example_questions,
123
+ label="Example Queries",
124
+ info="Select an example question or enter your own."
125
+ )
126
+ example_radio.change(
127
+ fn=lambda q: q if q else "No question selected.",
128
+ inputs=[example_radio],
129
+ outputs=[user_input]
130
+ )
131
+
132
+ # Chatbot functionality
133
+ send_button.click(
134
+ fn=chat_with_bot_stream,
135
+ inputs=[user_input, temperature_component, top_p_component],
136
+ outputs=chatbot,
137
+ queue=True
138
+ ).then(
139
+ fn=lambda: "",
140
+ inputs=None,
141
+ outputs=user_input
142
+ )
143
+
144
+ with gr.TabItem("📖 Generate Storyboard"):
145
+ gr.Markdown("## Generate a Storyboard")
146
+ scenario_input = gr.Textbox(label="Enter your scenario")
147
+ example_radio = gr.Radio(
148
+ choices=example_scenarios,
149
+ label="Example Scenarios",
150
+ info="Select an example scenario or enter your own."
151
+ )
152
+ generate_btn = gr.Button("Generate Storyboard")
153
+ storyboard_output = gr.Textbox(label="Generated Storyboard", interactive=False)
154
+
155
+ with gr.Accordion("🛠️ Customize Storyboard", open=False):
156
+ gr.Row([temperature_component, top_p_component])
157
+
158
+ generate_btn.click(
159
+ generate_storyboard,
160
+ inputs=[scenario_input, temperature_component, top_p_component],
161
+ outputs=storyboard_output
162
+ )
163
+
164
+ example_radio.change(
165
+ fn=lambda scenario: scenario if scenario else "No scenario selected.",
166
+ inputs=[example_radio],
167
+ outputs=[scenario_input]
168
+ )
169
+
170
+ demo.launch()