Spaces:
Runtime error
Runtime error
Thiwanka01
commited on
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import pipeline
|
3 |
+
import torch
|
4 |
+
from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
|
5 |
+
|
6 |
+
# Load pretrained models from Hugging Face
|
7 |
+
nlp_pipeline = pipeline("text-generation", model="gpt2") # For text generation (voice assistant)
|
8 |
+
speech_recognition_model = Wav2Vec2ForCTC.from_pretrained("facebook/wav2vec2-large-960h")
|
9 |
+
speech_recognition_processor = Wav2Vec2Processor.from_pretrained("facebook/wav2vec2-large-960h")
|
10 |
+
|
11 |
+
# Function for voice-to-text conversion using Wav2Vec2
|
12 |
+
def recognize_speech(audio):
|
13 |
+
# Process the audio file
|
14 |
+
input_values = speech_recognition_processor(audio, return_tensors="pt").input_values
|
15 |
+
with torch.no_grad():
|
16 |
+
logits = speech_recognition_model(input_values).logits
|
17 |
+
predicted_ids = torch.argmax(logits, dim=-1)
|
18 |
+
# Decode the prediction
|
19 |
+
transcription = speech_recognition_processor.decode(predicted_ids[0])
|
20 |
+
return transcription
|
21 |
+
|
22 |
+
# Function for generating device commands using GPT-2 (e.g., for controlling smart devices)
|
23 |
+
def generate_response(user_input):
|
24 |
+
response = nlp_pipeline(user_input, max_length=50, num_return_sequences=1)[0]['generated_text']
|
25 |
+
return response
|
26 |
+
|
27 |
+
# Gradio Interface
|
28 |
+
def interact_with_system(audio=None, user_input=None):
|
29 |
+
if audio:
|
30 |
+
# Convert speech to text
|
31 |
+
transcription = recognize_speech(audio)
|
32 |
+
return transcription
|
33 |
+
elif user_input:
|
34 |
+
# Generate response to control devices
|
35 |
+
response = generate_response(user_input)
|
36 |
+
return response
|
37 |
+
else:
|
38 |
+
return "Please provide either voice or text input."
|
39 |
+
|
40 |
+
# Create a Gradio interface
|
41 |
+
interface = gr.Interface(
|
42 |
+
fn=interact_with_system,
|
43 |
+
inputs=[
|
44 |
+
gr.Audio(source="microphone", type="numpy", label="Voice Command"), # Voice input
|
45 |
+
gr.Textbox(label="Text Command") # Text input
|
46 |
+
],
|
47 |
+
outputs="text", # Output the text response (device control command)
|
48 |
+
title="AI-Driven Consumer Device Ecosystem",
|
49 |
+
description="Use voice or text commands to interact with smart devices in your ecosystem."
|
50 |
+
)
|
51 |
+
|
52 |
+
# Launch the interface
|
53 |
+
interface.launch()
|