Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,38 +8,42 @@ client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
|
|
| 8 |
|
| 9 |
# Initialize session state for chat history if it doesn't exist.
|
| 10 |
if "chat_history" not in st.session_state:
|
| 11 |
-
st.session_state.chat_history = [] #
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
# Speaker options (unused in chat but kept for reference)
|
| 14 |
-
speaker_options = {
|
| 15 |
-
"Liam": "A male voice",
|
| 16 |
-
"Dorothy": "A female voice",
|
| 17 |
-
}
|
| 18 |
-
|
| 19 |
-
# Build the Streamlit UI header.
|
| 20 |
st.title("Conversation Script Generator Using Groq API")
|
| 21 |
|
| 22 |
-
#
|
| 23 |
col1, col2 = st.columns(2)
|
| 24 |
with col1:
|
| 25 |
left_speaker = st.text_input("Left Speaker", placeholder="Enter left speaker name")
|
| 26 |
with col2:
|
| 27 |
right_speaker = st.text_input("Right Speaker", placeholder="Enter right speaker name")
|
| 28 |
|
| 29 |
-
# Text area for overall conversation theme.
|
| 30 |
theme = st.text_area("Overall Theme of the Conversation", height=100)
|
| 31 |
-
|
| 32 |
-
# Optional additional details.
|
| 33 |
additional_details = st.text_area("Additional Conversation Details (Optional)", height=100)
|
| 34 |
|
| 35 |
-
#
|
| 36 |
is_ready = bool(theme and left_speaker and right_speaker)
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
# Build the initial prompt.
|
| 42 |
-
prompt = f"""
|
| 43 |
π Objective
|
| 44 |
Generate a highly engaging and comedic iMessage chat script formatted for video generation.
|
| 45 |
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.
|
|
@@ -129,75 +133,54 @@ DO NOT USE THESE NAMES IN CONTACT OR IN TEXTS, THESE ARE ONLY TO TELL THE PROGRA
|
|
| 129 |
Left Actor = {left_speaker}
|
| 130 |
Right Actor = {right_speaker}
|
| 131 |
Theme = {theme}
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
label="Download Script as TXT",
|
| 167 |
-
data=txt_io,
|
| 168 |
-
file_name="conversation_script.txt",
|
| 169 |
-
mime="text/plain"
|
| 170 |
-
)
|
| 171 |
-
except Exception as e:
|
| 172 |
-
st.error(f"An error occurred while calling the API: {e}")
|
| 173 |
-
|
| 174 |
-
# ------------------------------------------------------------------
|
| 175 |
-
# Chat Interface Section
|
| 176 |
-
# This section will appear after the initial generation (or if history already exists)
|
| 177 |
st.markdown("---")
|
| 178 |
st.header("Chat & Modify Conversation Script")
|
| 179 |
|
| 180 |
-
# Display the chat history.
|
| 181 |
-
for msg in st.session_state.chat_history:
|
| 182 |
-
if msg["role"] == "user":
|
| 183 |
-
st.markdown(f"**User:** {msg['content']}")
|
| 184 |
-
else:
|
| 185 |
-
st.markdown(f"**Assistant:** {msg['content']}")
|
| 186 |
-
|
| 187 |
-
# Form to send a new chat message.
|
| 188 |
with st.form(key="chat_form", clear_on_submit=True):
|
| 189 |
user_message = st.text_area("Enter your message (e.g., request modifications)", height=100)
|
| 190 |
submit_chat = st.form_submit_button("Send")
|
| 191 |
-
|
| 192 |
-
# ... (previous code remains unchanged)
|
| 193 |
|
| 194 |
if submit_chat and user_message.strip():
|
| 195 |
-
# Append the
|
| 196 |
st.session_state.chat_history.append({"role": "user", "content": user_message})
|
| 197 |
-
|
| 198 |
-
# Call the
|
|
|
|
| 199 |
try:
|
| 200 |
-
model = "llama-3.3-70b-versatile"
|
| 201 |
chat_completion = client.chat.completions.create(
|
| 202 |
messages=st.session_state.chat_history,
|
| 203 |
model=model,
|
|
@@ -210,8 +193,27 @@ if submit_chat and user_message.strip():
|
|
| 210 |
except Exception as e:
|
| 211 |
st.error(f"An error occurred while calling the API: {e}")
|
| 212 |
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
# Initialize session state for chat history if it doesn't exist.
|
| 10 |
if "chat_history" not in st.session_state:
|
| 11 |
+
st.session_state.chat_history = [] # List of dicts: {"role": "user"/"assistant", "content": "..."}
|
| 12 |
+
|
| 13 |
+
def render_chat_history():
|
| 14 |
+
"""Render the current chat history in a dedicated container."""
|
| 15 |
+
chat_container = st.container()
|
| 16 |
+
# Build the chat history markdown string.
|
| 17 |
+
chat_md = ""
|
| 18 |
+
for msg in st.session_state.chat_history:
|
| 19 |
+
if msg["role"] == "user":
|
| 20 |
+
chat_md += f"**User:** {msg['content']}\n\n"
|
| 21 |
+
else:
|
| 22 |
+
chat_md += f"**Assistant:** {msg['content']}\n\n"
|
| 23 |
+
chat_container.markdown(chat_md)
|
| 24 |
+
|
| 25 |
+
# =====================
|
| 26 |
+
# Initial Conversation Generation
|
| 27 |
+
# =====================
|
| 28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
st.title("Conversation Script Generator Using Groq API")
|
| 30 |
|
| 31 |
+
# Input fields for initial generation.
|
| 32 |
col1, col2 = st.columns(2)
|
| 33 |
with col1:
|
| 34 |
left_speaker = st.text_input("Left Speaker", placeholder="Enter left speaker name")
|
| 35 |
with col2:
|
| 36 |
right_speaker = st.text_input("Right Speaker", placeholder="Enter right speaker name")
|
| 37 |
|
|
|
|
| 38 |
theme = st.text_area("Overall Theme of the Conversation", height=100)
|
|
|
|
|
|
|
| 39 |
additional_details = st.text_area("Additional Conversation Details (Optional)", height=100)
|
| 40 |
|
| 41 |
+
# Check if all required fields are filled.
|
| 42 |
is_ready = bool(theme and left_speaker and right_speaker)
|
| 43 |
|
| 44 |
+
if st.button("Generate Conversation", disabled=not is_ready):
|
| 45 |
+
# Build the initial prompt.
|
| 46 |
+
prompt = f"""
|
|
|
|
|
|
|
| 47 |
π Objective
|
| 48 |
Generate a highly engaging and comedic iMessage chat script formatted for video generation.
|
| 49 |
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.
|
|
|
|
| 133 |
Left Actor = {left_speaker}
|
| 134 |
Right Actor = {right_speaker}
|
| 135 |
Theme = {theme}
|
| 136 |
+
"""
|
| 137 |
+
if additional_details.strip():
|
| 138 |
+
prompt += f"\nAdditional Details: {additional_details}\n"
|
| 139 |
+
|
| 140 |
+
# Start the conversation history with the initial prompt.
|
| 141 |
+
st.session_state.chat_history = [{"role": "user", "content": prompt}]
|
| 142 |
+
|
| 143 |
+
model = "llama-3.3-70b-versatile"
|
| 144 |
+
try:
|
| 145 |
+
chat_completion = client.chat.completions.create(
|
| 146 |
+
messages=st.session_state.chat_history,
|
| 147 |
+
model=model,
|
| 148 |
+
temperature=1.2,
|
| 149 |
+
max_completion_tokens=21890,
|
| 150 |
+
)
|
| 151 |
+
result_text = chat_completion.choices[0].message.content
|
| 152 |
+
|
| 153 |
+
if not result_text:
|
| 154 |
+
st.error("The API call did not return any content.")
|
| 155 |
+
else:
|
| 156 |
+
st.success("Conversation generated successfully!")
|
| 157 |
+
# Append the generated conversation to chat history.
|
| 158 |
+
st.session_state.chat_history.append({"role": "assistant", "content": result_text})
|
| 159 |
+
except Exception as e:
|
| 160 |
+
st.error(f"An error occurred while calling the API: {e}")
|
| 161 |
+
|
| 162 |
+
# Render the current conversation.
|
| 163 |
+
st.markdown("### Generated Conversation Script")
|
| 164 |
+
render_chat_history()
|
| 165 |
+
|
| 166 |
+
# =====================
|
| 167 |
+
# Chat Interface for Modifications
|
| 168 |
+
# =====================
|
| 169 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 170 |
st.markdown("---")
|
| 171 |
st.header("Chat & Modify Conversation Script")
|
| 172 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 173 |
with st.form(key="chat_form", clear_on_submit=True):
|
| 174 |
user_message = st.text_area("Enter your message (e.g., request modifications)", height=100)
|
| 175 |
submit_chat = st.form_submit_button("Send")
|
|
|
|
|
|
|
| 176 |
|
| 177 |
if submit_chat and user_message.strip():
|
| 178 |
+
# Append the user's message to chat history.
|
| 179 |
st.session_state.chat_history.append({"role": "user", "content": user_message})
|
| 180 |
+
|
| 181 |
+
# Call the API with the updated conversation history.
|
| 182 |
+
model = "llama-3.3-70b-versatile"
|
| 183 |
try:
|
|
|
|
| 184 |
chat_completion = client.chat.completions.create(
|
| 185 |
messages=st.session_state.chat_history,
|
| 186 |
model=model,
|
|
|
|
| 193 |
except Exception as e:
|
| 194 |
st.error(f"An error occurred while calling the API: {e}")
|
| 195 |
|
| 196 |
+
# Render the updated conversation after processing the new message.
|
| 197 |
+
st.markdown("### Updated Conversation Script")
|
| 198 |
+
render_chat_history()
|
| 199 |
+
|
| 200 |
+
# -------------------------
|
| 201 |
+
# Download Option for the Latest Script (from the assistant response)
|
| 202 |
+
# -------------------------
|
| 203 |
+
if st.session_state.chat_history:
|
| 204 |
+
# Extract the last assistant response.
|
| 205 |
+
last_response = ""
|
| 206 |
+
for msg in reversed(st.session_state.chat_history):
|
| 207 |
+
if msg["role"] == "assistant":
|
| 208 |
+
last_response = msg["content"]
|
| 209 |
+
break
|
| 210 |
+
|
| 211 |
+
if last_response:
|
| 212 |
+
txt_bytes = last_response.encode("utf-8")
|
| 213 |
+
txt_io = io.BytesIO(txt_bytes)
|
| 214 |
+
st.download_button(
|
| 215 |
+
label="Download Latest Assistant Script as TXT",
|
| 216 |
+
data=txt_io,
|
| 217 |
+
file_name="conversation_script.txt",
|
| 218 |
+
mime="text/plain"
|
| 219 |
+
)
|