File size: 3,512 Bytes
4b91275
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
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"Create a conversation script between {left_speaker} and {right_speaker}.\n"
        f"Theme: {theme}\n"
    )
    if additional_details.strip():
        prompt += f"Additional Details: {additional_details}\n"

    # Display the generated prompt for debugging.
    st.write("### Generated Prompt")
    st.code(prompt)

    # 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}")