Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +31 -10
src/streamlit_app.py
CHANGED
|
@@ -8,6 +8,12 @@ from groq import Groq
|
|
| 8 |
from typing import Dict, Union
|
| 9 |
from elevenlabs.client import ElevenLabs
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
# Streamlit UI Configuration
|
| 12 |
st.set_page_config(
|
| 13 |
page_title="Tamil-English Voice Processor",
|
|
@@ -34,9 +40,13 @@ if ELEVENLABS_API_KEY:
|
|
| 34 |
def save_uploaded_file(uploaded_file):
|
| 35 |
"""Save uploaded file to a temporary location and return path"""
|
| 36 |
try:
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
except Exception as e:
|
| 41 |
st.error(f"Error saving file: {str(e)}")
|
| 42 |
return None
|
|
@@ -54,6 +64,14 @@ def transcribe_with_groq(audio_file_path) -> Union[str, None]:
|
|
| 54 |
except Exception as e:
|
| 55 |
st.error(f"Groq transcription failed: {str(e)}")
|
| 56 |
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
def transcribe_with_elevenlabs(audio_file_path) -> Union[str, None]:
|
| 59 |
"""Transcribe audio using ElevenLabs API"""
|
|
@@ -79,6 +97,14 @@ def transcribe_with_elevenlabs(audio_file_path) -> Union[str, None]:
|
|
| 79 |
if 'response' in locals():
|
| 80 |
st.error(f"API Response: {response.text}")
|
| 81 |
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
|
| 83 |
def extract_entities_with_llama(text: str) -> Dict:
|
| 84 |
"""Entity extraction using few-shot structured prompting"""
|
|
@@ -244,10 +270,5 @@ if audio_file and st.button("Process Audio", type="primary"):
|
|
| 244 |
for item in entities[category]:
|
| 245 |
st.write(f"- {item}")
|
| 246 |
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
if temp_file_path and os.path.exists(temp_file_path):
|
| 250 |
-
try:
|
| 251 |
-
os.unlink(temp_file_path)
|
| 252 |
-
except Exception as e:
|
| 253 |
-
st.warning(f"Could not delete temporary file: {str(e)}")
|
|
|
|
| 8 |
from typing import Dict, Union
|
| 9 |
from elevenlabs.client import ElevenLabs
|
| 10 |
|
| 11 |
+
# Disable telemetry to prevent permission issues in Hugging Face Spaces
|
| 12 |
+
os.environ["STREAMLIT_SERVER_ENABLE_STATIC_FILE_HANDLING"] = "false"
|
| 13 |
+
os.environ["STREAMLIT_SERVER_ENABLE_XSRF_PROTECTION"] = "false"
|
| 14 |
+
os.environ["STREAMLIT_SERVER_ENABLE_CORS"] = "false"
|
| 15 |
+
os.environ["STREAMLIT_TELEMETRY_ENABLED"] = "false"
|
| 16 |
+
|
| 17 |
# Streamlit UI Configuration
|
| 18 |
st.set_page_config(
|
| 19 |
page_title="Tamil-English Voice Processor",
|
|
|
|
| 40 |
def save_uploaded_file(uploaded_file):
|
| 41 |
"""Save uploaded file to a temporary location and return path"""
|
| 42 |
try:
|
| 43 |
+
# Create a temporary directory in /tmp which we have write access to
|
| 44 |
+
temp_dir = tempfile.mkdtemp()
|
| 45 |
+
temp_path = os.path.join(temp_dir, "uploaded_audio.mp3")
|
| 46 |
+
|
| 47 |
+
with open(temp_path, "wb") as f:
|
| 48 |
+
f.write(uploaded_file.getbuffer())
|
| 49 |
+
return temp_path
|
| 50 |
except Exception as e:
|
| 51 |
st.error(f"Error saving file: {str(e)}")
|
| 52 |
return None
|
|
|
|
| 64 |
except Exception as e:
|
| 65 |
st.error(f"Groq transcription failed: {str(e)}")
|
| 66 |
return None
|
| 67 |
+
finally:
|
| 68 |
+
# Clean up the temporary directory
|
| 69 |
+
if os.path.exists(os.path.dirname(audio_file_path)):
|
| 70 |
+
try:
|
| 71 |
+
os.remove(audio_file_path)
|
| 72 |
+
os.rmdir(os.path.dirname(audio_file_path))
|
| 73 |
+
except:
|
| 74 |
+
pass
|
| 75 |
|
| 76 |
def transcribe_with_elevenlabs(audio_file_path) -> Union[str, None]:
|
| 77 |
"""Transcribe audio using ElevenLabs API"""
|
|
|
|
| 97 |
if 'response' in locals():
|
| 98 |
st.error(f"API Response: {response.text}")
|
| 99 |
return None
|
| 100 |
+
finally:
|
| 101 |
+
# Clean up the temporary directory
|
| 102 |
+
if os.path.exists(os.path.dirname(audio_file_path)):
|
| 103 |
+
try:
|
| 104 |
+
os.remove(audio_file_path)
|
| 105 |
+
os.rmdir(os.path.dirname(audio_file_path))
|
| 106 |
+
except:
|
| 107 |
+
pass
|
| 108 |
|
| 109 |
def extract_entities_with_llama(text: str) -> Dict:
|
| 110 |
"""Entity extraction using few-shot structured prompting"""
|
|
|
|
| 270 |
for item in entities[category]:
|
| 271 |
st.write(f"- {item}")
|
| 272 |
|
| 273 |
+
except Exception as e:
|
| 274 |
+
st.error(f"An error occurred during processing: {str(e)}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|