Spaces:
Running
Running
add file uploader, errors and refresh
Browse files
app.py
CHANGED
@@ -8,13 +8,13 @@ import gradio as gr
|
|
8 |
import pandas as pd
|
9 |
from dotenv import load_dotenv
|
10 |
from openai import OpenAI
|
11 |
-
|
12 |
|
13 |
load_dotenv()
|
14 |
|
15 |
|
16 |
api_key = os.getenv("AIML_API_KEY")
|
17 |
-
|
18 |
|
19 |
CHARACTER_CLASSIFICATION_PROMPT = """
|
20 |
**Task:**
|
@@ -200,17 +200,50 @@ class AudiobookBuilder:
|
|
200 |
return response
|
201 |
|
202 |
|
203 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
204 |
builder = AudiobookBuilder()
|
205 |
-
|
206 |
annotated_text = builder.annotate_text(text)
|
207 |
unique_characters = builder.get_unique_characters(annotated_text)
|
208 |
character_to_gender = builder.classify_characters(text, unique_characters)
|
209 |
character_to_voice = builder.map_characters_to_voices(character_to_gender)
|
210 |
builder.generate_audio(annotated_text, character_to_voice)
|
211 |
-
|
212 |
audio, sr = librosa.load("audiobook.mp3", sr=None)
|
213 |
-
return (sr, audio)
|
|
|
|
|
|
|
|
|
214 |
|
215 |
|
216 |
with gr.Blocks(title="Audiobooks Generation") as ui:
|
@@ -218,16 +251,46 @@ with gr.Blocks(title="Audiobooks Generation") as ui:
|
|
218 |
|
219 |
with gr.Row(variant="panel"):
|
220 |
text_input = gr.Textbox(label="Enter the book text", lines=20)
|
|
|
|
|
221 |
|
222 |
with gr.Row(variant="panel"):
|
223 |
-
audio_output = gr.Audio(label="Generated audio")
|
|
|
224 |
|
225 |
submit_button = gr.Button("Submit")
|
226 |
submit_button.click(
|
227 |
fn=respond,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
228 |
inputs=[text_input],
|
229 |
-
outputs=
|
230 |
)
|
231 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
232 |
|
233 |
ui.launch()
|
|
|
|
8 |
import pandas as pd
|
9 |
from dotenv import load_dotenv
|
10 |
from openai import OpenAI
|
11 |
+
from langchain_community.document_loaders import PyPDFLoader
|
12 |
|
13 |
load_dotenv()
|
14 |
|
15 |
|
16 |
api_key = os.getenv("AIML_API_KEY")
|
17 |
+
FILE_SIZE_MAX = 0.5 #in mb
|
18 |
|
19 |
CHARACTER_CLASSIFICATION_PROMPT = """
|
20 |
**Task:**
|
|
|
200 |
return response
|
201 |
|
202 |
|
203 |
+
def parse_pdf(file_path):
|
204 |
+
"""Parse the PDF file and return the text content."""
|
205 |
+
loader = PyPDFLoader(file_path)
|
206 |
+
documents = loader.load()
|
207 |
+
return "\n".join([doc.page_content for doc in documents])
|
208 |
+
|
209 |
+
|
210 |
+
def respond(text, uploaded_file):
|
211 |
+
# Check if a file is uploaded
|
212 |
+
if uploaded_file is not None:
|
213 |
+
# Save the uploaded file temporarily to check its size
|
214 |
+
temp_file_path = uploaded_file.name
|
215 |
+
|
216 |
+
# Check the file size
|
217 |
+
if os.path.getsize(temp_file_path) > FILE_SIZE_MAX * 1024 * 1024: # Check if file size is greater than 0.5 MB
|
218 |
+
error_message = f"Error: The uploaded file exceeds the size limit of {FILE_SIZE_MAX} MB."
|
219 |
+
return None, error_message # Return None for audio output and the error message
|
220 |
+
|
221 |
+
# Determine file type
|
222 |
+
if uploaded_file.name.endswith('.txt'):
|
223 |
+
# Read the text from the uploaded .txt file
|
224 |
+
with open(temp_file_path, 'r', encoding='utf-8') as file:
|
225 |
+
text = file.read()
|
226 |
+
elif uploaded_file.name.endswith('.pdf'):
|
227 |
+
# Parse the PDF file and extract text
|
228 |
+
text = parse_pdf(temp_file_path)
|
229 |
+
else:
|
230 |
+
error_message = "Error: Unsupported file type. Please upload a .txt or .pdf file."
|
231 |
+
return None, error_message
|
232 |
+
|
233 |
+
# Proceed with the audiobook generation
|
234 |
builder = AudiobookBuilder()
|
|
|
235 |
annotated_text = builder.annotate_text(text)
|
236 |
unique_characters = builder.get_unique_characters(annotated_text)
|
237 |
character_to_gender = builder.classify_characters(text, unique_characters)
|
238 |
character_to_voice = builder.map_characters_to_voices(character_to_gender)
|
239 |
builder.generate_audio(annotated_text, character_to_voice)
|
240 |
+
|
241 |
audio, sr = librosa.load("audiobook.mp3", sr=None)
|
242 |
+
return (sr, audio), None # Return audio and None for error message
|
243 |
+
|
244 |
+
|
245 |
+
def refresh():
|
246 |
+
return None, None, None # Reset audio output, error message, and uploaded file
|
247 |
|
248 |
|
249 |
with gr.Blocks(title="Audiobooks Generation") as ui:
|
|
|
251 |
|
252 |
with gr.Row(variant="panel"):
|
253 |
text_input = gr.Textbox(label="Enter the book text", lines=20)
|
254 |
+
# Add a file upload field for .txt and .pdf files
|
255 |
+
file_input = gr.File(label="Upload a text file or PDF", file_types=['.txt', '.pdf'])
|
256 |
|
257 |
with gr.Row(variant="panel"):
|
258 |
+
audio_output = gr.Audio(label="Generated audio", type="numpy")
|
259 |
+
error_output = gr.Textbox(label="Error Messages", interactive=False, visible=False) # Initially hidden
|
260 |
|
261 |
submit_button = gr.Button("Submit")
|
262 |
submit_button.click(
|
263 |
fn=respond,
|
264 |
+
inputs=[text_input, file_input], # Include the uploaded file as an input
|
265 |
+
outputs=[audio_output, error_output], # Include the audio output and error message output
|
266 |
+
)
|
267 |
+
|
268 |
+
refresh_button = gr.Button("Refresh")
|
269 |
+
refresh_button.click(
|
270 |
+
fn=refresh,
|
271 |
+
inputs=[],
|
272 |
+
outputs=[audio_output, error_output, file_input] # Reset audio output, error message, and uploaded file
|
273 |
+
)
|
274 |
+
|
275 |
+
# Hide error message dynamically when input is received
|
276 |
+
text_input.change(
|
277 |
+
fn=lambda: gr.update(visible=False), # Hide the error field
|
278 |
inputs=[text_input],
|
279 |
+
outputs=error_output
|
280 |
)
|
281 |
|
282 |
+
file_input.change(
|
283 |
+
fn=lambda: gr.update(visible=False), # Hide the error field
|
284 |
+
inputs=[file_input],
|
285 |
+
outputs=error_output
|
286 |
+
)
|
287 |
+
|
288 |
+
# To clear error field when refreshing
|
289 |
+
refresh_button.click(
|
290 |
+
fn=lambda: gr.update(visible=False), # Hide the error field
|
291 |
+
inputs=[],
|
292 |
+
outputs=error_output,
|
293 |
+
)
|
294 |
|
295 |
ui.launch()
|
296 |
+
|