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 |
-
#
|
| 7 |
# -----------------------------
|
| 8 |
client = Groq(api_key=os.getenv("GROQ_API_KEY"))
|
| 9 |
|
|
@@ -15,24 +15,24 @@ SYSTEM_PROMPT = (
|
|
| 15 |
# -----------------------------
|
| 16 |
# Chat function
|
| 17 |
# -----------------------------
|
| 18 |
-
def
|
| 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":
|
| 26 |
|
| 27 |
-
|
| 28 |
model="llama-3.3-70b-versatile",
|
| 29 |
messages=history,
|
| 30 |
-
max_tokens=512,
|
| 31 |
temperature=1,
|
|
|
|
| 32 |
)
|
| 33 |
|
| 34 |
-
|
| 35 |
-
history.append({"role": "assistant", "content":
|
| 36 |
|
| 37 |
return history, history
|
| 38 |
|
|
@@ -44,7 +44,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,11 +53,11 @@ def generate_storyboard(scenario):
|
|
| 53 |
max_tokens=700,
|
| 54 |
)
|
| 55 |
|
| 56 |
-
return
|
| 57 |
|
| 58 |
|
| 59 |
# -----------------------------
|
| 60 |
-
# UI
|
| 61 |
# -----------------------------
|
| 62 |
with gr.Blocks(title="Storyboard Assistant") as demo:
|
| 63 |
gr.Markdown("## ๐ Storyboard Assistant")
|
|
@@ -68,26 +68,28 @@ with gr.Blocks(title="Storyboard Assistant") as demo:
|
|
| 68 |
state = gr.State([])
|
| 69 |
|
| 70 |
user_input = gr.Textbox(label="Your Message")
|
| 71 |
-
|
| 72 |
|
| 73 |
-
|
| 74 |
-
|
| 75 |
inputs=[user_input, state],
|
| 76 |
outputs=[chatbot, state],
|
| 77 |
)
|
| 78 |
|
| 79 |
with gr.Tab("๐ Generate Storyboard"):
|
| 80 |
scenario = gr.Textbox(label="Scenario")
|
| 81 |
-
|
| 82 |
output = gr.Textbox(lines=12)
|
| 83 |
|
| 84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
|
| 87 |
# -----------------------------
|
| 88 |
-
#
|
| 89 |
# -----------------------------
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
show_api=False # โ
prevents /api/info crash
|
| 93 |
-
)
|
|
|
|
| 3 |
from groq import Groq
|
| 4 |
|
| 5 |
# -----------------------------
|
| 6 |
+
# Groq Client
|
| 7 |
# -----------------------------
|
| 8 |
client = Groq(api_key=os.getenv("GROQ_API_KEY"))
|
| 9 |
|
|
|
|
| 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 |
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 |
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")
|
|
|
|
| 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()
|
|
|
|
|
|