Spaces:
Sleeping
Sleeping
janashraff commited on
Commit ·
b4cbd7a
1
Parent(s): bf1bf11
Feature: Extract and return audio file from agent output
Browse files
app.py
CHANGED
|
@@ -7,7 +7,7 @@ from langchain_google_genai import ChatGoogleGenerativeAI
|
|
| 7 |
from langchain.agents import create_agent
|
| 8 |
import tempfile
|
| 9 |
import shutil
|
| 10 |
-
|
| 11 |
# Get API keys from Hugging Face Secrets
|
| 12 |
GEMINI_API_KEY = os.environ.get("GEMINI_API_KEY")
|
| 13 |
ELEVENLABS_API_KEY = os.environ.get("ELEVENLABS_API_KEY")
|
|
@@ -107,7 +107,30 @@ def gradio_handler(age, gender, topic, pdf_file):
|
|
| 107 |
output = loop.run_until_complete(run_agent_dynamic(age, gender, topic, pdf_path))
|
| 108 |
finally:
|
| 109 |
loop.close()
|
| 110 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
except Exception as e:
|
| 112 |
return f"❌ Error: {str(e)}\n\nCheck logs for details.", None
|
| 113 |
finally:
|
|
|
|
| 7 |
from langchain.agents import create_agent
|
| 8 |
import tempfile
|
| 9 |
import shutil
|
| 10 |
+
import re
|
| 11 |
# Get API keys from Hugging Face Secrets
|
| 12 |
GEMINI_API_KEY = os.environ.get("GEMINI_API_KEY")
|
| 13 |
ELEVENLABS_API_KEY = os.environ.get("ELEVENLABS_API_KEY")
|
|
|
|
| 107 |
output = loop.run_until_complete(run_agent_dynamic(age, gender, topic, pdf_path))
|
| 108 |
finally:
|
| 109 |
loop.close()
|
| 110 |
+
|
| 111 |
+
# Extract audio file path from the output
|
| 112 |
+
output_str = str(output)
|
| 113 |
+
audio_path = None
|
| 114 |
+
|
| 115 |
+
# Look for the audio file mention in the output
|
| 116 |
+
if "File saved as:" in output_str:
|
| 117 |
+
# Extract filename from the output
|
| 118 |
+
import re
|
| 119 |
+
match = re.search(r'File saved as: ([\w,_\-\.]+\.mp3)', output_str)
|
| 120 |
+
if match:
|
| 121 |
+
filename = match.group(1)
|
| 122 |
+
# The file is saved in current directory, which is /app
|
| 123 |
+
audio_path = os.path.join("/app", filename)
|
| 124 |
+
|
| 125 |
+
# Check if file exists
|
| 126 |
+
if not os.path.exists(audio_path):
|
| 127 |
+
# Try current working directory
|
| 128 |
+
audio_path = filename
|
| 129 |
+
if not os.path.exists(audio_path):
|
| 130 |
+
audio_path = None
|
| 131 |
+
|
| 132 |
+
return f"✅ Success!\n\n{output_str}", audio_path
|
| 133 |
+
|
| 134 |
except Exception as e:
|
| 135 |
return f"❌ Error: {str(e)}\n\nCheck logs for details.", None
|
| 136 |
finally:
|