Spaces:
Sleeping
Sleeping
File size: 12,513 Bytes
fb59a4d 57966f8 fb59a4d 57966f8 e8513b3 fb59a4d 57966f8 fb59a4d 57966f8 fb59a4d 57966f8 fb59a4d 57966f8 fb59a4d 3c834a4 57966f8 fb59a4d 57966f8 fb59a4d 3c834a4 57966f8 fb59a4d 57966f8 fb59a4d 57966f8 fb59a4d 57966f8 fb59a4d 57966f8 fb59a4d 57966f8 fb59a4d 57966f8 fb59a4d 788d26b 57966f8 fb59a4d 3c834a4 2b82738 57966f8 fb59a4d 57966f8 fb59a4d 57966f8 2b82738 57966f8 fb59a4d 57966f8 fb59a4d e8513b3 57966f8 fb59a4d 57966f8 2db9a09 57966f8 fb59a4d 57966f8 fb59a4d 57966f8 fb59a4d 3c834a4 fb59a4d 57966f8 fb59a4d 3c834a4 fb59a4d 3c834a4 57966f8 fb59a4d 57966f8 fb59a4d 3c834a4 fb59a4d 57966f8 fb59a4d 57966f8 fb59a4d 57966f8 fb59a4d 57966f8 fb59a4d 57966f8 fb59a4d 57966f8 fb59a4d 57966f8 fb59a4d 57966f8 fb59a4d 3c834a4 788d26b fb59a4d e8513b3 788d26b fb59a4d 788d26b 3c834a4 fb59a4d 3c834a4 fb59a4d 3c834a4 fb59a4d 57966f8 fb59a4d 3c834a4 57966f8 3c834a4 57966f8 fb59a4d 57966f8 3c834a4 57966f8 fb59a4d 3c834a4 |
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 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 |
import os
import gc
import json
import logging
import tempfile
from datetime import datetime, timedelta
from pathlib import Path
from dataclasses import dataclass
import streamlit as st
import whisper
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
import numpy as np
import librosa
import humanize
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)
# Constants
MAX_FILE_SIZE = 25 * 1024 * 1024 # 25MB
MAX_AUDIO_DURATION = 600 # 10 minutes
MIN_SAMPLE_RATE = 16000 # 16kHz
SUPPORTED_FORMATS = {'.wav', '.mp3', '.m4a'}
# Model configuration
MODEL_CONFIG = {
"path": "gpt2",
"description": "Efficient open-source model for analysis",
"memory_required": "8GB"
}
@dataclass
class VCStyle:
name: str
note_format: dict
key_interests: list
custom_sections: list
insight_preferences: dict
class AudioValidator:
@staticmethod
def validate_audio_file(file):
stats = {
'file_size': None,
'duration': None,
'sample_rate': None,
'format': None
}
try:
if file is None:
return False, "No file was uploaded.", stats
# Check file size
file_size = len(file.getvalue())
stats['file_size'] = humanize.naturalsize(file_size)
if file_size > MAX_FILE_SIZE:
return False, f"File size ({stats['file_size']}) exceeds limit", stats
# Check file extension
file_extension = Path(file.name).suffix.lower()
stats['format'] = file_extension
if file_extension not in SUPPORTED_FORMATS:
return False, f"Unsupported format {file_extension}", stats
# Create temporary file
with tempfile.NamedTemporaryFile(delete=False, suffix=file_extension) as tmp_file:
tmp_file.write(file.getvalue())
tmp_file_path = tmp_file.name
try:
# Check audio properties
y, sr = librosa.load(tmp_file_path, sr=None)
duration = librosa.get_duration(y=y, sr=sr)
stats.update({
'duration': str(timedelta(seconds=int(duration))),
'sample_rate': f"{sr/1000:.1f}kHz"
})
if duration > MAX_AUDIO_DURATION:
return False, f"Duration ({stats['duration']}) exceeds limit", stats
if sr < MIN_SAMPLE_RATE:
return False, f"Sample rate too low ({stats['sample_rate']})", stats
return True, "Audio file is valid", stats
finally:
os.unlink(tmp_file_path)
except Exception as e:
logger.error(f"Validation error: {str(e)}")
return False, str(e), stats
class AudioProcessor:
def __init__(self, model):
self.model = model
self.validator = AudioValidator()
def process_audio(self, audio_file):
stats = {
'status': 'processing',
'start_time': datetime.now(),
'file_info': None,
'processing_time': None,
'error': None
}
try:
# Validate file
is_valid, message, file_stats = self.validator.validate_audio_file(audio_file)
stats['file_info'] = file_stats
if not is_valid:
stats['status'] = 'failed'
stats['error'] = message
return None, stats
# Process audio
with tempfile.NamedTemporaryFile(delete=False, suffix=file_stats['format']) as tmp_file:
tmp_file.write(audio_file.getvalue())
tmp_file_path = tmp_file.name
try:
result = self.model.transcribe(
tmp_file_path,
language="en",
task="transcribe",
fp16=torch.cuda.is_available()
)
stats['status'] = 'success'
stats['processing_time'] = str(datetime.now() - stats['start_time'])
return result["text"], stats
finally:
os.unlink(tmp_file_path)
except Exception as e:
logger.error(f"Processing error: {str(e)}")
stats['status'] = 'failed'
stats['error'] = str(e)
return None, stats
finally:
if torch.cuda.is_available():
torch.cuda.empty_cache()
gc.collect()
@st.cache_resource
def load_whisper():
try:
return whisper.load_model("base")
except Exception as e:
logger.error(f"Whisper model loading error: {str(e)}")
return None
@st.cache_resource
def load_llm():
try:
tokenizer = AutoTokenizer.from_pretrained(
MODEL_CONFIG["path"],
trust_remote_code=True
)
model = AutoModelForCausalLM.from_pretrained(
MODEL_CONFIG["path"],
device_map="auto",
torch_dtype=torch.float16,
low_cpu_mem_usage=True
)
return pipeline(
"text-generation",
model=model,
tokenizer=tokenizer,
max_new_tokens=512,
temperature=0.7,
top_p=0.95,
repetition_penalty=1.15,
batch_size=1
)
except Exception as e:
logger.error(f"LLM loading error: {str(e)}")
return None
class ContentAnalyzer:
def __init__(self, generator):
self.generator = generator
def analyze_text(self, text, vc_style):
try:
prompt = self._create_analysis_prompt(text, vc_style)
response = self._generate_response(prompt)
return self._parse_response(response)
except Exception as e:
logger.error(f"Analysis error: {str(e)}")
return None
def _create_analysis_prompt(self, text, vc_style):
interests = ', '.join(vc_style.key_interests)
return f"""Analyze this startup pitch focusing on {interests}:
{text}
Provide structured insights for:
1. Key Points
2. Metrics
3. Risks
4. Questions"""
def _generate_response(self, prompt):
try:
response = self.generator(prompt)
return response[0]['generated_text']
except Exception as e:
logger.error(f"Generation error: {str(e)}")
return ""
def _parse_response(self, response):
try:
sections = response.split('\n\n')
parsed = {}
current_section = "general"
for section in sections:
if section.strip().endswith(':'):
current_section = section.strip()[:-1].lower()
parsed[current_section] = []
else:
if current_section in parsed:
parsed[current_section].append(section.strip())
else:
parsed[current_section] = [section.strip()]
return parsed
except Exception as e:
logger.error(f"Parsing error: {str(e)}")
return {"error": "Failed to parse response"}
def setup_page():
st.set_page_config(
page_title="VC Call Assistant",
page_icon="ποΈ",
layout="wide",
)
def show_file_uploader():
st.markdown("""
### π Upload Audio File
**Supported formats:** WAV, MP3, M4A
**Limits:** 25MB, 10 minutes, 16kHz min quality
""")
return st.file_uploader(
"Choose an audio file",
type=['wav', 'mp3', 'm4a']
)
def show_processing_stats(stats):
if not stats:
return
st.markdown("### π Processing Information")
cols = st.columns(3)
if stats.get('file_info'):
with cols[0]:
st.metric("File Size", stats['file_info'].get('file_size', 'N/A'))
st.metric("Format", stats['file_info'].get('format', 'N/A'))
with cols[1]:
st.metric("Duration", stats['file_info'].get('duration', 'N/A'))
st.metric("Sample Rate", stats['file_info'].get('sample_rate', 'N/A'))
with cols[2]:
status = stats.get('status', 'unknown')
if status == 'success':
st.success(f"Processed in {stats.get('processing_time', 'N/A')}")
elif status == 'failed':
st.error(f"Failed: {stats.get('error', 'Unknown error')}")
else:
st.info("Processing...")
def main():
try:
setup_page()
with st.sidebar:
st.title("VC Assistant Settings")
st.info(f"""Using GPT2
Memory: {MODEL_CONFIG['memory_required']}
Info: {MODEL_CONFIG['description']}""")
vc_name = st.text_input("Your Name")
note_style = st.selectbox(
"Note Style",
["Bullet Points", "Paragraphs", "Q&A"]
)
interests = st.multiselect(
"Focus Areas",
["Product", "Market", "Team", "Financials", "Technology"],
default=["Product", "Market"]
)
st.title("ποΈ VC Call Assistant")
if not vc_name:
st.warning("Please enter your name in the sidebar.")
return
with st.spinner("Loading models..."):
whisper_model = load_whisper()
llm = load_llm()
if not whisper_model or not llm:
st.error("Failed to initialize models. Please refresh the page.")
return
audio_processor = AudioProcessor(whisper_model)
analyzer = ContentAnalyzer(llm)
audio_file = show_file_uploader()
if audio_file:
with st.spinner("Processing audio..."):
transcription, stats = audio_processor.process_audio(audio_file)
show_processing_stats(stats)
if transcription and stats['status'] == 'success':
col1, col2 = st.columns(2)
with col1:
st.subheader("π Transcript")
st.write(transcription)
with col2:
st.subheader("π Analysis")
with st.spinner("Analyzing transcript..."):
vc_style = VCStyle(
name=vc_name,
note_format={"style": note_style},
key_interests=interests,
custom_sections=[],
insight_preferences={}
)
analysis = analyzer.analyze_text(transcription, vc_style)
if analysis:
st.write(analysis)
st.download_button(
"π₯ Export Analysis",
data=json.dumps({
"timestamp": datetime.now().isoformat(),
"transcription": transcription,
"analysis": analysis,
"processing_stats": stats
}, indent=2),
file_name=f"vc_analysis_{datetime.now():%Y%m%d_%H%M%S}.json",
mime="application/json"
)
except Exception as e:
logger.error(f"Application error: {str(e)}")
st.error("An error occurred. Please refresh the page and try again.")
finally:
gc.collect()
if torch.cuda.is_available():
torch.cuda.empty_cache()
if __name__ == "__main__":
main()
|