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

Update app.py

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