Spaces:
Running
Running
| 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", | |
| "Josh": "A male voice", | |
| "Arnold": "A male voice", | |
| "Matilda": "A female voice", | |
| "Brian": "A narration voice", | |
| "Alice": "A news voice", | |
| "Bill": "A narration voice", | |
| "Callum": "A character voice", | |
| "Charlie": "A conversational voice", | |
| "Charlotte": "A character voice", | |
| "Chris": "A conversational voice", | |
| "Daniel": "A news voice", | |
| "Eric": "A voice", | |
| "George": "A narration voice", | |
| "Jessica": "A voice", | |
| "Laura": "A voice", | |
| "Lily": "A narration voice", | |
| "Sarah": "A news voice", | |
| "Will": "A 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}") | |