Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import pipeline,Conversation
|
| 2 |
+
import gradio as gr
|
| 3 |
+
|
| 4 |
+
pipe = pipeline(
|
| 5 |
+
"automatic-speech-recognition",
|
| 6 |
+
model="openai/whisper-base",
|
| 7 |
+
)
|
| 8 |
+
|
| 9 |
+
model= pipeline("conversational", model="facebook/blenderbot-400M-distill")
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
# Get a response
|
| 14 |
+
|
| 15 |
+
# Initialize the conversational model
|
| 16 |
+
# conversational_model = pipeline("conversational", model="microsoft/DialoGPT-medium")
|
| 17 |
+
|
| 18 |
+
# Mock implementation of transcribe_speech for demonstration
|
| 19 |
+
def transcribe_speech(filepath):
|
| 20 |
+
output = pipe(
|
| 21 |
+
filepath,
|
| 22 |
+
max_new_tokens=256,
|
| 23 |
+
generate_kwargs={
|
| 24 |
+
"task": "transcribe",
|
| 25 |
+
"language": "english",
|
| 26 |
+
}, # update with the language you've fine-tuned on
|
| 27 |
+
chunk_length_s=30,
|
| 28 |
+
batch_size=8,
|
| 29 |
+
)
|
| 30 |
+
return output["text"]
|
| 31 |
+
|
| 32 |
+
def handle_audio_input(audio_file):
|
| 33 |
+
try:
|
| 34 |
+
# Step 1: Transcribe the audio
|
| 35 |
+
transcribed_text = transcribe_speech(audio_file)
|
| 36 |
+
print(f"Transcribed text: {transcribed_text}")
|
| 37 |
+
|
| 38 |
+
# Step 2: Create a conversation and generate a response from transcribed text
|
| 39 |
+
conversation = Conversation(transcribed_text)
|
| 40 |
+
response = model(conversation)
|
| 41 |
+
chatbot_response = response.generated_responses[-1]
|
| 42 |
+
print(f"Chatbot response: {chatbot_response}")
|
| 43 |
+
|
| 44 |
+
return transcribed_text, chatbot_response
|
| 45 |
+
except Exception as e:
|
| 46 |
+
print(f"Error: {e}")
|
| 47 |
+
return "Error in processing audio", str(e)
|
| 48 |
+
|
| 49 |
+
# Create the Gradio Blocks container
|
| 50 |
+
with gr.Blocks() as demo:
|
| 51 |
+
gr.Markdown("## Customer query audio to text chatbot")
|
| 52 |
+
|
| 53 |
+
with gr.Tab("Microphone"):
|
| 54 |
+
mic_transcribe = gr.Interface(
|
| 55 |
+
fn=handle_audio_input,
|
| 56 |
+
inputs=gr.Audio(sources="microphone", type="filepath"),
|
| 57 |
+
outputs=[gr.Textbox(label="Transcription"), gr.Textbox(label="Chatbot Response")],
|
| 58 |
+
)
|
| 59 |
+
mic_transcribe.render()
|
| 60 |
+
|
| 61 |
+
with gr.Tab("File Upload"):
|
| 62 |
+
file_transcribe = gr.Interface(
|
| 63 |
+
fn=handle_audio_input,
|
| 64 |
+
inputs=gr.Audio(sources="upload", type="filepath"),
|
| 65 |
+
outputs=[gr.Textbox(label="Transcription"), gr.Textbox(label="Chatbot Response")],
|
| 66 |
+
)
|
| 67 |
+
file_transcribe.render()
|
| 68 |
+
|
| 69 |
+
# Launch the Gradio app
|
| 70 |
+
demo.launch(share=True)
|