Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files
app.py
CHANGED
@@ -1,63 +1,19 @@
|
|
1 |
-
|
2 |
-
import whisper
|
3 |
-
import pandas as pd
|
4 |
-
from datetime import datetime
|
5 |
-
import tempfile
|
6 |
-
import os
|
7 |
-
import torch
|
8 |
-
from transformers import (
|
9 |
-
AutoModelForCausalLM,
|
10 |
-
AutoTokenizer,
|
11 |
-
pipeline,
|
12 |
-
BitsAndBytesConfig
|
13 |
-
)
|
14 |
-
import gc
|
15 |
-
from typing import Optional, Dict, Any, List
|
16 |
-
import logging
|
17 |
-
import json
|
18 |
-
import numpy as np
|
19 |
-
from dataclasses import dataclass, asdict
|
20 |
-
from queue import Queue
|
21 |
-
import threading
|
22 |
-
from collections import defaultdict
|
23 |
|
24 |
-
#
|
25 |
-
logging.basicConfig(level=logging.INFO)
|
26 |
-
logger = logging.getLogger(__name__)
|
27 |
-
|
28 |
-
# Constants for memory optimization
|
29 |
-
CHUNK_SIZE = 30 # seconds
|
30 |
-
MAX_AUDIO_LENGTH = 600 # seconds (10 minutes)
|
31 |
-
BATCH_SIZE = 8
|
32 |
-
|
33 |
-
# Model configurations with memory optimization
|
34 |
MODEL_CONFIGS = {
|
35 |
-
"
|
36 |
-
"path": "
|
37 |
-
"description": "Efficient model for
|
38 |
-
"memory_required": "
|
|
|
|
|
|
|
|
|
|
|
39 |
}
|
40 |
}
|
41 |
|
42 |
-
@dataclass
|
43 |
-
class VCStyle:
|
44 |
-
"""Store VC's personal style preferences"""
|
45 |
-
name: str
|
46 |
-
note_format: Dict[str, Any]
|
47 |
-
key_interests: List[str]
|
48 |
-
custom_sections: List[str]
|
49 |
-
insight_preferences: Dict[str, float]
|
50 |
-
|
51 |
-
@dataclass
|
52 |
-
class LiveCallContext:
|
53 |
-
"""Store context for live calls"""
|
54 |
-
meeting_id: str
|
55 |
-
participants: List[str]
|
56 |
-
topics: List[str]
|
57 |
-
key_points: List[str]
|
58 |
-
questions_asked: List[str]
|
59 |
-
action_items: List[str]
|
60 |
-
|
61 |
class ModelManager:
|
62 |
"""Handles model loading and resource management"""
|
63 |
|
@@ -87,13 +43,11 @@ class ModelManager:
|
|
87 |
|
88 |
tokenizer = AutoTokenizer.from_pretrained(
|
89 |
config["path"],
|
90 |
-
token=st.secrets.get("HF_TOKEN"),
|
91 |
trust_remote_code=True
|
92 |
)
|
93 |
|
94 |
model = AutoModelForCausalLM.from_pretrained(
|
95 |
config["path"],
|
96 |
-
token=st.secrets.get("HF_TOKEN"),
|
97 |
quantization_config=bnb_config,
|
98 |
device_map="auto",
|
99 |
torch_dtype=torch.float16,
|
@@ -104,7 +58,7 @@ class ModelManager:
|
|
104 |
"text-generation",
|
105 |
model=model,
|
106 |
tokenizer=tokenizer,
|
107 |
-
max_new_tokens=512,
|
108 |
temperature=0.7,
|
109 |
top_p=0.95,
|
110 |
repetition_penalty=1.15,
|
@@ -118,100 +72,6 @@ class ModelManager:
|
|
118 |
st.error("Failed to load language model. Please try again.")
|
119 |
return None
|
120 |
|
121 |
-
class AudioProcessor:
|
122 |
-
"""Handles audio processing with memory optimization"""
|
123 |
-
|
124 |
-
def __init__(self, model):
|
125 |
-
self.model = model
|
126 |
-
self.chunk_queue = Queue()
|
127 |
-
|
128 |
-
def process_audio_chunk(self, audio_chunk) -> Optional[str]:
|
129 |
-
try:
|
130 |
-
# Clear GPU memory before processing
|
131 |
-
if torch.cuda.is_available():
|
132 |
-
torch.cuda.empty_cache()
|
133 |
-
|
134 |
-
result = self.model.transcribe(
|
135 |
-
audio_chunk,
|
136 |
-
language="en",
|
137 |
-
task="transcribe",
|
138 |
-
fp16=True # Use half precision
|
139 |
-
)
|
140 |
-
return result["text"]
|
141 |
-
|
142 |
-
except Exception as e:
|
143 |
-
logger.error(f"Error processing audio chunk: {e}")
|
144 |
-
return None
|
145 |
-
finally:
|
146 |
-
# Cleanup
|
147 |
-
gc.collect()
|
148 |
-
if torch.cuda.is_available():
|
149 |
-
torch.cuda.empty_cache()
|
150 |
-
|
151 |
-
class ContentAnalyzer:
|
152 |
-
"""Handles text analysis with optimized prompts"""
|
153 |
-
|
154 |
-
def __init__(self, generator):
|
155 |
-
self.generator = generator
|
156 |
-
|
157 |
-
def analyze_text(self, text: str, vc_style: VCStyle) -> Optional[Dict[str, Any]]:
|
158 |
-
try:
|
159 |
-
prompt = self._create_analysis_prompt(text, vc_style)
|
160 |
-
response = self._generate_response(prompt, max_length=512)
|
161 |
-
return self._parse_response(response)
|
162 |
-
except Exception as e:
|
163 |
-
logger.error(f"Analysis error: {e}")
|
164 |
-
return None
|
165 |
-
|
166 |
-
def _create_analysis_prompt(self, text: str, vc_style: VCStyle) -> str:
|
167 |
-
return f"""Analyze this startup pitch focusing on {', '.join(vc_style.key_interests)}:
|
168 |
-
|
169 |
-
{text}
|
170 |
-
|
171 |
-
Provide structured insights for:
|
172 |
-
1. Key Points
|
173 |
-
2. Metrics
|
174 |
-
3. Risks
|
175 |
-
4. Questions"""
|
176 |
-
|
177 |
-
def _generate_response(self, prompt: str, max_length: int) -> str:
|
178 |
-
try:
|
179 |
-
response = self.generator(
|
180 |
-
prompt,
|
181 |
-
max_new_tokens=max_length,
|
182 |
-
temperature=0.7,
|
183 |
-
top_p=0.95,
|
184 |
-
repetition_penalty=1.15
|
185 |
-
)
|
186 |
-
return response[0]['generated_text']
|
187 |
-
except Exception as e:
|
188 |
-
logger.error(f"Generation error: {e}")
|
189 |
-
return ""
|
190 |
-
|
191 |
-
class UIManager:
|
192 |
-
"""Manages Streamlit UI with performance optimization"""
|
193 |
-
|
194 |
-
@staticmethod
|
195 |
-
def setup_page():
|
196 |
-
st.set_page_config(
|
197 |
-
page_title="VC Call Assistant",
|
198 |
-
page_icon="🎙️",
|
199 |
-
layout="wide",
|
200 |
-
initial_sidebar_state="expanded"
|
201 |
-
)
|
202 |
-
|
203 |
-
@staticmethod
|
204 |
-
def show_file_uploader() -> Optional[Any]:
|
205 |
-
return st.file_uploader(
|
206 |
-
"Upload Audio (Max 10 minutes)",
|
207 |
-
type=['wav', 'mp3', 'm4a'],
|
208 |
-
help="Supports WAV, MP3, M4A formats. Maximum duration: 10 minutes."
|
209 |
-
)
|
210 |
-
|
211 |
-
@staticmethod
|
212 |
-
def show_progress(text: str) -> Any:
|
213 |
-
return st.progress(0, text=text)
|
214 |
-
|
215 |
def main():
|
216 |
try:
|
217 |
# Initialize UI
|
@@ -220,95 +80,15 @@ def main():
|
|
220 |
# Sidebar
|
221 |
with st.sidebar:
|
222 |
st.title("VC Assistant Settings")
|
223 |
-
model_name =
|
|
|
|
|
|
|
|
|
|
|
224 |
|
225 |
st.info(f"""Using {model_name}
|
226 |
Memory Usage: {MODEL_CONFIGS[model_name]['memory_required']}
|
227 |
Description: {MODEL_CONFIGS[model_name]['description']}""")
|
228 |
|
229 |
-
#
|
230 |
-
vc_name = st.text_input("Your Name")
|
231 |
-
note_style = st.selectbox(
|
232 |
-
"Note Style",
|
233 |
-
["Bullet Points", "Paragraphs", "Q&A"]
|
234 |
-
)
|
235 |
-
|
236 |
-
interests = st.multiselect(
|
237 |
-
"Focus Areas",
|
238 |
-
["Product", "Market", "Team", "Financials", "Technology"],
|
239 |
-
default=["Product", "Market"]
|
240 |
-
)
|
241 |
-
|
242 |
-
# Main content
|
243 |
-
st.title("🎙️ VC Call Assistant")
|
244 |
-
|
245 |
-
if not vc_name:
|
246 |
-
st.warning("Please enter your name in the sidebar.")
|
247 |
-
return
|
248 |
-
|
249 |
-
# Initialize processors
|
250 |
-
with st.spinner("Loading models..."):
|
251 |
-
whisper_model = ModelManager.load_whisper()
|
252 |
-
llm = ModelManager.load_llm(model_name)
|
253 |
-
|
254 |
-
if not whisper_model or not llm:
|
255 |
-
st.error("Failed to initialize models. Please refresh the page.")
|
256 |
-
return
|
257 |
-
|
258 |
-
audio_processor = AudioProcessor(whisper_model)
|
259 |
-
analyzer = ContentAnalyzer(llm)
|
260 |
-
|
261 |
-
# File upload
|
262 |
-
audio_file = UIManager.show_file_uploader()
|
263 |
-
|
264 |
-
if audio_file:
|
265 |
-
# Process audio
|
266 |
-
with st.spinner("Processing audio..."):
|
267 |
-
transcription = audio_processor.process_audio_chunk(audio_file)
|
268 |
-
|
269 |
-
if transcription:
|
270 |
-
# Display results
|
271 |
-
col1, col2 = st.columns(2)
|
272 |
-
|
273 |
-
with col1:
|
274 |
-
st.subheader("📝 Transcript")
|
275 |
-
st.write(transcription)
|
276 |
-
|
277 |
-
with col2:
|
278 |
-
st.subheader("🔍 Analysis")
|
279 |
-
vc_style = VCStyle(
|
280 |
-
name=vc_name,
|
281 |
-
note_format={"style": note_style},
|
282 |
-
key_interests=interests,
|
283 |
-
custom_sections=[],
|
284 |
-
insight_preferences={}
|
285 |
-
)
|
286 |
-
|
287 |
-
analysis = analyzer.analyze_text(transcription, vc_style)
|
288 |
-
if analysis:
|
289 |
-
st.write(analysis)
|
290 |
-
|
291 |
-
# Export option
|
292 |
-
st.download_button(
|
293 |
-
"📥 Export Analysis",
|
294 |
-
data=json.dumps({
|
295 |
-
"timestamp": datetime.now().isoformat(),
|
296 |
-
"transcription": transcription,
|
297 |
-
"analysis": analysis
|
298 |
-
}, indent=2),
|
299 |
-
file_name=f"vc_analysis_{datetime.now():%Y%m%d_%H%M%S}.json",
|
300 |
-
mime="application/json"
|
301 |
-
)
|
302 |
-
|
303 |
-
except Exception as e:
|
304 |
-
logger.error(f"Application error: {e}")
|
305 |
-
st.error("An unexpected error occurred. Please refresh the page.")
|
306 |
-
|
307 |
-
finally:
|
308 |
-
# Cleanup
|
309 |
-
gc.collect()
|
310 |
-
if torch.cuda.is_available():
|
311 |
-
torch.cuda.empty_cache()
|
312 |
-
|
313 |
-
if __name__ == "__main__":
|
314 |
-
main()
|
|
|
1 |
+
# Only showing the modified sections for brevity
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
+
# Update MODEL_CONFIGS to use open models
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
MODEL_CONFIGS = {
|
5 |
+
"FLAN-T5-Large": {
|
6 |
+
"path": "google/flan-t5-large",
|
7 |
+
"description": "Efficient open-source model for analysis",
|
8 |
+
"memory_required": "8GB"
|
9 |
+
},
|
10 |
+
"OpenAssistant": {
|
11 |
+
"path": "OpenAssistant/oasst-sft-4-pythia-12b-epoch-3.5",
|
12 |
+
"description": "Powerful open-source assistant model",
|
13 |
+
"memory_required": "12GB"
|
14 |
}
|
15 |
}
|
16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
class ModelManager:
|
18 |
"""Handles model loading and resource management"""
|
19 |
|
|
|
43 |
|
44 |
tokenizer = AutoTokenizer.from_pretrained(
|
45 |
config["path"],
|
|
|
46 |
trust_remote_code=True
|
47 |
)
|
48 |
|
49 |
model = AutoModelForCausalLM.from_pretrained(
|
50 |
config["path"],
|
|
|
51 |
quantization_config=bnb_config,
|
52 |
device_map="auto",
|
53 |
torch_dtype=torch.float16,
|
|
|
58 |
"text-generation",
|
59 |
model=model,
|
60 |
tokenizer=tokenizer,
|
61 |
+
max_new_tokens=512,
|
62 |
temperature=0.7,
|
63 |
top_p=0.95,
|
64 |
repetition_penalty=1.15,
|
|
|
72 |
st.error("Failed to load language model. Please try again.")
|
73 |
return None
|
74 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
75 |
def main():
|
76 |
try:
|
77 |
# Initialize UI
|
|
|
80 |
# Sidebar
|
81 |
with st.sidebar:
|
82 |
st.title("VC Assistant Settings")
|
83 |
+
model_name = st.selectbox(
|
84 |
+
"Select Model",
|
85 |
+
list(MODEL_CONFIGS.keys()),
|
86 |
+
index=0,
|
87 |
+
help="Choose the AI model for analysis"
|
88 |
+
)
|
89 |
|
90 |
st.info(f"""Using {model_name}
|
91 |
Memory Usage: {MODEL_CONFIGS[model_name]['memory_required']}
|
92 |
Description: {MODEL_CONFIGS[model_name]['description']}""")
|
93 |
|
94 |
+
# Rest of the sidebar code remains the same
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|