Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import io
|
| 3 |
+
import streamlit as st
|
| 4 |
+
from groq import Groq
|
| 5 |
+
|
| 6 |
+
# Set up Groq client using the environment variable for API key.
|
| 7 |
+
client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
|
| 8 |
+
|
| 9 |
+
# Define the speaker options and their descriptions.
|
| 10 |
+
speaker_options = {
|
| 11 |
+
"Liam": "A male voice",
|
| 12 |
+
"Dorothy": "A female voice",
|
| 13 |
+
"Josh": "A male voice",
|
| 14 |
+
"Arnold": "A male voice",
|
| 15 |
+
"Matilda": "A female voice",
|
| 16 |
+
"Brian": "A narration voice",
|
| 17 |
+
"Alice": "A news voice",
|
| 18 |
+
"Bill": "A narration voice",
|
| 19 |
+
"Callum": "A character voice",
|
| 20 |
+
"Charlie": "A conversational voice",
|
| 21 |
+
"Charlotte": "A character voice",
|
| 22 |
+
"Chris": "A conversational voice",
|
| 23 |
+
"Daniel": "A news voice",
|
| 24 |
+
"Eric": "A voice",
|
| 25 |
+
"George": "A narration voice",
|
| 26 |
+
"Jessica": "A voice",
|
| 27 |
+
"Laura": "A voice",
|
| 28 |
+
"Lily": "A narration voice",
|
| 29 |
+
"Sarah": "A news voice",
|
| 30 |
+
"Will": "A voice"
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
# Build the Streamlit UI.
|
| 34 |
+
st.title("Conversation Script Generator Using Groq API")
|
| 35 |
+
|
| 36 |
+
# Layout two columns for speaker selection.
|
| 37 |
+
col1, col2 = st.columns(2)
|
| 38 |
+
with col1:
|
| 39 |
+
left_speaker = st.selectbox(
|
| 40 |
+
"Left Speaker",
|
| 41 |
+
options=list(speaker_options.keys()),
|
| 42 |
+
format_func=lambda x: f"{x}: {speaker_options[x]}"
|
| 43 |
+
)
|
| 44 |
+
with col2:
|
| 45 |
+
right_speaker = st.selectbox(
|
| 46 |
+
"Right Speaker",
|
| 47 |
+
options=list(speaker_options.keys()),
|
| 48 |
+
format_func=lambda x: f"{x}: {speaker_options[x]}"
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
# Text area for overall conversation theme.
|
| 52 |
+
theme = st.text_area("Overall Theme of the Conversation", height=100)
|
| 53 |
+
|
| 54 |
+
# Optional additional details.
|
| 55 |
+
additional_details = st.text_area("Additional Conversation Details (Optional)", height=100)
|
| 56 |
+
|
| 57 |
+
# When the user clicks the button, build the prompt and make the API call.
|
| 58 |
+
if st.button("Generate Conversation"):
|
| 59 |
+
# Build the prompt message for Groq API.
|
| 60 |
+
prompt = (
|
| 61 |
+
f"""
|
| 62 |
+
{os.environ.get("PROMPT")}
|
| 63 |
+
Left Actor={left_speaker}
|
| 64 |
+
Right Actor={right_speaker}
|
| 65 |
+
Theme={theme}
|
| 66 |
+
"""
|
| 67 |
+
)
|
| 68 |
+
if additional_details.strip():
|
| 69 |
+
prompt += f"Additional Details: {additional_details}\n"
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
# Prepare the message payload for the Groq API call.
|
| 74 |
+
messages = [
|
| 75 |
+
{
|
| 76 |
+
"role": "user",
|
| 77 |
+
"content": prompt,
|
| 78 |
+
}
|
| 79 |
+
]
|
| 80 |
+
|
| 81 |
+
# Model selection: adjust as needed.
|
| 82 |
+
model = "llama-3.3-70b-versatile"
|
| 83 |
+
|
| 84 |
+
try:
|
| 85 |
+
# Make the chat completion call using Groq.
|
| 86 |
+
chat_completion = client.chat.completions.create(
|
| 87 |
+
messages=messages,
|
| 88 |
+
model=model,
|
| 89 |
+
)
|
| 90 |
+
|
| 91 |
+
# Extract the generated conversation from the API response.
|
| 92 |
+
result_text = chat_completion.choices[0].message.content
|
| 93 |
+
|
| 94 |
+
if not result_text:
|
| 95 |
+
st.error("The API call did not return any content.")
|
| 96 |
+
else:
|
| 97 |
+
st.success("Conversation generated successfully!")
|
| 98 |
+
|
| 99 |
+
# Display the result inside a text area.
|
| 100 |
+
st.write("### Generated Conversation Script")
|
| 101 |
+
st.text_area("", result_text, height=300)
|
| 102 |
+
|
| 103 |
+
# Create a downloadable TXT file.
|
| 104 |
+
txt_bytes = result_text.encode("utf-8")
|
| 105 |
+
txt_io = io.BytesIO(txt_bytes)
|
| 106 |
+
st.download_button(
|
| 107 |
+
label="Download Script as TXT",
|
| 108 |
+
data=txt_io,
|
| 109 |
+
file_name="conversation_script.txt",
|
| 110 |
+
mime="text/plain"
|
| 111 |
+
)
|
| 112 |
+
|
| 113 |
+
except Exception as e:
|
| 114 |
+
st.error(f"An error occurred while calling the API: {e}")
|