Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -38,43 +38,38 @@ def generate_podcast_script(api_key, prompt, uploaded_file, duration, num_hosts)
|
|
38 |
try:
|
39 |
genai.configure(api_key=api_key)
|
40 |
model = genai.GenerativeModel('gemini-2.5-pro-preview-03-25')
|
41 |
-
|
42 |
combined_content = prompt or ""
|
43 |
-
|
44 |
-
if uploaded_file is not None:
|
45 |
-
file_bytes = io.BytesIO(uploaded_file)
|
46 |
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
#
|
51 |
-
file_bytes.seek(0)
|
52 |
-
pdf_reader = PyPDF2.PdfReader(file_bytes)
|
53 |
-
file_content = "\n".join([page.extract_text() for page in pdf_reader.pages])
|
54 |
-
else:
|
55 |
-
# Try as text file first
|
56 |
file_bytes.seek(0)
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
|
|
|
|
|
|
61 |
file_bytes.seek(0)
|
62 |
try:
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
combined_content += "\n" + file_content if combined_content else file_content
|
74 |
|
75 |
num_hosts = int(num_hosts) # Convert to integer
|
76 |
|
77 |
-
|
78 |
Create a podcast script for {num_hosts} {'person' if num_hosts == 1 else 'people'} discussing:
|
79 |
{combined_content}
|
80 |
|
@@ -91,17 +86,17 @@ def generate_podcast_script(api_key, prompt, uploaded_file, duration, num_hosts)
|
|
91 |
The intro always includes the first speaker and should be in the same paragraph.
|
92 |
The outro always includes the first speaker and should be in the same paragraph
|
93 |
Do not include these types of transition "Intro Music fades in and then fades slightly to background"
|
94 |
-
Keep each speaker's entire monologue in a single paragraph, regardless of length if the
|
95 |
-
Start a new paragraph only when switching to a different speaker if the
|
96 |
Maintain natural conversation flow and speech patterns within each monologue.
|
97 |
-
Use context clues or subtle references to indicate who is speaking without explicit labels if the
|
98 |
Use speaker names sparingly, only when necessary for clarity or emphasis. Avoid starting every line with the other person's name.
|
99 |
Rely more on context and speech patterns to indicate who is speaking, rather than always stating names.
|
100 |
Use names primarily for transitions sparingly, definitely with agreements, or to draw attention to a specific point, not as a constant form of address.
|
101 |
{'Make sure the script is a monologue for one person.' if num_hosts == 1 else 'Ensure the dialogue alternates between two distinct voices, with one speaking on odd-numbered lines and the other on even-numbered lines.'}
|
102 |
"""
|
103 |
|
104 |
-
response = model.generate_content(
|
105 |
return re.sub(r'[^a-zA-Z0-9\s.,?!<>]', '', response.text)
|
106 |
except Exception as e:
|
107 |
logger.error(f"Error generating podcast script: {str(e)}")
|
|
|
38 |
try:
|
39 |
genai.configure(api_key=api_key)
|
40 |
model = genai.GenerativeModel('gemini-2.5-pro-preview-03-25')
|
41 |
+
|
42 |
combined_content = prompt or ""
|
|
|
|
|
|
|
43 |
|
44 |
+
if uploaded_file is not None:
|
45 |
+
file_bytes = io.BytesIO(uploaded_file)
|
46 |
+
|
47 |
+
# Try to detect the file type based on content
|
|
|
|
|
|
|
|
|
|
|
48 |
file_bytes.seek(0)
|
49 |
+
if file_bytes.read(4) == b'%PDF':
|
50 |
+
# It's a PDF file
|
51 |
+
file_bytes.seek(0)
|
52 |
+
pdf_reader = PyPDF2.PdfReader(file_bytes)
|
53 |
+
file_content = "\n".join([page.extract_text() for page in pdf_reader.pages])
|
54 |
+
else:
|
55 |
+
# Try as text file first
|
56 |
file_bytes.seek(0)
|
57 |
try:
|
58 |
+
file_content = file_bytes.read().decode('utf-8')
|
59 |
+
except UnicodeDecodeError:
|
60 |
+
# If it's not a text file, try as a docx
|
61 |
+
file_bytes.seek(0)
|
62 |
+
try:
|
63 |
+
doc = Document(file_bytes)
|
64 |
+
file_content = "\n".join([para.text for para in doc.paragraphs])
|
65 |
+
except:
|
66 |
+
raise ValueError("Unsupported file type or corrupted file")
|
67 |
+
|
68 |
combined_content += "\n" + file_content if combined_content else file_content
|
69 |
|
70 |
num_hosts = int(num_hosts) # Convert to integer
|
71 |
|
72 |
+
prompt_template = f"""
|
73 |
Create a podcast script for {num_hosts} {'person' if num_hosts == 1 else 'people'} discussing:
|
74 |
{combined_content}
|
75 |
|
|
|
86 |
The intro always includes the first speaker and should be in the same paragraph.
|
87 |
The outro always includes the first speaker and should be in the same paragraph
|
88 |
Do not include these types of transition "Intro Music fades in and then fades slightly to background"
|
89 |
+
Keep each speaker's entire monologue in a single paragraph, regardless of length if the number of hosts is not 1.
|
90 |
+
Start a new paragraph only when switching to a different speaker if the number of hosts is not 1.
|
91 |
Maintain natural conversation flow and speech patterns within each monologue.
|
92 |
+
Use context clues or subtle references to indicate who is speaking without explicit labels if the number of hosts is not 1
|
93 |
Use speaker names sparingly, only when necessary for clarity or emphasis. Avoid starting every line with the other person's name.
|
94 |
Rely more on context and speech patterns to indicate who is speaking, rather than always stating names.
|
95 |
Use names primarily for transitions sparingly, definitely with agreements, or to draw attention to a specific point, not as a constant form of address.
|
96 |
{'Make sure the script is a monologue for one person.' if num_hosts == 1 else 'Ensure the dialogue alternates between two distinct voices, with one speaking on odd-numbered lines and the other on even-numbered lines.'}
|
97 |
"""
|
98 |
|
99 |
+
response = model.generate_content(prompt_template)
|
100 |
return re.sub(r'[^a-zA-Z0-9\s.,?!<>]', '', response.text)
|
101 |
except Exception as e:
|
102 |
logger.error(f"Error generating podcast script: {str(e)}")
|