Spaces:
Runtime error
Runtime error
File size: 1,768 Bytes
71b2ec4 aa6e23f 71b2ec4 133ee24 71b2ec4 aa6e23f 133ee24 4c3d026 133ee24 71b2ec4 133ee24 71b2ec4 4222d8d 71b2ec4 4c3d026 133ee24 aa6e23f 71b2ec4 aa6e23f 71b2ec4 aa6e23f 71b2ec4 aa6e23f 71b2ec4 aa6e23f 71b2ec4 aa6e23f |
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 |
import gradio as gr
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline, WhisperProcessor, WhisperForConditionalGeneration
from gtts import gTTS
import os
class InteractiveChat:
def __init__(self):
self.whisper_processor = WhisperProcessor.from_pretrained("openai/whisper-large")
self.whisper_model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-large")
self.zephyr_tokenizer = AutoTokenizer.from_pretrained("HuggingFaceH4/zephyr-7b-beta")
self.zephyr_model = AutoModelForCausalLM.from_pretrained("HuggingFaceH4/zephyr-7b-beta", device_map="auto")
def generate_response(self, input_data):
input_features = self.whisper_processor(input_data)
predicted_ids = self.whisper_model.generate(input_features)
transcription = self.whisper_processor.batch_decode(predicted_ids)
response = self.get_zephyr_response(transcription)
self.speak(response)
return response
def get_zephyr_response(self, transcription):
zephyr_pipeline = pipeline("text-generation")
response = zephyr_pipeline(transcription)[0]["generated_text"]
return response
def speak(self, text):
tts = gTTS(text=text, lang='en')
tts.save("output.mp3")
os.system("mpg321 output.mp3")
# Create an instance of the InteractiveChat class
chat = InteractiveChat()
# Define a function that wraps the generate_response method
def generate_response_fn(input_data):
return chat.generate_response(input_data)
# Use the function in gr.Interface
interface = gr.Interface(
gr.Audio(type="filepath"), # Accept audio files
gr.Textbox(),
generate_response_fn # Pass the function here
)
interface.launch()
|