Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,61 +1,37 @@
|
|
| 1 |
-
import
|
| 2 |
-
import
|
| 3 |
-
import
|
| 4 |
-
import
|
| 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 |
-
speak("Speech service is unavailable. Please check your internet connection.")
|
| 39 |
-
return None
|
| 40 |
-
|
| 41 |
-
def authenticate():
|
| 42 |
-
"""Authenticate user based on voice input."""
|
| 43 |
-
speech_text = recognize_speech()
|
| 44 |
-
if not speech_text:
|
| 45 |
-
return False
|
| 46 |
-
|
| 47 |
-
# Extract username and password
|
| 48 |
-
for username, password in USER_CREDENTIALS.items():
|
| 49 |
-
if username in speech_text and password in speech_text:
|
| 50 |
-
speak(f"Welcome, {username}. You are now logged in.")
|
| 51 |
-
return True
|
| 52 |
-
|
| 53 |
-
speak("Authentication failed. Please try again.")
|
| 54 |
-
return False
|
| 55 |
-
|
| 56 |
-
if __name__ == "__main__":
|
| 57 |
-
speak("Welcome to the voice login system.")
|
| 58 |
-
if authenticate():
|
| 59 |
-
print("Login Successful!")
|
| 60 |
-
else:
|
| 61 |
-
print("Login Failed!")
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
|
| 3 |
+
import torch
|
| 4 |
+
import soundfile as sf
|
| 5 |
+
|
| 6 |
+
# Load the processor and model from Hugging Face
|
| 7 |
+
processor = Wav2Vec2Processor.from_pretrained("facebook/wav2vec2-large-xlsr-53")
|
| 8 |
+
model = Wav2Vec2ForCTC.from_pretrained("facebook/wav2vec2-large-xlsr-53")
|
| 9 |
+
|
| 10 |
+
def transcribe_audio(audio):
|
| 11 |
+
"""
|
| 12 |
+
Takes an audio file, processes it using Hugging Face Wav2Vec2 model,
|
| 13 |
+
and returns the transcribed text.
|
| 14 |
+
"""
|
| 15 |
+
# Read the audio file
|
| 16 |
+
audio_input, _ = sf.read(audio.name)
|
| 17 |
+
|
| 18 |
+
# Process audio input
|
| 19 |
+
input_values = processor(audio_input, return_tensors="pt").input_values
|
| 20 |
+
|
| 21 |
+
# Get model logits (raw prediction)
|
| 22 |
+
logits = model(input_values).logits
|
| 23 |
+
|
| 24 |
+
# Decode the prediction into text
|
| 25 |
+
predicted_ids = torch.argmax(logits, dim=-1)
|
| 26 |
+
transcription = processor.batch_decode(predicted_ids)
|
| 27 |
+
|
| 28 |
+
return transcription[0]
|
| 29 |
+
|
| 30 |
+
# Create a Gradio interface for users to upload audio files
|
| 31 |
+
iface = gr.Interface(fn=transcribe_audio,
|
| 32 |
+
inputs=gr.Audio(source="upload", type="file"),
|
| 33 |
+
outputs="text",
|
| 34 |
+
title="Voice Login System",
|
| 35 |
+
description="Upload an audio file for transcription using Wav2Vec2 model.")
|
| 36 |
+
|
| 37 |
+
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|