Rahatara commited on
Commit
41bfd24
ยท
verified ยท
1 Parent(s): de72b93

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -63
app.py CHANGED
@@ -3,84 +3,72 @@ 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
 
@@ -88,7 +76,7 @@ def generate_storyboard(scenario):
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
  )
@@ -98,37 +86,38 @@ def generate_storyboard(scenario):
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()
 
3
  from groq import Groq
4
 
5
  # -------------------------------
6
+ # Groq Client (HF-safe)
7
  # -------------------------------
8
  client = Groq(api_key=os.getenv("GROQ_API_KEY"))
9
 
10
  # -------------------------------
11
+ # In-memory chat history
12
  # -------------------------------
13
+ chat_history = []
 
 
 
 
 
 
 
 
 
14
 
15
  # -------------------------------
16
+ # Chat function (NON-streaming)
17
  # -------------------------------
18
+ def chat_with_bot(user_input, history):
19
+ messages = [
20
+ {
21
+ "role": "system",
22
+ "content": (
23
+ "You are an expert in storyboarding. "
24
+ "Provide clear, structured, and visual guidance."
25
+ )
26
+ }
27
+ ]
28
 
29
+ for user, assistant in history:
30
+ if user:
31
+ messages.append({"role": "user", "content": user})
32
+ if assistant:
33
+ messages.append({"role": "assistant", "content": assistant})
34
+
35
+ messages.append({"role": "user", "content": user_input})
36
 
37
  completion = client.chat.completions.create(
38
  model="llama-3.3-70b-versatile",
39
+ messages=messages,
40
  temperature=1,
41
+ max_completion_tokens=512,
42
  top_p=1,
43
+ stream=False,
44
  )
45
 
46
+ reply = completion.choices[0].message.content
47
+ history.append((user_input, reply))
48
+ return history, ""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
 
50
  # -------------------------------
51
  # Storyboard Generator
52
  # -------------------------------
53
  def generate_storyboard(scenario):
54
  if not scenario.strip():
55
+ return "Please enter a scenario."
56
 
57
  messages = [
58
  {
59
  "role": "system",
60
  "content": (
61
+ "Generate a storyboard as a markdown table with EXACTLY six scenes.\n\n"
62
+ "Columns:\n"
63
+ "- Scene\n"
64
+ "- Scenario (problem + product/feature)\n"
65
+ "- Storyline (visual description + purpose)\n\n"
66
+ "Return ONLY the table."
67
  )
68
  },
69
  {
70
  "role": "user",
71
+ "content": scenario
72
  }
73
  ]
74
 
 
76
  model="llama-3.3-70b-versatile",
77
  messages=messages,
78
  temperature=1,
79
+ max_completion_tokens=700,
80
  top_p=1,
81
  stream=False,
82
  )
 
86
  # -------------------------------
87
  # UI
88
  # -------------------------------
89
+ with gr.Blocks() as demo:
90
  gr.Markdown("# ๐Ÿ“– Storyboard Assistant")
91
 
92
  with gr.Tabs():
93
+
94
+ # ---- Chat Tab ----
95
  with gr.TabItem("๐Ÿ’ฌ Chat"):
96
  chatbot = gr.Chatbot(height=400)
97
+ user_input = gr.Textbox(
98
+ placeholder="Ask a storyboarding question..."
99
+ )
100
+ send_btn = gr.Button("Send")
 
 
101
 
102
  send_btn.click(
103
+ chat_with_bot,
104
+ inputs=[user_input, chatbot],
105
+ outputs=[chatbot, user_input]
106
+ )
107
 
108
+ # ---- Storyboard Tab ----
109
  with gr.TabItem("๐Ÿ“– Generate Storyboard"):
110
  scenario_input = gr.Textbox(
111
  label="Scenario",
112
+ placeholder="A student using an AI planner to manage exams"
113
  )
114
+ generate_btn = gr.Button("Generate")
115
+ output = gr.Markdown()
116
 
117
  generate_btn.click(
118
  generate_storyboard,
119
  inputs=scenario_input,
120
+ outputs=output
121
  )
122
 
123
  demo.launch()