pavlyhalim commited on
Commit
fd8df46
Β·
1 Parent(s): 9f56911

added app.py and req

Browse files
Files changed (2) hide show
  1. app.py +814 -0
  2. requirements.txt +9 -0
app.py ADDED
@@ -0,0 +1,814 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import plotly.express as px
3
+ import plotly.graph_objects as go
4
+ import google.generativeai as genai
5
+ import pandas as pd
6
+ from typing import List, Dict, Any
7
+ import json
8
+ import os
9
+ import re
10
+ from datetime import datetime, timedelta
11
+ import logging
12
+ from pathlib import Path
13
+ import asyncio
14
+ from collections import deque
15
+ import time
16
+ import docx
17
+
18
+ # Utility Functions
19
+ def read_file_content(file):
20
+ """Read content from either TXT or DOCX file"""
21
+ try:
22
+ file_extension = file.name.lower().split('.')[-1]
23
+ if file_extension == 'txt':
24
+ return file.read().decode('utf-8')
25
+ elif file_extension == 'docx':
26
+ doc_bytes = file.read()
27
+ with open("temp.docx", "wb") as temp_file:
28
+ temp_file.write(doc_bytes)
29
+ doc = docx.Document("temp.docx")
30
+ content = "\n".join([paragraph.text for paragraph in doc.paragraphs])
31
+ os.remove("temp.docx")
32
+ return content
33
+ else:
34
+ raise ValueError(f"Unsupported file format: {file_extension}")
35
+ except Exception as e:
36
+ raise Exception(f"Error reading file {file.name}: {str(e)}")
37
+
38
+ # Rate Limiter Class
39
+ class RateLimiter:
40
+ def __init__(self, rpm_limit=2, tpm_limit=32000, rpd_limit=50):
41
+ self.rpm_limit = rpm_limit
42
+ self.tpm_limit = tpm_limit
43
+ self.rpd_limit = rpd_limit
44
+ self.requests = deque()
45
+ self.tokens = deque()
46
+ self.daily_requests = deque()
47
+
48
+ def can_make_request(self, token_count=0):
49
+ now = datetime.now()
50
+ while self.requests and (now - self.requests[0]) > timedelta(minutes=1):
51
+ self.requests.popleft()
52
+ while self.tokens and (now - self.tokens[0][0]) > timedelta(minutes=1):
53
+ self.tokens.popleft()
54
+ while self.daily_requests and (now - self.daily_requests[0]) > timedelta(days=1):
55
+ self.daily_requests.popleft()
56
+ if (len(self.requests) >= self.rpm_limit or
57
+ sum(tokens for _, tokens in self.tokens) + token_count > self.tpm_limit or
58
+ len(self.daily_requests) >= self.rpd_limit):
59
+ return False
60
+ return True
61
+
62
+ def add_request(self, token_count=0):
63
+ now = datetime.now()
64
+ self.requests.append(now)
65
+ self.tokens.append((now, token_count))
66
+ self.daily_requests.append(now)
67
+
68
+ def wait_time(self):
69
+ if not self.requests:
70
+ return 0
71
+ oldest_request = self.requests[0]
72
+ return max(0, 60 - (datetime.now() - oldest_request).total_seconds())
73
+
74
+ # Interview Analyzer Class
75
+ class InterviewAnalyzer:
76
+ def __init__(self, api_key: str):
77
+ """Initialize the analyzer with API key and setup logging"""
78
+ self.setup_logging()
79
+ self.setup_gemini(api_key)
80
+ self.rate_limiter = RateLimiter()
81
+
82
+ def setup_logging(self):
83
+ """Setup logging configuration"""
84
+ log_dir = Path("logs")
85
+ log_dir.mkdir(exist_ok=True)
86
+ logging.basicConfig(
87
+ level=logging.INFO,
88
+ format='%(asctime)s - %(levelname)s - %(message)s',
89
+ handlers=[
90
+ logging.FileHandler(log_dir / f'analysis_{datetime.now().strftime("%Y%m%d_%H%M%S")}.log'),
91
+ logging.StreamHandler()
92
+ ]
93
+ )
94
+ self.logger = logging.getLogger(__name__)
95
+
96
+ def setup_gemini(self, api_key: str):
97
+ """Setup Gemini API"""
98
+ try:
99
+ genai.configure(api_key=api_key)
100
+ self.model = genai.GenerativeModel("gemini-1.5-pro")
101
+ self.logger.info("Gemini API initialized successfully")
102
+ except Exception as e:
103
+ self.logger.error(f"Failed to initialize Gemini API: {str(e)}")
104
+ raise
105
+
106
+ def extract_interview_data(self, content: str, filename: str) -> Dict[str, str]:
107
+ """Extract relevant information from the document content"""
108
+ # Extract name from content using regex
109
+ name_match = re.search(r'Interview\s*[-–—]\s*([^\n]+)', content)
110
+ name = name_match.group(1).strip() if name_match else "Unknown"
111
+ # Clean the content
112
+ cleaned_content = self.clean_text(content)
113
+ return {
114
+ 'name': name,
115
+ 'content': cleaned_content,
116
+ 'source': filename
117
+ }
118
+
119
+ def clean_text(self, text: str) -> str:
120
+ """Clean and normalize text content"""
121
+ # Remove special characters and normalize whitespace
122
+ text = re.sub(r'\\n', '\n', text)
123
+ text = re.sub(r'\s+', ' ', text)
124
+ text = re.sub(r'\*\*', '', text) # Remove markdown-style formatting
125
+ return text.strip()
126
+
127
+ def clean_gemini_response(self, response_text: str) -> str:
128
+ """Clean and format Gemini API response to ensure valid JSON"""
129
+ try:
130
+ # Use regex to find the first JSON object
131
+ match = re.search(r'({.*})', response_text, re.DOTALL)
132
+ if not match:
133
+ self.logger.error("No JSON structure found in response")
134
+ return None
135
+
136
+ json_str = match.group(1)
137
+
138
+ # Remove any Markdown or code block indicators
139
+ json_str = re.sub(r'```json\s*', '', json_str)
140
+ json_str = re.sub(r'```\s*', '', json_str)
141
+
142
+ # Validate JSON structure
143
+ json.loads(json_str)
144
+ return json_str
145
+ except json.JSONDecodeError:
146
+ self.logger.error("Invalid JSON structure after cleaning")
147
+ return None
148
+ except Exception as e:
149
+ self.logger.error(f"Error cleaning response: {str(e)}")
150
+ return None
151
+
152
+ def create_analysis_prompt(self, interview_data: Dict[str, str]) -> str:
153
+ """Create a structured prompt for analysis"""
154
+ return f"""
155
+ You are a career analysis expert. Analyze this interview transcript for {interview_data['name']}
156
+ regarding their career path prediction (Academia vs Industry).
157
+ Provide your analysis in the following JSON format ONLY, without any additional text or markdown:
158
+
159
+ {{
160
+ "interviewee": {{
161
+ "name": "{interview_data['name']}",
162
+ "source_document": "{interview_data['source']}"
163
+ }},
164
+ "sentiment_analysis": {{
165
+ "academia": {{
166
+ "research": {{"score": 0.0, "quotes": []}},
167
+ "teaching": {{"score": 0.0, "quotes": []}},
168
+ "publication": {{"score": 0.0, "quotes": []}},
169
+ "grant_writing": {{"score": 0.0, "quotes": []}},
170
+ "mentoring": {{"score": 0.0, "quotes": []}},
171
+ "work_life_balance": {{"score": 0.0, "quotes": []}},
172
+ "collaboration": {{"score": 0.0, "quotes": []}}
173
+ }},
174
+ "industry": {{
175
+ "product_development": {{"score": 0.0, "quotes": []}},
176
+ "business_strategy": {{"score": 0.0, "quotes": []}},
177
+ "management": {{"score": 0.0, "quotes": []}},
178
+ "work_life_balance": {{"score": 0.0, "quotes": []}},
179
+ "financial_rewards": {{"score": 0.0, "quotes": []}}
180
+ }}
181
+ }},
182
+ "keyword_analysis": {{
183
+ "academia": [],
184
+ "industry": []
185
+ }},
186
+ "themes": [],
187
+ "motivations": {{
188
+ "primary": [],
189
+ "intrinsic": [],
190
+ "extrinsic": [],
191
+ "evidence_quotes": []
192
+ }},
193
+ "risk_assessment": {{
194
+ "level": "",
195
+ "description": "",
196
+ "supporting_quotes": []
197
+ }},
198
+ "long_term_goals": {{
199
+ "vision": "",
200
+ "alignment": "",
201
+ "supporting_quotes": []
202
+ }},
203
+ "career_prediction": {{
204
+ "path": "",
205
+ "confidence": 0,
206
+ "rationale": ""
207
+ }}
208
+ }}
209
+
210
+ Here is the interview transcript to analyze:
211
+
212
+ {interview_data['content']}
213
+
214
+ Analyze the transcript and fill in the JSON structure with your findings. Ensure all scores are between -1.0 and 1.0,
215
+ confidence is between 1 and 10, and include relevant quotes from the text to support your analysis.
216
+ Return ONLY the JSON structure without any additional text or formatting.
217
+ """
218
+
219
+ def analyze_transcript(self, interview_data: Dict[str, str]) -> Dict:
220
+ """Analyze a single transcript using Gemini API with rate limiting"""
221
+ try:
222
+ prompt = self.create_analysis_prompt(interview_data)
223
+ estimated_tokens = len(prompt.split()) # rough estimation
224
+
225
+ # Check rate limits
226
+ while not self.rate_limiter.can_make_request(estimated_tokens):
227
+ wait_time = self.rate_limiter.wait_time()
228
+ st.warning(f"Rate limit reached. Waiting {wait_time:.1f} seconds...")
229
+ time.sleep(wait_time + 1) # Add 1 second buffer
230
+
231
+ # Make the API call
232
+ st.info("Making API request...")
233
+ response = self.model.generate_content(prompt)
234
+ self.rate_limiter.add_request(estimated_tokens)
235
+
236
+ if not response or not response.text:
237
+ st.error("Received empty response from API")
238
+ return None
239
+
240
+ # Clean and parse the response
241
+ cleaned_response = self.clean_gemini_response(response.text)
242
+ if not cleaned_response:
243
+ st.error("Failed to extract valid JSON from API response")
244
+ return None
245
+
246
+ # Parse the cleaned JSON
247
+ analysis = json.loads(cleaned_response)
248
+ analysis['metadata'] = {
249
+ 'analysis_timestamp': datetime.now().isoformat(),
250
+ 'source_document': interview_data['source']
251
+ }
252
+ return analysis
253
+ except Exception as e:
254
+ st.error(f"Analysis Error: {str(e)}")
255
+ self.logger.error(f"Error analyzing transcript for {interview_data['name']}: {str(e)}")
256
+ return None
257
+
258
+ def _validate_analysis_structure(self, analysis: Dict, interview_data: Dict) -> Dict:
259
+ """Validate and fix analysis structure if needed"""
260
+ template = {
261
+ "interviewee": {
262
+ "name": interview_data['name'],
263
+ "source_document": interview_data['source']
264
+ },
265
+ "sentiment_analysis": {
266
+ "academia": {},
267
+ "industry": {}
268
+ },
269
+ "keyword_analysis": {
270
+ "academia": [],
271
+ "industry": []
272
+ },
273
+ "themes": [],
274
+ "motivations": {
275
+ "primary": [],
276
+ "intrinsic": [],
277
+ "extrinsic": [],
278
+ "evidence_quotes": []
279
+ },
280
+ "risk_assessment": {
281
+ "level": "",
282
+ "description": "",
283
+ "supporting_quotes": []
284
+ },
285
+ "long_term_goals": {
286
+ "vision": "",
287
+ "alignment": "",
288
+ "supporting_quotes": []
289
+ },
290
+ "career_prediction": {
291
+ "path": "",
292
+ "confidence": 0,
293
+ "rationale": ""
294
+ },
295
+ "metadata": {
296
+ "analysis_timestamp": datetime.now().isoformat(),
297
+ "source_document": interview_data['source']
298
+ }
299
+ }
300
+
301
+ for key, value in template.items():
302
+ if key not in analysis:
303
+ analysis[key] = value
304
+ return analysis
305
+
306
+ def display_analysis_results(self, analysis: Dict):
307
+ """Display analysis results in Streamlit with enhanced visualizations"""
308
+ try:
309
+ st.subheader(f"Analysis for {analysis.get('interviewee', {}).get('name', 'Unknown')}")
310
+ col1, col2 = st.columns(2)
311
+
312
+ with col1:
313
+ st.write("#### Career Prediction")
314
+ prediction = analysis.get('career_prediction', {})
315
+ st.write(f"**Predicted Path:** {prediction.get('path', 'Unknown')}")
316
+ st.write(f"**Confidence:** {prediction.get('confidence', 0)}/10")
317
+ st.write("**Rationale:**", prediction.get('rationale', 'Not available'))
318
+
319
+ with col2:
320
+ st.write("#### Sentiment Analysis")
321
+ sentiment = analysis.get('sentiment_analysis', {})
322
+ st.write(f"**Academia:** {self._safe_calculate_sentiment(sentiment.get('academia', {}))}")
323
+ st.write(f"**Industry:** {self._safe_calculate_sentiment(sentiment.get('industry', {}))}")
324
+
325
+ # Radar Chart
326
+ st.write("### Sentiment Analysis Radar Chart")
327
+ radar_fig = self.create_radar_chart(analysis)
328
+ st.plotly_chart(radar_fig)
329
+
330
+ # Bar Chart
331
+ st.write("### Sentiment Scores Bar Chart")
332
+ bar_fig = self.create_bar_chart(analysis)
333
+ st.plotly_chart(bar_fig)
334
+
335
+ # Pie Chart for Motivations
336
+ st.write("### Motivations Breakdown")
337
+ motivations = analysis.get('motivations', {})
338
+ pie_fig = self.create_pie_chart({
339
+ 'Primary': motivations.get('primary', []),
340
+ 'Intrinsic': motivations.get('intrinsic', []),
341
+ 'Extrinsic': motivations.get('extrinsic', [])
342
+ }, "Motivations Distribution")
343
+ st.plotly_chart(pie_fig)
344
+
345
+ # Themes
346
+ st.write("### Key Themes")
347
+ themes = analysis.get('themes', [])
348
+ if isinstance(themes, list):
349
+ for theme in themes:
350
+ if isinstance(theme, dict):
351
+ st.write(f"- **{theme.get('name', '')}:** {theme.get('description', '')}")
352
+
353
+ # Motivations Details
354
+ st.write("### Motivations")
355
+ motivations = analysis.get('motivations', {})
356
+ st.write(f"**Primary:** {', '.join(motivations.get('primary', []))}")
357
+ st.write(f"**Intrinsic:** {', '.join(motivations.get('intrinsic', []))}")
358
+ st.write(f"**Extrinsic:** {', '.join(motivations.get('extrinsic', []))}")
359
+
360
+ # Risk Assessment
361
+ st.write("### Risk Assessment")
362
+ risk = analysis.get('risk_assessment', {})
363
+ st.write(f"**Level:** {risk.get('level', 'Unknown')}")
364
+ st.write(risk.get('description', 'Not available'))
365
+
366
+ # Long Term Goals
367
+ st.write("### Long Term Goals")
368
+ goals = analysis.get('long_term_goals', {})
369
+ st.write(f"**Vision:** {goals.get('vision', '')}")
370
+ st.write(f"**Alignment:** {goals.get('alignment', '')}")
371
+
372
+ except Exception as e:
373
+ st.error(f"Error displaying analysis: {str(e)}")
374
+
375
+ def generate_summary_table(self, analyses: List[Dict]) -> pd.DataFrame:
376
+ """Generate a summary table from all analyses"""
377
+ summary_data = []
378
+ for analysis in analyses:
379
+ if not analysis:
380
+ continue
381
+ try:
382
+ themes = []
383
+ if isinstance(analysis.get('themes', []), list):
384
+ themes = [theme.get('name', '') for theme in analysis['themes'] if isinstance(theme, dict)]
385
+ motivations = []
386
+ if isinstance(analysis.get('motivations', {}), dict):
387
+ motivations = analysis['motivations'].get('primary', [])
388
+ summary_row = {
389
+ 'Name': analysis.get('interviewee', {}).get('name', 'Unknown'),
390
+ 'Predicted Career Path': analysis.get('career_prediction', {}).get('path', 'Unknown'),
391
+ 'Confidence Score': analysis.get('career_prediction', {}).get('confidence', 0),
392
+ 'Primary Motivations': ', '.join(motivations) if isinstance(motivations, list) else '',
393
+ 'Risk Level': analysis.get('risk_assessment', {}).get('level', 'Unknown'),
394
+ 'Key Themes': ', '.join(themes),
395
+ 'Academia Sentiment': self._safe_calculate_sentiment(analysis.get('sentiment_analysis', {}).get('academia', {})),
396
+ 'Industry Sentiment': self._safe_calculate_sentiment(analysis.get('sentiment_analysis', {}).get('industry', {})),
397
+ 'Source Document': analysis.get('metadata', {}).get('source_document', 'Unknown')
398
+ }
399
+ summary_data.append(summary_row)
400
+ except Exception as e:
401
+ self.logger.error(f"Error processing analysis for summary: {str(e)}")
402
+ st.error(f"Error processing analysis: {str(e)}")
403
+ continue
404
+ return pd.DataFrame(summary_data)
405
+
406
+ def _safe_calculate_sentiment(self, sentiment_dict: Dict) -> float:
407
+ """Safely calculate average sentiment score from a sentiment dictionary"""
408
+ try:
409
+ if not isinstance(sentiment_dict, dict):
410
+ return 0.0
411
+ scores = []
412
+ for item in sentiment_dict.values():
413
+ if isinstance(item, dict) and 'score' in item:
414
+ try:
415
+ scores.append(float(item['score']))
416
+ except (ValueError, TypeError):
417
+ continue
418
+ return round(sum(scores) / len(scores), 2) if scores else 0.0
419
+ except Exception:
420
+ return 0.0
421
+
422
+ # Visualization Methods
423
+ def create_radar_chart(self, analysis: Dict) -> go.Figure:
424
+ """Create a radar chart for sentiment analysis"""
425
+ try:
426
+ academia_scores = analysis.get('sentiment_analysis', {}).get('academia', {})
427
+ industry_scores = analysis.get('sentiment_analysis', {}).get('industry', {})
428
+
429
+ # Define separate categories for academia and industry
430
+ academia_categories = list(academia_scores.keys())
431
+ industry_categories = list(industry_scores.keys())
432
+
433
+ # Combine categories for consistent radar chart
434
+ categories = sorted(list(set(academia_categories + industry_categories)))
435
+
436
+ # Prepare values, aligning categories
437
+ academia_values = [academia_scores.get(cat, {}).get('score', 0.0) for cat in categories]
438
+ industry_values = [industry_scores.get(cat, {}).get('score', 0.0) for cat in categories]
439
+
440
+ # Create radar chart
441
+ fig = go.Figure()
442
+
443
+ fig.add_trace(go.Scatterpolar(
444
+ r=academia_values,
445
+ theta=categories,
446
+ fill='toself',
447
+ name='Academia'
448
+ ))
449
+ fig.add_trace(go.Scatterpolar(
450
+ r=industry_values,
451
+ theta=categories,
452
+ fill='toself',
453
+ name='Industry'
454
+ ))
455
+
456
+ fig.update_layout(
457
+ polar=dict(
458
+ radialaxis=dict(
459
+ visible=True,
460
+ range=[-1, 1]
461
+ )
462
+ ),
463
+ showlegend=True,
464
+ title=f"Sentiment Analysis Radar for {analysis['interviewee']['name']}"
465
+ )
466
+ return fig
467
+ except Exception as e:
468
+ self.logger.error(f"Error creating radar chart: {str(e)}")
469
+ return go.Figure()
470
+
471
+ def create_bar_chart(self, analysis: Dict) -> go.Figure:
472
+ """Create a bar chart for sentiment scores"""
473
+ try:
474
+ academia_scores = analysis.get('sentiment_analysis', {}).get('academia', {})
475
+ industry_scores = analysis.get('sentiment_analysis', {}).get('industry', {})
476
+
477
+ # Define categories for academia and industry
478
+ academia_categories = list(academia_scores.keys())
479
+ industry_categories = list(industry_scores.keys())
480
+
481
+ # Create separate dataframes
482
+ df_academia = pd.DataFrame({
483
+ 'Category': academia_categories,
484
+ 'Sentiment': [academia_scores[cat]['score'] for cat in academia_categories],
485
+ 'Sector': ['Academia'] * len(academia_categories)
486
+ })
487
+
488
+ df_industry = pd.DataFrame({
489
+ 'Category': industry_categories,
490
+ 'Sentiment': [industry_scores[cat]['score'] for cat in industry_categories],
491
+ 'Sector': ['Industry'] * len(industry_categories)
492
+ })
493
+
494
+ # Combine dataframes
495
+ df = pd.concat([df_academia, df_industry], ignore_index=True)
496
+
497
+ fig = px.bar(df, x='Category', y='Sentiment', color='Sector', barmode='group',
498
+ title=f"Sentiment Scores for {analysis['interviewee']['name']}",
499
+ range_y=[-1, 1])
500
+
501
+ return fig
502
+ except Exception as e:
503
+ self.logger.error(f"Error creating bar chart: {str(e)}")
504
+ return go.Figure()
505
+
506
+ def create_pie_chart(self, motivations: Dict[str, List[str]], title: str) -> go.Figure:
507
+ """Create a pie chart for motivations breakdown"""
508
+ try:
509
+ labels = list(motivations.keys())
510
+ values = [len(v) for v in motivations.values()]
511
+ fig = px.pie(names=labels, values=values, title=title)
512
+ return fig
513
+ except Exception as e:
514
+ self.logger.error(f"Error creating pie chart: {str(e)}")
515
+ return go.Figure()
516
+
517
+ # Additional Features
518
+ def comparative_dashboard(self):
519
+ """Comparative Analysis Dashboard"""
520
+ st.header("Comparative Analysis")
521
+ selected = st.multiselect("Select Interviewees to Compare",
522
+ options=[a['interviewee']['name'] for a in st.session_state.analyses])
523
+
524
+ if len(selected) >= 2:
525
+ selected_analyses = [a for a in st.session_state.analyses if a['interviewee']['name'] in selected]
526
+ fig = go.Figure()
527
+
528
+ for analysis in selected_analyses:
529
+ academia_scores = analysis.get('sentiment_analysis', {}).get('academia', {})
530
+ industry_scores = analysis.get('sentiment_analysis', {}).get('industry', {})
531
+
532
+ academia_categories = list(academia_scores.keys())
533
+ industry_categories = list(industry_scores.keys())
534
+ categories = sorted(list(set(academia_categories + industry_categories)))
535
+
536
+ academia_values = [academia_scores.get(cat, {}).get('score', 0.0) for cat in categories]
537
+ industry_values = [industry_scores.get(cat, {}).get('score', 0.0) for cat in categories]
538
+
539
+ # Academia Trace
540
+ fig.add_trace(go.Scatterpolar(
541
+ r=academia_values,
542
+ theta=categories,
543
+ fill='toself',
544
+ name=f"{analysis['interviewee']['name']} - Academia"
545
+ ))
546
+
547
+ # Industry Trace
548
+ fig.add_trace(go.Scatterpolar(
549
+ r=industry_values,
550
+ theta=categories,
551
+ fill='toself',
552
+ name=f"{analysis['interviewee']['name']} - Industry"
553
+ ))
554
+
555
+ fig.update_layout(
556
+ polar=dict(
557
+ radialaxis=dict(
558
+ visible=True,
559
+ range=[-1, 1]
560
+ )
561
+ ),
562
+ showlegend=True,
563
+ title="Comparative Sentiment Analysis Radar Chart"
564
+ )
565
+ st.plotly_chart(fig)
566
+ elif len(selected) == 1:
567
+ st.info("Select at least two interviewees to compare sentiment analyses.")
568
+
569
+ def clear_chat(self):
570
+ """Clear the chat history"""
571
+ st.session_state.chat_history = []
572
+
573
+ def chat_interface(self):
574
+ """Chat section to ask questions about uploaded files"""
575
+ st.header("πŸ“’ Chat with Analysis")
576
+
577
+ if 'chat_history' not in st.session_state:
578
+ st.session_state.chat_history = []
579
+
580
+ # Display chat history
581
+ for chat in st.session_state.chat_history:
582
+ if chat['role'] == 'user':
583
+ st.markdown(f"**You:** {chat['content']}")
584
+ else:
585
+ st.markdown(f"**Assistant:** {chat['content']}")
586
+
587
+ # User input
588
+ user_input = st.text_input("Ask a question about your uploaded files:")
589
+
590
+ if st.button("Send") and user_input:
591
+ st.session_state.chat_history.append({"role": "user", "content": user_input})
592
+ response = self.handle_chat(user_input)
593
+ st.session_state.chat_history.append({"role": "assistant", "content": response})
594
+
595
+ def handle_chat(self, question: str) -> str:
596
+ """Handle user questions by sending them to the Gemini API with context"""
597
+ try:
598
+ # Compile all analyses into a summary
599
+ summary = ""
600
+ for analysis in st.session_state.analyses:
601
+ summary += f"Interviewee: {analysis.get('interviewee', {}).get('name', 'Unknown')}\n"
602
+ summary += json.dumps(analysis, indent=2) + "\n\n"
603
+
604
+ # Create prompt for Gemini
605
+ prompt = f"""
606
+ You are an assistant specialized in analyzing interview data. Based on the following analysis summaries, answer the user's question concisely. Provide your answer in JSON format only, without any additional text or explanations.
607
+
608
+ Analysis Summaries:
609
+ {summary}
610
+
611
+ User Question: {question}
612
+
613
+ JSON Response:
614
+ """
615
+
616
+ # Rate limiting
617
+ estimated_tokens = len(prompt.split())
618
+ while not self.rate_limiter.can_make_request(estimated_tokens):
619
+ wait_time = self.rate_limiter.wait_time()
620
+ st.warning(f"Rate limit reached. Waiting {wait_time:.1f} seconds before answering your question...")
621
+ time.sleep(wait_time + 1) # Add 1 second buffer
622
+
623
+ # Make API call
624
+ response = self.model.generate_content(prompt)
625
+ self.rate_limiter.add_request(estimated_tokens)
626
+
627
+ if not response or not response.text:
628
+ return "I'm sorry, I couldn't retrieve an answer at this time."
629
+
630
+ # Clean and parse the response
631
+ cleaned_response = self.clean_gemini_response(response.text)
632
+ if not cleaned_response:
633
+ self.logger.debug(f"Raw API response: {response.text}")
634
+ return "I'm sorry, I couldn't understand the response from the analysis."
635
+
636
+ return cleaned_response
637
+ except Exception as e:
638
+ self.logger.error(f"Chat Error: {str(e)}")
639
+ return "An error occurred while processing your request."
640
+ # Main Application
641
+ def main():
642
+ st.set_page_config(
643
+ page_title="Interview Analysis App",
644
+ layout="wide",
645
+ initial_sidebar_state="expanded"
646
+ )
647
+
648
+ st.title("πŸ“ Interview Analysis App")
649
+ st.markdown("""
650
+ This app analyzes interview transcripts to predict career paths and provide detailed insights.
651
+ Upload your interview transcripts to get started.
652
+ """)
653
+
654
+ # Sidebar Configuration
655
+ with st.sidebar:
656
+ st.header("πŸ”§ Configuration")
657
+ api_key = st.text_input(
658
+ "Enter your Gemini API Key",
659
+ type="password",
660
+ help="Your Gemini API key is required for analysis"
661
+ )
662
+
663
+ st.subheader("βš™οΈ Analysis Settings")
664
+ show_details = st.checkbox("Show detailed analysis", value=True)
665
+ confidence_threshold = st.slider(
666
+ "Confidence Threshold",
667
+ min_value=1,
668
+ max_value=10,
669
+ value=5,
670
+ help="Minimum confidence score for predictions"
671
+ )
672
+
673
+ st.markdown("""
674
+ ### πŸ“ˆ API Rate Limits
675
+ - **2 requests per minute**
676
+ - **32,000 tokens per minute**
677
+ - **50 requests per day**
678
+ """)
679
+
680
+ if not api_key:
681
+ st.warning("⚠️ Please enter your Gemini API Key in the sidebar to continue")
682
+ st.stop()
683
+
684
+ # Initialize session state
685
+ if 'analyses' not in st.session_state:
686
+ st.session_state.analyses = []
687
+ if 'processed_files' not in st.session_state:
688
+ st.session_state.processed_files = set()
689
+ if 'rate_limiter' not in st.session_state:
690
+ st.session_state.rate_limiter = RateLimiter()
691
+ if 'chat_history' not in st.session_state:
692
+ st.session_state.chat_history = []
693
+
694
+ try:
695
+ analyzer = InterviewAnalyzer(api_key)
696
+ st.header("πŸ“€ Upload Interview Transcripts")
697
+ uploaded_files = st.file_uploader(
698
+ "Upload your interview transcripts",
699
+ accept_multiple_files=True,
700
+ type=['txt', 'docx'],
701
+ help="Supported formats: TXT, DOCX"
702
+ )
703
+
704
+ if uploaded_files:
705
+ for file in uploaded_files:
706
+ file_id = f"{file.name}_{file.size}"
707
+ if file_id in st.session_state.processed_files:
708
+ st.info(f"βœ… {file.name} has already been processed.")
709
+ continue
710
+ with st.expander(f"Processing {file.name}", expanded=True):
711
+ try:
712
+ st.info(f"πŸ“„ Reading file: {file.name}")
713
+ content = read_file_content(file)
714
+ interview_data = analyzer.extract_interview_data(content, file.name)
715
+ st.write(f"πŸ” Analyzing interview for: {interview_data['name']}")
716
+ with st.spinner('Analyzing interview...'):
717
+ analysis = analyzer.analyze_transcript(interview_data)
718
+ if analysis:
719
+ analyzer._validate_analysis_structure(analysis, interview_data)
720
+ st.session_state.analyses.append(analysis)
721
+ st.session_state.processed_files.add(file_id)
722
+ st.success(f"βœ… Analysis complete for {interview_data['name']}")
723
+ else:
724
+ st.error(f"❌ Failed to analyze {file.name}")
725
+ except Exception as e:
726
+ st.error(f"Error processing {file.name}: {str(e)}")
727
+ continue
728
+
729
+ if st.session_state.analyses:
730
+ st.header("πŸ“Š Analysis Results")
731
+ try:
732
+ summary_df = analyzer.generate_summary_table(st.session_state.analyses)
733
+ if confidence_threshold > 1:
734
+ summary_df = summary_df[summary_df['Confidence Score'] >= confidence_threshold]
735
+ st.subheader("πŸ“‹ Summary of Analyses")
736
+ st.dataframe(
737
+ summary_df,
738
+ use_container_width=True,
739
+ hide_index=True
740
+ )
741
+
742
+ # Download Options
743
+ st.subheader("πŸ’Ύ Download Results")
744
+ col1, col2 = st.columns(2)
745
+ with col1:
746
+ json_str = json.dumps(st.session_state.analyses, indent=2)
747
+ st.download_button(
748
+ label="πŸ“₯ Download Detailed Analysis (JSON)",
749
+ data=json_str,
750
+ file_name=f"interview_analysis_{datetime.now().strftime('%Y%m%d_%H%M%S')}.json",
751
+ mime="application/json",
752
+ help="Download the complete analysis results in JSON format"
753
+ )
754
+ with col2:
755
+ csv = summary_df.to_csv(index=False)
756
+ st.download_button(
757
+ label="πŸ“₯ Download Summary (CSV)",
758
+ data=csv,
759
+ file_name=f"analysis_summary_{datetime.now().strftime('%Y%m%d_%H%M%S')}.csv",
760
+ mime="text/csv",
761
+ help="Download the summary table in CSV format"
762
+ )
763
+
764
+ # Detailed Analysis with Visualizations
765
+ if show_details:
766
+ st.header("πŸ” Detailed Analysis")
767
+ for idx, analysis in enumerate(st.session_state.analyses, 1):
768
+ with st.expander(
769
+ f"πŸ“Š Analysis {idx}: {analysis.get('interviewee', {}).get('name', 'Unknown')}",
770
+ expanded=False
771
+ ):
772
+ analyzer.display_analysis_results(analysis)
773
+
774
+ # Comparative Analysis Dashboard
775
+ analyzer.comparative_dashboard()
776
+
777
+ # Chat Interface
778
+ analyzer.chat_interface()
779
+
780
+ # Clear Results Button
781
+ if st.button("πŸ—‘οΈ Clear All Results and Chat History"):
782
+ st.session_state.analyses = []
783
+ st.session_state.processed_files = set()
784
+ st.session_state.rate_limiter = RateLimiter()
785
+ st.session_state.chat_history = []
786
+ st.experimental_rerun()
787
+
788
+ # Instructions
789
+ st.header("ℹ️ Instructions")
790
+ st.markdown("""
791
+ ### How to Use:
792
+
793
+ 1. **Enter your Gemini API key** in the sidebar.
794
+ 2. **Upload** one or more interview transcript files (`.txt` or `.docx`).
795
+ 3. **Wait** for the analysis to complete.
796
+ 4. **View** results, explore detailed analyses, and download reports.
797
+ 5. **Use the chat section** to ask specific questions about your uploaded files.
798
+
799
+ **Supported file formats:** `.txt`, `.docx`
800
+ """)
801
+
802
+ except Exception as e:
803
+ st.error("Error displaying results")
804
+ st.exception(e)
805
+ else:
806
+ st.info("πŸ‘† Upload your interview transcripts to begin analysis")
807
+
808
+ except Exception as e:
809
+ st.error("Critical Error")
810
+ st.exception(e)
811
+ st.warning("Please refresh the page and try again")
812
+
813
+ if __name__ == "__main__":
814
+ main()
requirements.txt ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ google-generativeai
2
+ pandas
3
+ typing
4
+ datetime
5
+ logging
6
+ pathlib
7
+ streamlit
8
+ python-docx
9
+ plotly-express