Spaces:
Sleeping
Sleeping
File size: 10,135 Bytes
4850901 5f476ba 4850901 6db0300 4850901 5f476ba d116a29 5f476ba 4850901 5f476ba 4850901 6db0300 5f476ba 6db0300 5f476ba 6db0300 4850901 5f476ba 4850901 6db0300 5f476ba 6db0300 5f476ba 6db0300 4850901 5f476ba 4850901 6db0300 5f476ba 6db0300 5f476ba 6db0300 4850901 5f476ba 4850901 4e31df1 6db0300 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 |
import streamlit as st
import json
import re
import tempfile
import os
import requests
from groq import Groq
from typing import Dict, Union
from elevenlabs.client import ElevenLabs
from pydub import AudioSegment
# Disable telemetry to prevent permission issues in Hugging Face Spaces
os.environ["STREAMLIT_SERVER_ENABLE_STATIC_FILE_HANDLING"] = "false"
os.environ["STREAMLIT_SERVER_ENABLE_XSRF_PROTECTION"] = "false"
os.environ["STREAMLIT_SERVER_ENABLE_CORS"] = "false"
os.environ["STREAMLIT_TELEMETRY_ENABLED"] = "false"
# Streamlit UI Configuration
st.set_page_config(
page_title="Tamil-English Voice Processor",
layout="wide",
page_icon="🎤"
)
# Configuration - Using environment variables for Hugging Face Spaces
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
ELEVENLABS_API_KEY = os.getenv("ELEVENLABS_API_KEY")
if not GROQ_API_KEY:
st.error("GROQ API Key not found in environment variables")
st.stop()
# Initialize clients
client = Groq(api_key=GROQ_API_KEY)
if ELEVENLABS_API_KEY:
eleven_client = ElevenLabs(api_key=ELEVENLABS_API_KEY)
def convert_to_mp3(audio_file_path):
"""Convert audio file to MP3 format if needed"""
try:
audio = AudioSegment.from_file(audio_file_path)
mp3_path = audio_file_path.replace(os.path.splitext(audio_file_path)[1], ".mp3")
audio.export(mp3_path, format="mp3")
return mp3_path
except Exception as e:
st.error(f"Audio conversion error: {str(e)}")
return None
def save_uploaded_file(uploaded_file):
"""Save uploaded file to a temporary location and return path"""
try:
# Create a temporary directory in /tmp which we have write access to
temp_dir = tempfile.mkdtemp()
temp_path = os.path.join(temp_dir, uploaded_file.name)
with open(temp_path, "wb") as f:
f.write(uploaded_file.getbuffer())
# Convert to MP3 if needed (Whisper works best with MP3)
if not temp_path.lower().endswith('.mp3'):
converted_path = convert_to_mp3(temp_path)
if converted_path:
return converted_path
return temp_path
except Exception as e:
st.error(f"Error saving file: {str(e)}")
return None
def transcribe_with_groq(audio_file_path) -> Union[str, None]:
"""Transcribe audio using Groq's Whisper model"""
try:
with open(audio_file_path, "rb") as audio_file:
transcription = client.audio.transcriptions.create(
file=audio_file,
model="whisper-large-v3",
response_format="text"
)
return transcription
except Exception as e:
st.error(f"Groq transcription failed: {str(e)}")
return None
finally:
# Clean up the temporary files
if os.path.exists(audio_file_path):
try:
os.remove(audio_file_path)
temp_dir = os.path.dirname(audio_file_path)
if os.path.exists(temp_dir):
os.rmdir(temp_dir)
except:
pass
def transcribe_with_elevenlabs(audio_file_path) -> Union[str, None]:
"""Transcribe audio using ElevenLabs API"""
if not ELEVENLABS_API_KEY:
st.error("ElevenLabs API key not configured")
return None
try:
url = "https://api.elevenlabs.io/v1/speech-to-text"
headers = {"xi-api-key": ELEVENLABS_API_KEY}
with open(audio_file_path, 'rb') as f:
files = {'file': f}
data = {'model_id': 'eleven_monolingual_v2'}
response = requests.post(url, headers=headers, files=files, data=data)
response.raise_for_status()
result = response.json()
return result.get('text', '')
except Exception as e:
st.error(f"ElevenLabs transcription failed: {str(e)}")
if 'response' in locals():
st.error(f"API Response: {response.text}")
return None
finally:
# Clean up the temporary files
if os.path.exists(audio_file_path):
try:
os.remove(audio_file_path)
temp_dir = os.path.dirname(audio_file_path)
if os.path.exists(temp_dir):
os.rmdir(temp_dir)
except:
pass
def extract_entities_with_llama(text: str) -> Dict:
"""Entity extraction using few-shot structured prompting"""
system_prompt = """
You are an expert at extracting structured information from Tamil-English (Tanglish) text.
Always return a JSON object with these exact fields:
- person_name: array of Tamil/English names
- place: array of locations
- phone_number: array of numbers in +91XXXXXXXXXX format
- skills: array of skills in Tamil/English
- intent: one of [Introduction, JobQuery, Complaint, InformationRequest, Other]
- intent_confidence: number between 0-1
Examples:
Example 1:
Text: "என் பெயர் ராஜா, நான் திரிச்சியில் வசிக்கிறேன். என் எண் +917777777777. நான் விவசாயி."
Output: {
"person_name": ["ராஜா"],
"place": ["திரிச்சி"],
"phone_number": ["+917777777777"],
"skills": ["விவசாயி"],
"intent": "Introduction",
"intent_confidence": 0.9
}
Example 2:
Text: "I'm Kumar from Chennai. My skills are Python and Machine Learning. Call me at 9876543210."
Output: {
"person_name": ["Kumar"],
"place": ["Chennai"],
"phone_number": ["+919876543210"],
"skills": ["Python", "Machine Learning"],
"intent": "JobQuery",
"intent_confidence": 0.85
}
Example 3:
Text: "The roads in Madurai are very bad. Please fix them."
Output: {
"person_name": [],
"place": ["Madurai"],
"phone_number": [],
"skills": [],
"intent": "Complaint",
"intent_confidence": 0.95
}
Now analyze this text:
"""
try:
response = client.chat.completions.create(
messages=[
{
"role": "system",
"content": system_prompt
},
{
"role": "user",
"content": text
}
],
model="llama3-70b-8192",
response_format={"type": "json_object"},
temperature=0.1
)
# Extract JSON from response
raw_response = response.choices[0].message.content
json_match = re.search(r'\{.*\}', raw_response, re.DOTALL)
if not json_match:
raise ValueError("No JSON found in response")
entities = json.loads(json_match.group())
# Validate and clean the response
return {
"person_name": entities.get("person_name", []),
"place": entities.get("place", []),
"phone_number": [re.sub(r'[^\d+]', '', num) for num in entities.get("phone_number", [])],
"skills": entities.get("skills", []),
"intent": entities.get("intent", "Other"),
"intent_confidence": min(max(float(entities.get("intent_confidence", 0)), 0), 1)
}
except Exception as e:
st.error(f"Analysis error: {str(e)}")
return {
"person_name": [],
"place": [],
"phone_number": [],
"skills": [],
"intent": "unknown",
"intent_confidence": 0
}
# UI Elements
st.title("🎤 Tamil+English Voice to Structured Data")
with st.expander("ℹ️ How to use"):
st.write("""
1. Upload an audio file (MP3/WAV format)
2. Select transcription model
3. Click 'Process Audio' button
4. View transcription and extracted information
""")
# Model selection
transcription_model = st.radio(
"Select Transcription Model",
["Groq (Whisper)", "ElevenLabs"],
index=0,
help="Choose which model to use for transcription"
)
audio_file = st.file_uploader(
"Upload audio file",
type=["mp3", "wav", "ogg", "m4a"],
help="Supported formats: MP3, WAV, OGG, M4A (max 25MB)"
)
if audio_file and st.button("Process Audio", type="primary"):
# Save the uploaded file first
temp_file_path = save_uploaded_file(audio_file)
if not temp_file_path:
st.error("Failed to process uploaded file")
st.stop()
try:
with st.spinner("Transcribing..."):
if transcription_model == "Groq (Whisper)":
transcription = transcribe_with_groq(temp_file_path)
else:
transcription = transcribe_with_elevenlabs(temp_file_path)
if transcription:
st.subheader("Transcription")
st.text_area("Transcript", transcription, height=150, label_visibility="collapsed")
with st.spinner("Analyzing content..."):
entities = extract_entities_with_llama(transcription)
st.subheader("Structured Output")
col1, col2 = st.columns(2)
with col1:
st.json(entities, expanded=True)
with col2:
st.metric("Detected Intent",
value=entities["intent"],
help=f"Confidence: {entities['intent_confidence']:.2f}")
for category, emoji in [("person_name", "👤"),
("place", "📍"),
("phone_number", "📞"),
("skills", "🛠️")]:
if entities[category]:
st.write(f"{emoji} **{category.replace('_', ' ').title()}**")
for item in entities[category]:
st.write(f"- {item}")
except Exception as e:
st.error(f"An error occurred during processing: {str(e)}") |