Spaces:
Sleeping
Sleeping
File size: 1,768 Bytes
00ae0ce 786ea23 00ae0ce 786ea23 cb9a254 786ea23 cb9a254 786ea23 8b1154e 786ea23 defc213 786ea23 cb9a254 786ea23 |
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 50 51 52 53 |
import gradio as gr
from transformers import pipeline
# Load Whisper for speech-to-text
whisper = pipeline("automatic-speech-recognition", model="openai/whisper-medium")
# Load a sentiment analysis model
sentiment_analyzer = pipeline("sentiment-analysis", model="cardiffnlp/twitter-roberta-base-sentiment")
# Function to process audio and analyze tone
def analyze_call(audio_file):
try:
# Step 1: Transcribe audio to text using Whisper
transcription = whisper(audio_file)["text"]
# Step 2: Analyze sentiment of the transcription
sentiment_result = sentiment_analyzer(transcription)[0]
# Prepare the output
output = {
"transcription": transcription,
"sentiment": sentiment_result["label"],
"confidence": round(sentiment_result["score"], 4)
}
return output
except Exception as e:
return {"error": str(e)}
# Gradio Interface
def gradio_interface(audio):
if audio is None:
return "Please record or upload an audio file."
result = analyze_call(audio)
if "error" in result:
return f"Error: {result['error']}"
return (
f"**Transcription:** {result['transcription']}\n\n"
f"**Sentiment:** {result['sentiment']}\n\n"
f"**Confidence:** {result['confidence']}"
)
# Create Gradio app
interface = gr.Interface(
fn=gradio_interface,
inputs=gr.Audio(type="filepath", label="Record or Upload Audio"),
outputs=gr.Textbox(label="Analysis Result", lines=5),
title="Real-Time Call Analysis",
description="Record or upload audio to analyze tone and sentiment in real time.",
live=False # Set to False to avoid constant re-runs
)
# Launch the app
interface.launch() |