Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,7 +3,7 @@ import gradio as gr
|
|
| 3 |
from groq import Groq
|
| 4 |
|
| 5 |
# -----------------------------
|
| 6 |
-
# Groq
|
| 7 |
# -----------------------------
|
| 8 |
client = Groq(api_key=os.getenv("GROQ_API_KEY"))
|
| 9 |
|
|
@@ -13,26 +13,28 @@ SYSTEM_PROMPT = (
|
|
| 13 |
)
|
| 14 |
|
| 15 |
# -----------------------------
|
| 16 |
-
# Chat
|
| 17 |
# -----------------------------
|
| 18 |
-
def chat(
|
| 19 |
if history is None:
|
| 20 |
history = []
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
|
| 26 |
|
| 27 |
-
|
| 28 |
model="llama-3.3-70b-versatile",
|
| 29 |
-
messages=
|
| 30 |
-
temperature=1,
|
| 31 |
max_tokens=512,
|
|
|
|
| 32 |
)
|
| 33 |
|
| 34 |
-
|
| 35 |
-
history.append(
|
| 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 |
-
|
| 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
|
| 57 |
|
| 58 |
|
| 59 |
# -----------------------------
|
| 60 |
-
#
|
| 61 |
# -----------------------------
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 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 |
-
#
|
| 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()
|