Spaces:
Sleeping
Sleeping
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() |