Spaces:
Sleeping
Sleeping
Commit
·
0c3c269
1
Parent(s):
cbe6726
Fix: Load CPU-safe model for HF Space
Browse files
app.py
CHANGED
@@ -6,6 +6,7 @@ from peft import PeftModel
|
|
6 |
from pydub import AudioSegment
|
7 |
import speech_recognition as sr
|
8 |
import io
|
|
|
9 |
|
10 |
# Load model and tokenizer from local fine-tuned directory
|
11 |
# Define base and adapter model paths
|
@@ -103,27 +104,53 @@ elif input_mode == "Text Phrase":
|
|
103 |
except Exception as e:
|
104 |
st.error(f"Error: {e}")
|
105 |
|
106 |
-
elif input_mode == "Audio Upload":
|
107 |
-
uploaded_file = st.file_uploader("Upload audio file (WAV, MP3, M4A, MPEG)", type=["wav", "mp3", "m4a","mpeg"])
|
108 |
-
if uploaded_file:
|
109 |
-
st.audio(uploaded_file, format='audio/wav')
|
110 |
-
audio = AudioSegment.from_file(uploaded_file)
|
111 |
-
wav_io = io.BytesIO()
|
112 |
-
audio.export(wav_io, format="wav")
|
113 |
-
wav_io.seek(0)
|
114 |
-
|
115 |
-
recognizer = sr.Recognizer()
|
116 |
-
with sr.AudioFile(wav_io) as source:
|
117 |
-
audio_data = recognizer.record(source)
|
118 |
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
from pydub import AudioSegment
|
7 |
import speech_recognition as sr
|
8 |
import io
|
9 |
+
from audiorecorder import audiorecorder # Add at the top with other imports
|
10 |
|
11 |
# Load model and tokenizer from local fine-tuned directory
|
12 |
# Define base and adapter model paths
|
|
|
104 |
except Exception as e:
|
105 |
st.error(f"Error: {e}")
|
106 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
107 |
|
108 |
+
elif input_mode == "Audio Upload":
|
109 |
+
audio_input_mode = st.radio("Choose audio input type:", ["Upload Audio File", "Record Audio"])
|
110 |
+
|
111 |
+
if audio_input_mode == "Upload Audio File":
|
112 |
+
uploaded_file = st.file_uploader("Upload audio file (WAV, MP3, M4A, MPEG)", type=["wav", "mp3", "m4a", "mpeg"])
|
113 |
+
if uploaded_file:
|
114 |
+
st.audio(uploaded_file, format='audio/wav')
|
115 |
+
audio = AudioSegment.from_file(uploaded_file)
|
116 |
+
wav_io = io.BytesIO()
|
117 |
+
audio.export(wav_io, format="wav")
|
118 |
+
wav_io.seek(0)
|
119 |
+
|
120 |
+
recognizer = sr.Recognizer()
|
121 |
+
with sr.AudioFile(wav_io) as source:
|
122 |
+
audio_data = recognizer.record(source)
|
123 |
+
|
124 |
+
try:
|
125 |
+
text = recognizer.recognize_google(audio_data)
|
126 |
+
st.markdown(f"**Transcribed Text:** _{text}_")
|
127 |
+
values = extract_details_from_text(text)
|
128 |
+
if all(v is not None for v in values):
|
129 |
+
diagnosis = get_prediction(*values)
|
130 |
+
st.success(f"🩺 **Predicted Diagnosis:** {diagnosis}")
|
131 |
+
else:
|
132 |
+
st.warning("Could not extract complete information from audio.")
|
133 |
+
except Exception as e:
|
134 |
+
st.error(f"Audio processing error: {e}")
|
135 |
+
|
136 |
+
elif audio_input_mode == "Record Audio":
|
137 |
+
audio = audiorecorder("Click to record", "Recording...")
|
138 |
+
if len(audio) > 0:
|
139 |
+
st.audio(audio.tobytes(), format="audio/wav")
|
140 |
+
wav_io = io.BytesIO(audio.tobytes())
|
141 |
+
|
142 |
+
recognizer = sr.Recognizer()
|
143 |
+
with sr.AudioFile(wav_io) as source:
|
144 |
+
audio_data = recognizer.record(source)
|
145 |
+
|
146 |
+
try:
|
147 |
+
text = recognizer.recognize_google(audio_data)
|
148 |
+
st.markdown(f"**Transcribed Text:** _{text}_")
|
149 |
+
values = extract_details_from_text(text)
|
150 |
+
if all(v is not None for v in values):
|
151 |
+
diagnosis = get_prediction(*values)
|
152 |
+
st.success(f"🩺 **Predicted Diagnosis:** {diagnosis}")
|
153 |
+
else:
|
154 |
+
st.warning("Could not extract complete information from recorded audio.")
|
155 |
+
except Exception as e:
|
156 |
+
st.error(f"Recording processing error: {e}")
|