Script-Gen / app.py
abdulllah01's picture
Create app.py
5b0ea37 verified
raw
history blame
2.87 kB
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"))
# Define the speaker options and their descriptions.
speaker_options = {
"Liam": "A male voice",
"Dorothy": "A female voice",
}
# Build the Streamlit UI.
st.title("Conversation Script Generator Using Groq API")
# Layout two columns for speaker selection.
col1, col2 = st.columns(2)
with col1:
left_speaker = st.selectbox(
"Left Speaker",
options=list(speaker_options.keys()),
format_func=lambda x: f"{x}: {speaker_options[x]}"
)
with col2:
right_speaker = st.selectbox(
"Right Speaker",
options=list(speaker_options.keys()),
format_func=lambda x: f"{x}: {speaker_options[x]}"
)
# 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)
# When the user clicks the button, build the prompt and make the API call.
if st.button("Generate Conversation"):
# Build the prompt message for Groq API.
prompt = (
f"""
{os.environ.get("PROMPT")}
Left Actor={left_speaker}
Right Actor={right_speaker}
Theme={theme}
"""
)
if additional_details.strip():
prompt += f"Additional Details: {additional_details}\n"
# Prepare the message payload for the Groq API call.
messages = [
{
"role": "user",
"content": prompt,
}
]
# Model selection: adjust as needed.
model = "llama-3.3-70b-versatile"
try:
# Make the chat completion call using Groq.
chat_completion = client.chat.completions.create(
messages=messages,
model=model,
)
# Extract the generated conversation from the API response.
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!")
# Display the result inside a text area.
st.write("### Generated Conversation Script")
st.text_area("", result_text, height=300)
# Create a downloadable TXT file.
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}")