Rahatara commited on
Commit
6c1e129
Β·
verified Β·
1 Parent(s): 324db76

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -42
app.py CHANGED
@@ -3,7 +3,7 @@ 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
 
@@ -13,26 +13,28 @@ SYSTEM_PROMPT = (
13
  )
14
 
15
  # -----------------------------
16
- # Chat function
17
  # -----------------------------
18
- def chat(user_message, history):
19
  if history is None:
20
  history = []
21
 
22
- if not history:
23
- history.append({"role": "system", "content": SYSTEM_PROMPT})
 
 
24
 
25
- history.append({"role": "user", "content": user_message})
26
 
27
- response = client.chat.completions.create(
28
  model="llama-3.3-70b-versatile",
29
- messages=history,
30
- temperature=1,
31
  max_tokens=512,
 
32
  )
33
 
34
- assistant_reply = response.choices[0].message.content
35
- history.append({"role": "assistant", "content": assistant_reply})
36
 
37
  return history, history
38
 
@@ -44,7 +46,7 @@ def generate_storyboard(scenario):
44
  if not scenario.strip():
45
  return "Please enter a scenario."
46
 
47
- response = client.chat.completions.create(
48
  model="llama-3.3-70b-versatile",
49
  messages=[
50
  {"role": "system", "content": "Generate a 6-scene storyboard in table format."},
@@ -53,43 +55,39 @@ def generate_storyboard(scenario):
53
  max_tokens=700,
54
  )
55
 
56
- return response.choices[0].message.content
57
 
58
 
59
  # -----------------------------
60
- # Gradio UI
61
  # -----------------------------
62
- with gr.Blocks(title="Storyboard Assistant") as demo:
63
- gr.Markdown("## πŸ“– Storyboard Assistant")
64
-
65
- with gr.Tabs():
66
- with gr.Tab("πŸ’¬ Chat"):
67
- chatbot = gr.Chatbot(type="messages")
68
- state = gr.State([])
69
-
70
- user_input = gr.Textbox(label="Your Message")
71
- send_btn = gr.Button("Ask")
72
-
73
- send_btn.click(
74
- chat,
75
- inputs=[user_input, state],
76
- outputs=[chatbot, state],
77
- )
78
-
79
- with gr.Tab("πŸ“– Generate Storyboard"):
80
- scenario = gr.Textbox(label="Scenario")
81
- generate_btn = gr.Button("Generate")
82
- output = gr.Textbox(lines=12)
83
-
84
- generate_btn.click(
85
- generate_storyboard,
86
- inputs=scenario,
87
- outputs=output,
88
- )
89
 
 
 
 
 
 
 
90
 
91
  # -----------------------------
92
- # Launch (CRITICAL FIX)
93
  # -----------------------------
 
 
 
 
 
94
  if __name__ == "__main__":
95
  demo.launch()
 
3
  from groq import Groq
4
 
5
  # -----------------------------
6
+ # Initialize Groq
7
  # -----------------------------
8
  client = Groq(api_key=os.getenv("GROQ_API_KEY"))
9
 
 
13
  )
14
 
15
  # -----------------------------
16
+ # Chat logic (NO message schema)
17
  # -----------------------------
18
+ def chat(user_input, history):
19
  if history is None:
20
  history = []
21
 
22
+ messages = [{"role": "system", "content": SYSTEM_PROMPT}]
23
+ for u, a in history:
24
+ messages.append({"role": "user", "content": u})
25
+ messages.append({"role": "assistant", "content": a})
26
 
27
+ messages.append({"role": "user", "content": user_input})
28
 
29
+ completion = client.chat.completions.create(
30
  model="llama-3.3-70b-versatile",
31
+ messages=messages,
 
32
  max_tokens=512,
33
+ temperature=1,
34
  )
35
 
36
+ reply = completion.choices[0].message.content
37
+ history.append((user_input, reply))
38
 
39
  return history, history
40
 
 
46
  if not scenario.strip():
47
  return "Please enter a scenario."
48
 
49
+ completion = client.chat.completions.create(
50
  model="llama-3.3-70b-versatile",
51
  messages=[
52
  {"role": "system", "content": "Generate a 6-scene storyboard in table format."},
 
55
  max_tokens=700,
56
  )
57
 
58
+ return completion.choices[0].message.content
59
 
60
 
61
  # -----------------------------
62
+ # UI (NO Blocks, NO schema)
63
  # -----------------------------
64
+ chat_ui = gr.Interface(
65
+ fn=chat,
66
+ inputs=[
67
+ gr.Textbox(label="Your Message"),
68
+ gr.State([])
69
+ ],
70
+ outputs=[
71
+ gr.Chatbot(label="Storyboard Chat"),
72
+ gr.State([])
73
+ ],
74
+ title="πŸ“– Storyboard Assistant",
75
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76
 
77
+ storyboard_ui = gr.Interface(
78
+ fn=generate_storyboard,
79
+ inputs=gr.Textbox(label="Scenario"),
80
+ outputs=gr.Textbox(lines=12),
81
+ title="πŸ“– Storyboard Generator",
82
+ )
83
 
84
  # -----------------------------
85
+ # App
86
  # -----------------------------
87
+ demo = gr.TabbedInterface(
88
+ [chat_ui, storyboard_ui],
89
+ ["πŸ’¬ Chat", "πŸ“– Storyboard"]
90
+ )
91
+
92
  if __name__ == "__main__":
93
  demo.launch()