Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -13,7 +13,6 @@ if "chat_history" not in st.session_state:
|
|
| 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":
|
|
@@ -22,24 +21,24 @@ def render_chat_history():
|
|
| 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 |
-
#
|
| 32 |
-
|
|
|
|
|
|
|
| 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.
|
|
@@ -113,7 +112,7 @@ Every script must begin with an insane, wacky brain rot hook that immediately gr
|
|
| 113 |
"SIR WHY IS MY LAWYER ALSO MY UBER DRIVER?!" 👨⚖🚗💨
|
| 114 |
Must be over-the-top and chaotic.
|
| 115 |
Encourages immediate engagement.
|
| 116 |
-
7
|
| 117 |
The story must escalate into chaos but stay coherent:
|
| 118 |
Start Crazy → Get Crazier → End With a Punchline or Twist
|
| 119 |
Use sudden twists and reveals to maintain engagement.
|
|
@@ -132,14 +131,15 @@ The final line should be either a funny resolution or an abrupt exit.
|
|
| 132 |
DO NOT USE THESE NAMES IN CONTACT OR IN TEXTS, THESE ARE ONLY TO TELL THE PROGRAM WHICH VOICE TO USE
|
| 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(
|
|
@@ -149,36 +149,46 @@ DO NOT USE THESE NAMES IN CONTACT OR IN TEXTS, THESE ARE ONLY TO TELL THE PROGRA
|
|
| 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
|
| 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
|
| 175 |
submit_chat = st.form_submit_button("Send")
|
| 176 |
|
| 177 |
if submit_chat and user_message.strip():
|
| 178 |
-
#
|
| 179 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 180 |
|
| 181 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 182 |
model = "llama-3.3-70b-versatile"
|
| 183 |
try:
|
| 184 |
chat_completion = client.chat.completions.create(
|
|
@@ -193,21 +203,18 @@ if submit_chat and user_message.strip():
|
|
| 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
|
| 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)
|
|
|
|
| 13 |
def render_chat_history():
|
| 14 |
"""Render the current chat history in a dedicated container."""
|
| 15 |
chat_container = st.container()
|
|
|
|
| 16 |
chat_md = ""
|
| 17 |
for msg in st.session_state.chat_history:
|
| 18 |
if msg["role"] == "user":
|
|
|
|
| 21 |
chat_md += f"**Assistant:** {msg['content']}\n\n"
|
| 22 |
chat_container.markdown(chat_md)
|
| 23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
st.title("Conversation Script Generator Using Groq API")
|
| 25 |
|
| 26 |
+
# ---------------------------
|
| 27 |
+
# Initial Conversation Generation Inputs
|
| 28 |
+
# ---------------------------
|
| 29 |
+
col1, col2, col3 = st.columns(3)
|
| 30 |
with col1:
|
| 31 |
left_speaker = st.text_input("Left Speaker", placeholder="Enter left speaker name")
|
| 32 |
with col2:
|
| 33 |
right_speaker = st.text_input("Right Speaker", placeholder="Enter right speaker name")
|
| 34 |
+
with col3:
|
| 35 |
+
contact_name = st.text_input("Contact Name", placeholder="Enter contact name for conversation")
|
| 36 |
|
| 37 |
theme = st.text_area("Overall Theme of the Conversation", height=100)
|
| 38 |
additional_details = st.text_area("Additional Conversation Details (Optional)", height=100)
|
| 39 |
|
| 40 |
# Check if all required fields are filled.
|
| 41 |
+
is_ready = bool(theme and left_speaker and right_speaker and contact_name)
|
| 42 |
|
| 43 |
if st.button("Generate Conversation", disabled=not is_ready):
|
| 44 |
# Build the initial prompt.
|
|
|
|
| 112 |
"SIR WHY IS MY LAWYER ALSO MY UBER DRIVER?!" 👨⚖🚗💨
|
| 113 |
Must be over-the-top and chaotic.
|
| 114 |
Encourages immediate engagement.
|
| 115 |
+
7️⃣ Escalation & Progression
|
| 116 |
The story must escalate into chaos but stay coherent:
|
| 117 |
Start Crazy → Get Crazier → End With a Punchline or Twist
|
| 118 |
Use sudden twists and reveals to maintain engagement.
|
|
|
|
| 131 |
DO NOT USE THESE NAMES IN CONTACT OR IN TEXTS, THESE ARE ONLY TO TELL THE PROGRAM WHICH VOICE TO USE
|
| 132 |
Left Actor = {left_speaker}
|
| 133 |
Right Actor = {right_speaker}
|
| 134 |
+
Contact Name = {contact_name}
|
| 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(
|
|
|
|
| 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 |
st.session_state.chat_history.append({"role": "assistant", "content": result_text})
|
| 158 |
except Exception as e:
|
| 159 |
st.error(f"An error occurred while calling the API: {e}")
|
| 160 |
|
|
|
|
| 161 |
st.markdown("### Generated Conversation Script")
|
| 162 |
render_chat_history()
|
| 163 |
|
| 164 |
+
# ---------------------------
|
| 165 |
+
# Chat Interface for Revisions
|
| 166 |
+
# ---------------------------
|
|
|
|
| 167 |
st.markdown("---")
|
| 168 |
st.header("Chat & Modify Conversation Script")
|
| 169 |
|
| 170 |
with st.form(key="chat_form", clear_on_submit=True):
|
| 171 |
+
user_message = st.text_area("Enter your revision request (e.g., modify the script)", height=100)
|
| 172 |
submit_chat = st.form_submit_button("Send")
|
| 173 |
|
| 174 |
if submit_chat and user_message.strip():
|
| 175 |
+
# Retrieve the last assistant-generated script for context.
|
| 176 |
+
last_script = ""
|
| 177 |
+
for msg in reversed(st.session_state.chat_history):
|
| 178 |
+
if msg["role"] == "assistant":
|
| 179 |
+
last_script = msg["content"]
|
| 180 |
+
break
|
| 181 |
+
# Build a default revision prompt that includes context and instructions.
|
| 182 |
+
revision_prompt = f"""Revision Request: {user_message}
|
| 183 |
+
|
| 184 |
+
Context: The current conversation script is as follows:
|
| 185 |
|
| 186 |
+
{last_script}
|
| 187 |
+
|
| 188 |
+
Please revise the script to incorporate the above requested changes. Do not simply repeat the same content—update it with new, modified details that reflect the user's instructions.
|
| 189 |
+
"""
|
| 190 |
+
st.session_state.chat_history.append({"role": "user", "content": revision_prompt})
|
| 191 |
+
|
| 192 |
model = "llama-3.3-70b-versatile"
|
| 193 |
try:
|
| 194 |
chat_completion = client.chat.completions.create(
|
|
|
|
| 203 |
except Exception as e:
|
| 204 |
st.error(f"An error occurred while calling the API: {e}")
|
| 205 |
|
|
|
|
| 206 |
st.markdown("### Updated Conversation Script")
|
| 207 |
render_chat_history()
|
| 208 |
|
| 209 |
+
# ---------------------------
|
| 210 |
+
# Download Option for the Latest Assistant Script
|
| 211 |
+
# ---------------------------
|
| 212 |
if st.session_state.chat_history:
|
|
|
|
| 213 |
last_response = ""
|
| 214 |
for msg in reversed(st.session_state.chat_history):
|
| 215 |
if msg["role"] == "assistant":
|
| 216 |
last_response = msg["content"]
|
| 217 |
break
|
|
|
|
| 218 |
if last_response:
|
| 219 |
txt_bytes = last_response.encode("utf-8")
|
| 220 |
txt_io = io.BytesIO(txt_bytes)
|