File size: 3,950 Bytes
3c02b94
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# import whisper
# import tempfile
# import os

# class AudioProcessor:
#     def __init__(self):
#         self.model = whisper.load_model("base")
    
#     def transcribe(self, audio_file):
#         with tempfile.NamedTemporaryFile(delete=False, suffix='.wav') as tmp:
#             tmp.write(audio_file.read())
#             tmp_path = tmp.name
        
#         try:
#             result = self.model.transcribe(tmp_path)
            
#             text = result['text']
#             segments = result.get('segments', [])
            
#             avg_confidence = 0.0
#             if segments:
#                 confidences = [s.get('no_speech_prob', 0) for s in segments]
#                 avg_confidence = 1 - (sum(confidences) / len(confidences))
#             else:
#                 avg_confidence = 0.8
            
#             return {
#                 'text': text,
#                 'confidence': avg_confidence,
#                 'needs_review': avg_confidence < 0.6
#             }
#         finally:
#             os.unlink(tmp_path)




# import whisper
# import tempfile
# import os
# import soundfile as sf
# import numpy as np

# class AudioProcessor:
#     def __init__(self):
#         self.model = whisper.load_model("base")
    
#     def transcribe(self, audio_file):
#         # Save uploaded file
#         with tempfile.NamedTemporaryFile(delete=False, suffix='.wav') as tmp:
#             tmp.write(audio_file.read())
#             tmp_path = tmp.name
        
#         try:
#             # Transcribe directly
#             result = self.model.transcribe(tmp_path, fp16=False)
            
#             text = result['text']
#             segments = result.get('segments', [])
            
#             avg_confidence = 0.8
#             if segments:
#                 confidences = [1 - s.get('no_speech_prob', 0) for s in segments]
#                 avg_confidence = sum(confidences) / len(confidences)
            
#             return {
#                 'text': text,
#                 'confidence': avg_confidence,
#                 'needs_review': avg_confidence < 0.6
#             }
#         except Exception as e:
#             # Fallback
#             return {
#                 'text': "Error transcribing audio. Please try again.",
#                 'confidence': 0.0,
#                 'needs_review': True
#             }
#         finally:
#             if os.path.exists(tmp_path):
#                 os.unlink(tmp_path)



import whisper
import os

class AudioProcessor:
    def __init__(self):
        try:
            self.model = whisper.load_model("tiny")  # Smaller, faster
        except:
            self.model = None
    
    def transcribe(self, audio_file):
        if self.model is None:
            return {
                'text': "",
                'confidence': 0.0,
                'needs_review': True
            }
        
        # Save to temp file
        temp_path = "temp_audio.wav"
        
        try:
            with open(temp_path, "wb") as f:
                f.write(audio_file.getvalue())
            
            # Transcribe
            result = self.model.transcribe(temp_path, language="en", fp16=False)
            
            text = result.get('text', '').strip()
            
            return {
                'text': text,
                'confidence': 0.8,
                'needs_review': len(text) < 5
            }
            
        except Exception as e:
            print(f"Transcription error: {e}")
            return {
                'text': "",
                'confidence': 0.0,
                'needs_review': True
            }
        finally:
            if os.path.exists(temp_path):
                try:
                    os.remove(temp_path)
                except:
                    pass