Rahatara commited on
Commit
e3e5585
·
verified ·
1 Parent(s): ba530ef

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -72
app.py CHANGED
@@ -3,100 +3,82 @@ 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="llama-3.3-70b-versatile",
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."},
49
- {"role": "user", "content": f"Generate a 6-scene storyboard for: {scenario}"}
50
  ]
51
-
52
  completion = client.chat.completions.create(
53
  model="llama-3.3-70b-versatile",
54
  messages=messages,
55
  temperature=1,
56
  max_tokens=1024,
57
- top_p=1,
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
- with gr.Blocks(theme=gr.themes.Glass(primary_hue="violet", secondary_hue="emerald", neutral_hue="stone")) as demo:
 
 
71
  with gr.Tabs():
72
- with gr.TabItem("💬Chat"):
73
- gr.HTML(TITLE)
74
- chatbot = gr.Chatbot(label="Storyboard Chatbot")
75
- with gr.Row():
76
- user_input = gr.Textbox(
77
- label="Your Message",
78
- placeholder="Type your question here...",
79
- lines=1
80
- )
81
- send_button = gr.Button("✋Ask Question")
82
-
83
- # Chatbot functionality
84
- send_button.click(
85
- fn=chat_with_bot_stream,
86
- inputs=user_input,
87
- outputs=chatbot,
88
- queue=True
89
- ).then(
90
- fn=lambda: "",
91
- inputs=None,
92
- outputs=user_input
93
  )
94
-
95
- with gr.TabItem("📖 Generate Storyboard"):
96
- gr.Markdown("## Generate a Storyboard")
97
- scenario_input = gr.Textbox(label="Enter your scenario")
98
- generate_btn = gr.Button("Generate Storyboard")
99
- storyboard_output = gr.Textbox(label="Generated Storyboard", interactive=False)
100
- generate_btn.click(generate_storyboard, inputs=scenario_input, outputs=storyboard_output)
 
 
 
 
 
 
 
101
 
102
  demo.launch()
 
3
  from groq import Groq
4
 
5
  # Initialize Groq client
6
+ client = Groq(api_key=os.getenv("GROQ_API_KEY"))
7
+
8
+ # Conversation memory
9
+ conversation_history = [
10
+ {
11
+ "role": "system",
12
+ "content": "You are an expert in storyboarding. Provide structured and insightful responses."
13
+ }
14
+ ]
15
+
16
+ def chat_with_bot(user_input, history):
17
+ history = history or conversation_history
18
+ history.append({"role": "user", "content": user_input})
19
+
 
 
20
  completion = client.chat.completions.create(
21
  model="llama-3.3-70b-versatile",
22
+ messages=history,
23
  temperature=1,
24
  max_tokens=1024,
25
  top_p=1,
26
+ stream=False,
 
27
  )
28
+
29
+ assistant_msg = completion.choices[0].message.content
30
+ history.append({"role": "assistant", "content": assistant_msg})
31
+
32
+ return history, history
33
+
 
 
 
 
 
 
34
  def generate_storyboard(scenario):
35
  if not scenario.strip():
36
+ return "Please provide a scenario."
37
+
38
  messages = [
39
+ {"role": "system", "content": "Generate a 6-scene storyboard in table form."},
40
+ {"role": "user", "content": scenario}
41
  ]
42
+
43
  completion = client.chat.completions.create(
44
  model="llama-3.3-70b-versatile",
45
  messages=messages,
46
  temperature=1,
47
  max_tokens=1024,
 
 
 
48
  )
49
+
50
  return completion.choices[0].message.content
51
 
52
+
53
  TITLE = """
54
+ <h1 style="text-align:center;">📖 Storyboard Assistant</h1>
 
 
 
55
  """
56
 
57
+ with gr.Blocks(theme=gr.themes.Glass()) as demo:
58
+ gr.HTML(TITLE)
59
+
60
  with gr.Tabs():
61
+ with gr.Tab("💬 Chat"):
62
+ chatbot = gr.Chatbot(type="messages", label="Storyboard Chatbot")
63
+ state = gr.State([])
64
+
65
+ user_input = gr.Textbox(
66
+ placeholder="Ask about storyboards...",
67
+ label="Your Message"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68
  )
69
+ send = gr.Button("Ask")
70
+
71
+ send.click(
72
+ chat_with_bot,
73
+ inputs=[user_input, state],
74
+ outputs=[chatbot, state]
75
+ )
76
+
77
+ with gr.Tab("📖 Generate Storyboard"):
78
+ scenario = gr.Textbox(label="Scenario")
79
+ generate = gr.Button("Generate")
80
+ output = gr.Textbox(label="Storyboard", lines=12)
81
+
82
+ generate.click(generate_storyboard, scenario, output)
83
 
84
  demo.launch()