Spaces:
Sleeping
Sleeping
Create app.py
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170import gradio as grimport osfrom groq import Groq
# Initialize Groq clientapi_key = os.getenv("GROQ_API_KEY")client = Groq(api_key=api_key)
# Initialize conversation historyconversation_history = []
def chat_with_bot_stream(user_input, temperature, top_p): global conversation_history conversation_history.append({"role": "user", "content": user_input}) if len(conversation_history) == 1: conversation_history.insert(0, { "role": "system", "content": "You are an expert in storyboarding. Provide structured and insightful responses to queries about creating and refining storyboards." }) completion = client.chat.completions.create( model="llama3-70b-8192", messages=conversation_history, temperature=temperature, top_p=top_p, max_tokens=1024, stream=True, stop=None, ) response_content = "" for chunk in completion: text_chunk = chunk.choices[0].delta.content or "" response_content += text_chunk yield [(msg["content"] if msg["role"] == "user" else None, msg["content"] if msg["role"] == "assistant" else None) for msg in conversation_history] + [(user_input, response_content)] conversation_history.append({"role": "assistant", "content": response_content})
# Function to generate a storyboarddef generate_storyboard(scenario, temperature, top_p): if not scenario.strip(): return "Please provide a scenario to generate the storyboard." messages = [ {"role": "system", "content": "You are an AI storyteller. Generate a storyboard in a structured table with six scenes."}, {"role": "user", "content": f"Generate a 6-scene storyboard for: {scenario}"} ] completion = client.chat.completions.create( model="llama3-70b-8192", messages=messages, temperature=temperature, top_p=top_p, max_tokens=1024, stream=False, stop=None, ) return completion.choices[0].message.content
TITLE = """<style>h1 { text-align: center; font-size: 24px; margin-bottom: 10px; }</style><h1>📖 Storyboard Assistant</h1>"""
example_scenarios = [ "A futuristic cityscape under AI governance.", "A detective solving a mystery in a cyberpunk world.", "A young explorer discovering an ancient civilization.", "A spaceship crew encountering an unknown planet.", "A medieval knight navigating political intrigue."]
example_questions = [ "How do I create a compelling storyboard?", "What are the key elements of a good visual story?", "How can AI help with storyboarding?", "What are common mistakes in storyboarding?", "How do I structure a scene effectively?"]
temperature_component = gr.Slider( minimum=0, maximum=1, value=1, step=0.01, label="Temperature", info="Controls randomness. Lower values make responses more deterministic.")
top_p_component = gr.Slider( minimum=0, maximum=1, value=1, step=0.01, label="Top-P Sampling", info="Limits token selection to tokens with a cumulative probability up to P.")
with gr.Blocks(theme=gr.themes.Glass(primary_hue="violet", secondary_hue="emerald", neutral_hue="stone")) as demo: with gr.Tabs(): with gr.TabItem("💬Chat"): gr.HTML(TITLE) chatbot = gr.Chatbot(label="Storyboard Chatbot", type="messages") with gr.Row(): user_input = gr.Textbox( label="Your Message", placeholder="Type your question here...", lines=1 ) send_button = gr.Button("✋Ask Question") with gr.Accordion("🛠️ Customize Chatbot", open=False): gr.Row([temperature_component, top_p_component]) with gr.Accordion("🧪 Example Questions", open=False): example_radio = gr.Radio( choices=example_questions, label="Example Queries", info="Select an example question or enter your own." ) example_radio.change( fn=lambda q: q if q else "No question selected.", inputs=[example_radio], outputs=[user_input] ) # Chatbot functionality send_button.click( fn=chat_with_bot_stream, inputs=[user_input, temperature_component, top_p_component], outputs=chatbot, queue=True ).then( fn=lambda: "", inputs=None, outputs=user_input ) with gr.TabItem("📖 Generate Storyboard"): gr.Markdown("## Generate a Storyboard") scenario_input = gr.Textbox(label="Enter your scenario") example_radio = gr.Radio( choices=example_scenarios, label="Example Scenarios", info="Select an example scenario or enter your own." ) generate_btn = gr.Button("Generate Storyboard") storyboard_output = gr.Textbox(label="Generated Storyboard", interactive=False) with gr.Accordion("🛠️ Customize Storyboard", open=False): gr.Row([temperature_component, top_p_component]) generate_btn.click( generate_storyboard, inputs=[scenario_input, temperature_component, top_p_component], outputs=storyboard_output ) example_radio.change( fn=lambda scenario: scenario if scenario else "No scenario selected.", inputs=[example_radio], outputs=[scenario_input] )
demo.launch()app.pyAdded+170