Spaces:
Sleeping
Sleeping
import os | |
import io | |
import streamlit as st | |
from groq import Groq | |
# Set up Groq client using the environment variable for API key. | |
client = Groq(api_key=os.environ.get("GROQ_API_KEY")) | |
# Initialize session state for chat history if it doesn't exist. | |
if "chat_history" not in st.session_state: | |
st.session_state.chat_history = [] # Each entry: {"role": "user"/"assistant", "content": "..."} | |
# Speaker options (unused in chat but kept for reference) | |
speaker_options = { | |
"Liam": "A male voice", | |
"Dorothy": "A female voice", | |
} | |
# Build the Streamlit UI header. | |
st.title("Conversation Script Generator Using Groq API") | |
# Create columns for left/right speaker inputs. | |
col1, col2 = st.columns(2) | |
with col1: | |
left_speaker = st.text_input("Left Speaker", placeholder="Enter left speaker name") | |
with col2: | |
right_speaker = st.text_input("Right Speaker", placeholder="Enter right speaker name") | |
# Text area for overall conversation theme. | |
theme = st.text_area("Overall Theme of the Conversation", height=100) | |
# Optional additional details. | |
additional_details = st.text_area("Additional Conversation Details (Optional)", height=100) | |
# Determine if required fields are filled. | |
is_ready = bool(theme and left_speaker and right_speaker) | |
# Container for initial conversation generation. | |
with st.container(): | |
if st.button("Generate Conversation", disabled=not is_ready): | |
# Build the initial prompt. | |
prompt = f""" | |
๐ Objective | |
Generate a highly engaging and comedic iMessage chat script formatted for video generation. | |
The script follows a structured left-right chat format, maintains a meme-heavy, brain rot style, and incorporates sound effects, reactions, and the Cantina Roast Bot segment in a specific format. | |
๐ Script Structure & Formatting Rules | |
1๏ธ- Contact Name (Title) | |
At the beginning of the script, define the contact name, appearing at the top of the iMessage screen. | |
<Contact Name>:<Contact Name> | |
โ Example: | |
Amir (expensive af delivery guy๐๐ธ):Amir (expensive af delivery guy๐๐ธ) | |
2๏ธ- Dialogue Format | |
Each dialogue line follows this format: | |
<Direction>: <SpeakerName> > <Phrase 1> <Phrase 2> <Phrase 3> <Phrase 4> [Reaction] | |
Left: Represents the left speaker (User). | |
Right: Represents the right speaker (Other character). | |
SpeakerName: A randomly assigned name, but characters do not know their own names. | |
Phrase Limit: Max 4 phrases per line. | |
Reaction Cues: Placed at the end of lines, enclosed in square brackets []. | |
โ Example: | |
Left: Unknown Guy> Bro what is this? This is not food [shocked] | |
Right: Amir> My guy that's a gourmet dish ๐คจ [suspense_music] | |
Left: Unknown Guy> Gourmet?? Itโs literally burnt [explosion] | |
Right: Amir> Charcoal adds flavor, trust me bro ๐ | |
3๏ธ- Bracketed Cues & Sound Effects | |
Use brackets [ ] for sound effects, reactions, or video cues. | |
[shocked] โ Must be used often | |
[fart2] โ Should be used less | |
[fortnite] โ Should be used less | |
[get_out] โ Must ONLY be used at the end of the conversation | |
โ Example: | |
Right: Amir> BRO WHY IS THERE A RAT IN MY BURGER?!! [shocked] ๐๐ | |
Left: Unknown Guy> Extra protein sir ๐ | |
Right: Amir> ๐๐๐ [fortnite] | |
4๏ธ- Image Placeholders | |
For inserting images, use: | |
<Direction>: img:<DescriptiveImageName> | |
โ Example: | |
Left: img:burnt_food_delivery | |
Right: Amir> BRO WHY IS MY PIZZA LOOKING LIKE CHARCOAL?!! [shocked] | |
Min. 3 images per script. | |
Descriptive placeholders must be clear. | |
5๏ธ- Cantina App (Roast Bot Segment) | |
๐จ Correct Format (ALWAYS THE SAME STRUCTURE) | |
Right speaker initiates by opening Cantina. | |
Right speaker asks Roast Bot for help. | |
Left speaker (ALWAYS Antoni) responds. | |
Right speaker must repeat what Roast Bot told them to say. | |
Contact Name Title must reappear after Cantina. | |
โ Correct Example: | |
Cantina: Roast bot:Roast bot pfp == Alex> Let me open the cantina app to talk to Roast bot | |
Right: Alex> Hey Roast Bot | |
Right: Alex> Amir is mad at me for not wanting to tip him | |
Right: Alex> help me roast him | |
Left: Antoni> okay | |
Left: Antoni> tell him this: | |
Left: Antoni> maybe you should start working for real and not begging for tips all the time | |
Amir (expensive af delivery guy๐๐ธ):Amir (expensive af delivery guy๐๐ธ) | |
Right: Alex> maybe you should start working for real and not begging for tips all the time | |
๐น Rules: | |
Right speaker MUST repeat the roast word-for-word. | |
The contact name/title must reappear after the Cantina segment. | |
DO NOT GIVE HEADING TO SEGMENTS, all the script should be in one flow and should be at least 30 messages long | |
6๏ธโฃ Brain Rot Hook (MANDATORY) | |
Every script must begin with an insane, wacky brain rot hook that immediately grabs attention. | |
โ Examples: | |
"๐จ BRO I JUST ORDERED UBER EATS AND THIS GUY SHOWED UP IN A SPACESHIP???" ๐๐ | |
"WHY DID MY DAD JUST SAY HE WANTS TO PLAY GTA WITH ME???" ๐ฎ๐ | |
"SIR WHY IS MY LAWYER ALSO MY UBER DRIVER?!" ๐จโโ๐๐จ | |
Must be over-the-top and chaotic. | |
Encourages immediate engagement. | |
7๏ธ- Escalation & Progression | |
The story must escalate into chaos but stay coherent: | |
Start Crazy โ Get Crazier โ End With a Punchline or Twist | |
Use sudden twists and reveals to maintain engagement. | |
The final line should be either a funny resolution or an abrupt exit. | |
โ Example Progression: | |
๐ Crazy Hook: | |
"BRO WHY IS MY PIZZA DELIVERY GUY ALSO MY LAWYER???" ๐๐จโโ | |
๐ฅ Escalation: | |
"Sir, we found out you stole a fighter jet." | |
๐ญ Insane Event: | |
"Oh that? I do that every Tuesday, itโs fine." | |
๐คฏ Twist: | |
"Lawyer wins case in 5 seconds and gets a $2 tip." | |
๐โโ๏ธ Dramatic Exit (ALWAYS USE [get_out]) | |
"Left: Lawyer> I quit. [get_out]" | |
DO NOT USE THESE NAMES IN CONTACT OR IN TEXTS, THESE ARE ONLY TO TELL THE PROGRAM WHICH VOICE TO USE | |
Left Actor = {left_speaker} | |
Right Actor = {right_speaker} | |
Theme = {theme} | |
""" | |
if additional_details.strip(): | |
prompt += f"\nAdditional Details: {additional_details}\n" | |
# Start the conversation history with the initial prompt. | |
st.session_state.chat_history = [{"role": "user", "content": prompt}] | |
model = "llama-3.3-70b-versatile" | |
try: | |
chat_completion = client.chat.completions.create( | |
messages=st.session_state.chat_history, | |
model=model, | |
temperature=1.2, | |
max_completion_tokens=21890, | |
) | |
result_text = chat_completion.choices[0].message.content | |
if not result_text: | |
st.error("The API call did not return any content.") | |
else: | |
st.success("Conversation generated successfully!") | |
# Append the generated conversation to chat history. | |
st.session_state.chat_history.append({"role": "assistant", "content": result_text}) | |
# Display the generated conversation. | |
st.write("### Generated Conversation Script") | |
st.text_area("", result_text, height=300) | |
# Offer a download of the script. | |
txt_bytes = result_text.encode("utf-8") | |
txt_io = io.BytesIO(txt_bytes) | |
st.download_button( | |
label="Download Script as TXT", | |
data=txt_io, | |
file_name="conversation_script.txt", | |
mime="text/plain" | |
) | |
except Exception as e: | |
st.error(f"An error occurred while calling the API: {e}") | |
# ------------------------------------------------------------------ | |
# Chat Interface Section | |
# This section will appear after the initial generation (or if history already exists) | |
st.markdown("---") | |
st.header("Chat & Modify Conversation Script") | |
# Display the chat history. | |
for msg in st.session_state.chat_history: | |
if msg["role"] == "user": | |
st.markdown(f"**User:** {msg['content']}") | |
else: | |
st.markdown(f"**Assistant:** {msg['content']}") | |
# Form to send a new chat message. | |
with st.form(key="chat_form", clear_on_submit=True): | |
user_message = st.text_area("Enter your message (e.g., request modifications)", height=100) | |
submit_chat = st.form_submit_button("Send") | |
if submit_chat and user_message.strip(): | |
# Append the new user message to chat history. | |
st.session_state.chat_history.append({"role": "user", "content": user_message}) | |
# Call the Groq API with the entire conversation history to keep memory intact. | |
try: | |
model = "llama-3.3-70b-versatile" | |
chat_completion = client.chat.completions.create( | |
messages=st.session_state.chat_history, | |
model=model, | |
temperature=1.2, | |
max_completion_tokens=21890, | |
) | |
reply = chat_completion.choices[0].message.content | |
st.session_state.chat_history.append({"role": "assistant", "content": reply}) | |
st.success("Response received!") | |
except Exception as e: | |
st.error(f"An error occurred while calling the API: {e}") | |
# Refresh the chat display. | |
st.experimental_rerun() | |