abdulllah01 commited on
Commit
4b91275
·
verified ·
1 Parent(s): 03532cb

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +112 -0
app.py ADDED
@@ -0,0 +1,112 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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"Create a conversation script between {left_speaker} and {right_speaker}.\n"
62
+ f"Theme: {theme}\n"
63
+ )
64
+ if additional_details.strip():
65
+ prompt += f"Additional Details: {additional_details}\n"
66
+
67
+ # Display the generated prompt for debugging.
68
+ st.write("### Generated Prompt")
69
+ st.code(prompt)
70
+
71
+ # Prepare the message payload for the Groq API call.
72
+ messages = [
73
+ {
74
+ "role": "user",
75
+ "content": prompt,
76
+ }
77
+ ]
78
+
79
+ # Model selection: adjust as needed.
80
+ model = "llama-3.3-70b-versatile"
81
+
82
+ try:
83
+ # Make the chat completion call using Groq.
84
+ chat_completion = client.chat.completions.create(
85
+ messages=messages,
86
+ model=model,
87
+ )
88
+
89
+ # Extract the generated conversation from the API response.
90
+ result_text = chat_completion.choices[0].message.content
91
+
92
+ if not result_text:
93
+ st.error("The API call did not return any content.")
94
+ else:
95
+ st.success("Conversation generated successfully!")
96
+
97
+ # Display the result inside a text area.
98
+ st.write("### Generated Conversation Script")
99
+ st.text_area("", result_text, height=300)
100
+
101
+ # Create a downloadable TXT file.
102
+ txt_bytes = result_text.encode("utf-8")
103
+ txt_io = io.BytesIO(txt_bytes)
104
+ st.download_button(
105
+ label="Download Script as TXT",
106
+ data=txt_io,
107
+ file_name="conversation_script.txt",
108
+ mime="text/plain"
109
+ )
110
+
111
+ except Exception as e:
112
+ st.error(f"An error occurred while calling the API: {e}")