udayl commited on
Commit
cea6d8c
·
1 Parent(s): 61caafb

fix: using /tmp instead of /app

Browse files
Files changed (1) hide show
  1. gradio_app.py +19 -37
gradio_app.py CHANGED
@@ -89,70 +89,52 @@ def generate_audio_from_script_with_voices(script, speaker1_voice, speaker2_voic
89
  def process_pdf(pdf_file, speaker1_voice, speaker2_voice, provider, api_key, openrouter_base=None):
90
  """Process the uploaded PDF file and generate audio"""
91
  try:
92
-
93
  # Set API configuration based on provider
 
94
  if provider == "openai":
95
- os.environ["OPENAI_API_KEY"] = api_key
96
  os.environ["OPENROUTER_API_BASE"] = "https://api.openai.com/v1"
97
  else:
98
- os.environ["OPENAI_API_KEY"] = api_key
99
  os.environ["OPENROUTER_API_BASE"] = openrouter_base or "https://openrouter.ai/api/v1"
100
- # Check if we received a valid file
101
  if pdf_file is None:
102
  return "No file uploaded", None
103
-
104
- # Create a temporary file with .pdf extension
105
- with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp:
106
- # For Gradio uploads, we need to copy the file
107
- shutil.copy2(pdf_file.name, tmp.name)
108
- tmp_path = tmp.name
109
-
110
- print(f"Uploaded PDF saved at {tmp_path}")
111
 
112
- # Generate transcript using your existing function
 
 
 
 
 
 
 
 
 
113
  transcript, transcript_path = generate_podcast_script(tmp_path, provider=provider)
114
  if transcript is None:
115
  return "Error generating transcript", None
116
 
117
- # Define an output file path for the generated audio
118
  audio_output_path = os.path.join(
119
- os.path.dirname(tmp_path),
120
- f"audio_{os.path.basename(tmp_path).replace('.pdf', '.wav')}"
121
  )
122
-
123
- # result = generate_audio_from_script_with_voices(
124
- # transcript,
125
- # speaker1_voice,
126
- # speaker2_voice,
127
- # output_file=audio_output_path
128
- # )
129
 
130
- # Use ProcessPoolExecutor with explicit number of workers
131
  with concurrent.futures.ProcessPoolExecutor(max_workers=NUM_WORKERS) as executor:
132
- print(f"Processing with {NUM_WORKERS} CPU cores")
133
- # Submit audio generation task to the executor
134
  future = executor.submit(
135
  generate_audio_from_script_with_voices,
136
  transcript, speaker1_voice, speaker2_voice, audio_output_path
137
  )
138
  result = future.result()
139
-
140
- if result is None:
141
- return "Error generating audio", None
142
-
143
- return "Process complete!", result
144
 
145
- except Exception as e:
146
- print(f"Error in process_pdf: {str(e)}")
147
- return f"Error processing file: {str(e)}", None
148
-
149
  if result is None:
150
  return "Error generating audio", None
151
-
152
  return "Process complete!", result
153
 
154
  except Exception as e:
155
- print(f"Error in process_pdf: {str(e)}")
156
  return f"Error processing file: {str(e)}", None
157
 
158
 
 
89
  def process_pdf(pdf_file, speaker1_voice, speaker2_voice, provider, api_key, openrouter_base=None):
90
  """Process the uploaded PDF file and generate audio"""
91
  try:
 
92
  # Set API configuration based on provider
93
+ os.environ["OPENAI_API_KEY"] = api_key
94
  if provider == "openai":
 
95
  os.environ["OPENROUTER_API_BASE"] = "https://api.openai.com/v1"
96
  else:
 
97
  os.environ["OPENROUTER_API_BASE"] = openrouter_base or "https://openrouter.ai/api/v1"
98
+
99
  if pdf_file is None:
100
  return "No file uploaded", None
 
 
 
 
 
 
 
 
101
 
102
+ # Use /tmp or current directory for temp file creation
103
+ base_dir = "/tmp" if os.path.exists("/tmp") else os.getcwd()
104
+
105
+ # Create a temp copy of the uploaded PDF
106
+ pdf_filename = os.path.basename(pdf_file.name)
107
+ tmp_path = os.path.join(base_dir, f"uploaded_{pdf_filename}")
108
+ shutil.copy2(pdf_file.name, tmp_path)
109
+ print(f"[INFO] Uploaded PDF saved at {tmp_path}")
110
+
111
+ # Generate the podcast script
112
  transcript, transcript_path = generate_podcast_script(tmp_path, provider=provider)
113
  if transcript is None:
114
  return "Error generating transcript", None
115
 
116
+ # Define audio output path in the same temp dir
117
  audio_output_path = os.path.join(
118
+ base_dir,
119
+ f"audio_{os.path.splitext(pdf_filename)[0]}.wav"
120
  )
 
 
 
 
 
 
 
121
 
122
+ # Process audio generation using parallel CPU workers
123
  with concurrent.futures.ProcessPoolExecutor(max_workers=NUM_WORKERS) as executor:
124
+ print(f"[INFO] Processing with {NUM_WORKERS} CPU cores")
 
125
  future = executor.submit(
126
  generate_audio_from_script_with_voices,
127
  transcript, speaker1_voice, speaker2_voice, audio_output_path
128
  )
129
  result = future.result()
 
 
 
 
 
130
 
 
 
 
 
131
  if result is None:
132
  return "Error generating audio", None
133
+
134
  return "Process complete!", result
135
 
136
  except Exception as e:
137
+ print(f"[ERROR] process_pdf failed: {str(e)}")
138
  return f"Error processing file: {str(e)}", None
139
 
140