Rahatara commited on
Commit
de72b93
·
verified ·
1 Parent(s): d9cd6e9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +98 -70
app.py CHANGED
@@ -1,106 +1,134 @@
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):
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. For each scene you provide
49
- 1) A Scenario text describing what problem a pesona is trying to resolve and by using what product or feature.
50
- 2) Storyline text for each scene, descriptive visual information and the purpose of the scene.
51
- You must provide the output in structured format like table.
52
- """},
53
- {"role": "user", "content": f"Generate a 6-scene storyboard for: {scenario}"}
 
 
 
 
 
 
 
 
 
54
  ]
55
-
56
  completion = client.chat.completions.create(
57
- model="llama3-70b-8192",
58
  messages=messages,
59
  temperature=1,
60
- max_tokens=1024,
61
  top_p=1,
62
  stream=False,
63
- stop=None,
64
  )
 
65
  return completion.choices[0].message.content
66
 
67
- TITLE = """
68
- <style>
69
- h1 { text-align: center; font-size: 24px; margin-bottom: 10px; }
70
- </style>
71
- <h1>📖 Storyboard Assistant</h1>
72
- """
73
 
74
- with gr.Blocks(theme=gr.themes.Glass(primary_hue="violet", secondary_hue="violet", neutral_hue="stone")) as demo:
75
  with gr.Tabs():
76
- with gr.TabItem("💬Chat"):
77
- gr.HTML(TITLE)
78
- chatbot = gr.Chatbot(label="Storyboard Chatbot")
79
  with gr.Row():
80
  user_input = gr.Textbox(
81
- label="Your Message",
82
- placeholder="Type your question here...",
83
- lines=1
84
  )
85
- send_button = gr.Button("Ask Question")
86
-
87
- # Chatbot functionality
88
- send_button.click(
89
- fn=chat_with_bot_stream,
90
  inputs=user_input,
91
  outputs=chatbot,
92
- queue=True
93
- ).then(
94
- fn=lambda: "",
95
- inputs=None,
96
- outputs=user_input
97
- )
98
-
99
  with gr.TabItem("📖 Generate Storyboard"):
100
- gr.Markdown("## Generate a Storyboard")
101
- scenario_input = gr.Textbox(label="Enter your scenario")
 
 
102
  generate_btn = gr.Button("Generate Storyboard")
103
- storyboard_output = gr.Textbox(label="Generated Storyboard", interactive=False)
104
- generate_btn.click(generate_storyboard, inputs=scenario_input, outputs=storyboard_output)
 
 
 
 
 
105
 
106
  demo.launch()
 
 
1
  import os
2
+ import gradio as gr
3
  from groq import Groq
4
 
5
+ # -------------------------------
6
+ # Initialize Groq Client
7
+ # -------------------------------
8
+ client = Groq(api_key=os.getenv("GROQ_API_KEY"))
9
 
10
+ # -------------------------------
11
+ # Conversation Memory
12
+ # -------------------------------
13
+ conversation_history = [
14
+ {
15
+ "role": "system",
16
+ "content": (
17
+ "You are an expert in storyboarding. "
18
+ "Provide structured, visual, and insightful guidance "
19
+ "for creating and refining storyboards."
20
+ )
21
+ }
22
+ ]
23
 
24
+ # -------------------------------
25
+ # Chatbot (Streaming)
26
+ # -------------------------------
27
+ def chat_with_bot(user_input):
28
  global conversation_history
29
+
30
+ conversation_history.append(
31
+ {"role": "user", "content": user_input}
32
+ )
33
+
 
 
 
34
  completion = client.chat.completions.create(
35
+ model="llama-3.3-70b-versatile",
36
  messages=conversation_history,
37
  temperature=1,
38
+ max_completion_tokens=1024,
39
  top_p=1,
40
  stream=True,
 
41
  )
42
+
43
+ response = ""
44
  for chunk in completion:
45
+ delta = chunk.choices[0].delta.content
46
+ if delta:
47
+ response += delta
48
+
49
+ conversation_history.append(
50
+ {"role": "assistant", "content": response}
51
+ )
52
+
53
+ return [
54
+ (
55
+ msg["content"] if msg["role"] == "user" else None,
56
+ msg["content"] if msg["role"] == "assistant" else None
57
+ )
58
+ for msg in conversation_history
59
+ if msg["role"] != "system"
60
+ ]
61
+
62
+ # -------------------------------
63
+ # Storyboard Generator
64
+ # -------------------------------
65
  def generate_storyboard(scenario):
66
  if not scenario.strip():
67
+ return "Please provide a scenario."
68
+
69
  messages = [
70
+ {
71
+ "role": "system",
72
+ "content": (
73
+ "You are an AI storyteller. Generate a storyboard "
74
+ "as a table with exactly six scenes.\n\n"
75
+ "Each scene must include:\n"
76
+ "1) Scenario: the user problem and product/feature used\n"
77
+ "2) Storyline: visual description and purpose\n\n"
78
+ "Output ONLY a markdown table."
79
+ )
80
+ },
81
+ {
82
+ "role": "user",
83
+ "content": f"Generate a 6-scene storyboard for: {scenario}"
84
+ }
85
  ]
86
+
87
  completion = client.chat.completions.create(
88
+ model="llama-3.3-70b-versatile",
89
  messages=messages,
90
  temperature=1,
91
+ max_completion_tokens=1024,
92
  top_p=1,
93
  stream=False,
 
94
  )
95
+
96
  return completion.choices[0].message.content
97
 
98
+ # -------------------------------
99
+ # UI
100
+ # -------------------------------
101
+ with gr.Blocks(theme=gr.themes.Glass()) as demo:
102
+ gr.Markdown("# 📖 Storyboard Assistant")
 
103
 
 
104
  with gr.Tabs():
105
+ with gr.TabItem("💬 Chat"):
106
+ chatbot = gr.Chatbot(height=400)
 
107
  with gr.Row():
108
  user_input = gr.Textbox(
109
+ placeholder="Ask about storyboarding...",
110
+ scale=4
 
111
  )
112
+ send_btn = gr.Button("Ask")
113
+
114
+ send_btn.click(
115
+ fn=chat_with_bot,
 
116
  inputs=user_input,
117
  outputs=chatbot,
118
+ ).then(lambda: "", outputs=user_input)
119
+
 
 
 
 
 
120
  with gr.TabItem("📖 Generate Storyboard"):
121
+ scenario_input = gr.Textbox(
122
+ label="Scenario",
123
+ placeholder="e.g., A student using an AI planner app"
124
+ )
125
  generate_btn = gr.Button("Generate Storyboard")
126
+ storyboard_output = gr.Markdown()
127
+
128
+ generate_btn.click(
129
+ generate_storyboard,
130
+ inputs=scenario_input,
131
+ outputs=storyboard_output
132
+ )
133
 
134
  demo.launch()