Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,109 +3,63 @@ 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
|
| 12 |
# -------------------------------------------------
|
| 13 |
-
def
|
| 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 |
-
#
|
| 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 |
-
|
| 32 |
model="llama-3.3-70b-versatile",
|
| 33 |
messages=messages,
|
| 34 |
temperature=1,
|
| 35 |
-
max_completion_tokens=
|
| 36 |
top_p=1,
|
|
|
|
|
|
|
| 37 |
)
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
# -------------------------------------------------
|
| 46 |
-
def storyboard_fn(scenario):
|
| 47 |
-
if not scenario.strip():
|
| 48 |
-
return "Please enter a scenario."
|
| 49 |
-
|
| 50 |
-
messages = [
|
| 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 |
-
},
|
| 60 |
-
{
|
| 61 |
-
"role": "user",
|
| 62 |
-
"content": scenario
|
| 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 |
-
|
| 85 |
-
chatbot = gr.Chatbot(height=400)
|
| 86 |
-
user_input = gr.Textbox(
|
| 87 |
-
placeholder="Ask about storyboarding..."
|
| 88 |
-
)
|
| 89 |
-
send = gr.Button("Send")
|
| 90 |
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
outputs=chatbot
|
| 95 |
-
)
|
| 96 |
|
| 97 |
-
|
| 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 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
|
| 111 |
demo.launch()
|
|
|
|
| 3 |
from groq import Groq
|
| 4 |
|
| 5 |
# -------------------------------------------------
|
| 6 |
+
# Groq Client (uses HF secret GROQ_API_KEY)
|
| 7 |
# -------------------------------------------------
|
| 8 |
client = Groq(api_key=os.getenv("GROQ_API_KEY"))
|
| 9 |
|
| 10 |
# -------------------------------------------------
|
| 11 |
+
# Chat streaming generator (Groq correct usage)
|
| 12 |
# -------------------------------------------------
|
| 13 |
+
def chat_stream(message, history):
|
| 14 |
messages = [
|
| 15 |
{
|
| 16 |
"role": "system",
|
| 17 |
+
"content": "act like a storyboard generator"
|
|
|
|
|
|
|
|
|
|
| 18 |
}
|
| 19 |
]
|
| 20 |
|
| 21 |
+
# rebuild conversation from Gradio history
|
| 22 |
for user, assistant in history:
|
| 23 |
messages.append({"role": "user", "content": user})
|
| 24 |
messages.append({"role": "assistant", "content": assistant})
|
| 25 |
|
| 26 |
messages.append({"role": "user", "content": message})
|
| 27 |
|
| 28 |
+
completion = client.chat.completions.create(
|
| 29 |
model="llama-3.3-70b-versatile",
|
| 30 |
messages=messages,
|
| 31 |
temperature=1,
|
| 32 |
+
max_completion_tokens=1024,
|
| 33 |
top_p=1,
|
| 34 |
+
stream=True # ✅ streaming ON
|
| 35 |
+
# ❌ stop=None REMOVED (this caused the TypeError)
|
| 36 |
)
|
| 37 |
|
| 38 |
+
response = ""
|
| 39 |
+
for chunk in completion:
|
| 40 |
+
delta = chunk.choices[0].delta.content
|
| 41 |
+
if delta:
|
| 42 |
+
response += delta
|
| 43 |
+
yield history + [(message, response)]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
# -------------------------------------------------
|
| 46 |
# UI
|
| 47 |
# -------------------------------------------------
|
| 48 |
with gr.Blocks() as demo:
|
| 49 |
+
gr.Markdown("# 📖 Storyboard Assistant (Groq Streaming)")
|
|
|
|
|
|
|
| 50 |
|
| 51 |
+
chatbot = gr.Chatbot(height=450)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
+
user_input = gr.Textbox(
|
| 54 |
+
placeholder="Ask about storyboarding..."
|
| 55 |
+
)
|
|
|
|
|
|
|
| 56 |
|
| 57 |
+
send = gr.Button("Send")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
+
send.click(
|
| 60 |
+
chat_stream,
|
| 61 |
+
inputs=[user_input, chatbot],
|
| 62 |
+
outputs=chatbot
|
| 63 |
+
)
|
| 64 |
|
| 65 |
demo.launch()
|