Spaces:
Sleeping
Sleeping
Update app.py
Browse files1 unmodified line2345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455561 unmodified line58596061626364656667683 unmodified lines727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221 unmodified lineimport gradio as grfrom groq import Groq# -------------------------------# Groq Client (HF-safe)# -------------------------------client = Groq(api_key=os.getenv("GROQ_API_KEY"))# -------------------------------# In-memory chat history# -------------------------------chat_history = []# -------------------------------# Chat function (NON-streaming)# -------------------------------def chat_with_bot(user_input, history):messages = [{"role": "system","content": ("You are an expert in storyboarding. ""Provide clear, structured, and visual guidance.")}]for user, assistant in history:if user:messages.append({"role": "user", "content": user})if assistant:messages.append({"role": "assistant", "content": assistant})messages.append({"role": "user", "content": user_input})completion = client.chat.completions.create(model="llama-3.3-70b-versatile",messages=messages,temperature=1,max_completion_tokens=512,top_p=1,stream=False,)reply = completion.choices[0].message.contenthistory.append((user_input, reply))return history, ""# -------------------------------# Storyboard Generator# -------------------------------def generate_storyboard(scenario):if not scenario.strip():return "Please enter a scenario."1 unmodified line{"role": "system","content": ("Generate a storyboard as a markdown table with EXACTLY six scenes.\n\n""Columns:\n""- Scene\n""- Scenario (problem + product/feature)\n""- Storyline (visual description + purpose)\n\n""Return ONLY the table.")},3 unmodified lines}]completion = client.chat.completions.create(model="llama-3.3-70b-versatile",messages=messages,temperature=1,max_completion_tokens=700,top_p=1,stream=False,)return completion.choices[0].message.content# -------------------------------# UI# -------------------------------with gr.Blocks() as demo:gr.Markdown("# π Storyboard Assistant")with gr.Tabs():# ---- Chat Tab ----with gr.TabItem("π¬ Chat"):chatbot = gr.Chatbot(height=400)user_input = gr.Textbox(placeholder="Ask a storyboarding question...")send_btn = gr.Button("Send")send_btn.click(chat_with_bot,inputs=[user_input, chatbot],outputs=[chatbot, user_input])# ---- Storyboard Tab ----with gr.TabItem("π Generate Storyboard"):scenario_input = gr.Textbox(label="Scenario",placeholder="A student using an AI planner to manage exams")generate_btn = gr.Button("Generate")output = gr.Markdown()generate_btn.click(generate_storyboard,inputs=scenario_input,outputs=output)1 unmodified line23456789101112131415161718192021222324252627282930313233343536373839404142434445464748491 unmodified line5152535455565758593 unmodified lines636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101 unmodified lineimport gradio as grfrom groq import Groq# -------------------------------------------------# Groq Client# -------------------------------------------------client = Groq(api_key=os.getenv("GROQ_API_KEY"))# -------------------------------------------------# Chat Function (Groq mode, non-streaming)# -------------------------------------------------def chat_fn(message, history):messages = [{"role": "system","content": ("You are an expert in storyboarding. ""Give clear, structured, and visual guidance.")}]# Rebuild conversation from Gradio historyfor user, assistant in history:messages.append({"role": "user", "content": user})messages.append({"role": "assistant", "content": assistant})messages.append({"role": "user", "content": message})response = client.chat.completions.create(model="llama-3.3-70b-versatile",messages=messages,temperature=1,max_completion_tokens=512,top_p=1,)assistant_reply = response.choices[0].message.contenthistory.append((message, assistant_reply))return history# -------------------------------------------------# Storyboard Generator# -------------------------------------------------def storyboard_fn(scenario):if not scenario.strip():return "Please enter a scenario."1 unmodified line{"role": "system","content": ("Create a storyboard as a MARKDOWN TABLE with EXACTLY 6 scenes.\n\n""Columns:\n""Scene | Scenario (problem + product/feature) | Storyline (visual + purpose)\n\n""Return ONLY the table.")},3 unmodified lines}]response = client.chat.completions.create(model="llama-3.3-70b-versatile",messages=messages,temperature=1,max_completion_tokens=700,top_p=1,)return response.choices[0].message.content# -------------------------------------------------# UI# -------------------------------------------------with gr.Blocks() as demo:gr.Markdown("# π Storyboard Assistant (Groq)")with gr.Tabs():with gr.TabItem("π¬ Chat"):chatbot = gr.Chatbot(height=400)user_input = gr.Textbox(placeholder="Ask about storyboarding...")send = gr.Button("Send")send.click(chat_fn,inputs=[user_input, chatbot],outputs=chatbot)with gr.TabItem("π Generate Storyboard"):scenario = gr.Textbox(label="Scenario",placeholder="A student using an AI planner to manage exams")generate = gr.Button("Generate")output = gr.Markdown()generate.click(storyboard_fn,inputs=scenario,outputs=output)
app.py+36-48