Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
# Function to generate a storyboard using LLM
|
10 |
+
def generate_storyboard(scenario):
|
11 |
+
if not scenario.strip():
|
12 |
+
return "Please provide a scenario to generate the storyboard."
|
13 |
+
|
14 |
+
messages = [
|
15 |
+
{"role": "system", "content": "You are an AI storyteller. Generate a storyboard in a structured table with six scenes."},
|
16 |
+
{"role": "user", "content": f"Generate a 6-scene storyboard for: {scenario}"}
|
17 |
+
]
|
18 |
+
|
19 |
+
completion = client.chat.completions.create(
|
20 |
+
model="llama3-70b-8192",
|
21 |
+
messages=messages,
|
22 |
+
temperature=1,
|
23 |
+
max_tokens=1024,
|
24 |
+
top_p=1,
|
25 |
+
stream=False,
|
26 |
+
stop=None,
|
27 |
+
)
|
28 |
+
return completion.choices[0].message.content
|
29 |
+
|
30 |
+
# Interactive chatbot with streaming response
|
31 |
+
def chatbot_stream(user_input):
|
32 |
+
messages = [
|
33 |
+
{"role": "system", "content": "You are an expert in storyboarding. Answer questions interactively."},
|
34 |
+
{"role": "user", "content": user_input}
|
35 |
+
]
|
36 |
+
|
37 |
+
completion = client.chat.completions.create(
|
38 |
+
model="llama3-70b-8192",
|
39 |
+
messages=messages,
|
40 |
+
temperature=1,
|
41 |
+
max_tokens=1024,
|
42 |
+
top_p=1,
|
43 |
+
stream=True,
|
44 |
+
stop=None,
|
45 |
+
)
|
46 |
+
|
47 |
+
response = ""
|
48 |
+
for chunk in completion:
|
49 |
+
text_chunk = chunk.choices[0].delta.content or ""
|
50 |
+
response += text_chunk
|
51 |
+
yield {"role": "assistant", "content": response}
|
52 |
+
|
53 |
+
# Gradio UI with enhanced chat interface
|
54 |
+
def ui():
|
55 |
+
with gr.Blocks(theme=gr.themes.Glass(primary_hue="violet", secondary_hue="emerald", neutral_hue="stone")) as app:
|
56 |
+
with gr.Tabs():
|
57 |
+
with gr.TabItem("💬Chat"):
|
58 |
+
gr.Markdown("# AI Storyboard & Chatbot")
|
59 |
+
chatbot = gr.Chatbot(label="Storyboard Chatbot", type="messages")
|
60 |
+
with gr.Row():
|
61 |
+
chat_input = gr.Textbox(
|
62 |
+
label="Your Message",
|
63 |
+
placeholder="Type your question here...",
|
64 |
+
lines=1
|
65 |
+
)
|
66 |
+
send_button = gr.Button("✋Ask Question")
|
67 |
+
|
68 |
+
# Quick question examples
|
69 |
+
quick_questions = ["How do I structure a storyboard?", "What makes a good visual scene?", "How to add depth to a story?"]
|
70 |
+
with gr.Row():
|
71 |
+
for question in quick_questions:
|
72 |
+
gr.Button(question, variant="secondary").click(lambda q=question: chatbot_stream(q), inputs=None, outputs=chatbot)
|
73 |
+
|
74 |
+
# Chatbot functionality
|
75 |
+
send_button.click(
|
76 |
+
fn=chatbot_stream,
|
77 |
+
inputs=chat_input,
|
78 |
+
outputs=chatbot,
|
79 |
+
queue=True
|
80 |
+
).then(
|
81 |
+
fn=lambda: "", # Clear the input box after sending
|
82 |
+
inputs=None,
|
83 |
+
outputs=chat_input
|
84 |
+
)
|
85 |
+
|
86 |
+
with gr.TabItem("📖 Storyboard Generator"):
|
87 |
+
gr.Markdown("## Generate a Storyboard")
|
88 |
+
scenario_input = gr.Textbox(label="Enter your scenario")
|
89 |
+
generate_btn = gr.Button("Generate Storyboard")
|
90 |
+
storyboard_output = gr.Textbox(label="Generated Storyboard", interactive=False)
|
91 |
+
generate_btn.click(generate_storyboard, inputs=scenario_input, outputs=storyboard_output)
|
92 |
+
|
93 |
+
app.launch()
|
94 |
+
|
95 |
+
ui()
|