Spaces:
Sleeping
Sleeping
File size: 4,909 Bytes
221417c 452575b 221417c |
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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
import gradio as gr
import openai
import deepgram_sdk
import assemblyai
from transformers import pipeline
# Initialize sentiment analysis model
sentiment_analyzer = pipeline("text-classification", model="Vasanth/tamil-sentiment-distilbert")
# Placeholder transcription functions - Replace with your actual API implementation
def whisper_openai_transcribe(audio_file):
# Replace with actual Whisper API transcription code
transcription = "This is a dummy transcription from Whisper OpenAI API"
return transcription
def deepgram_transcribe(audio_file):
# Replace with actual Deepgram API transcription code
transcription = "This is a dummy transcription from Deepgram API"
return transcription
def assemblyai_transcribe(audio_file):
import assemblyai as aai
# Replace with your API key
aai.settings.api_key = "96206c6070cf4157b84f4f8eb66b5903"
# URL of the file to transcribe
#FILE_URL = "https://assemblyaiusercontent.com/playground/ECw2Ncu7btO.mp3"
#FILE_URL = "C:/lakshmi/AI usecases/tamil_audio1.mp3"
# You can also transcribe a local file by passing in a file path
# FILE_URL = './path/to/file.mp3'
# You can set additional parameters for the transcription
config = aai.TranscriptionConfig(
speech_model=aai.SpeechModel.nano,
language_detection=True
)
transcriber = aai.Transcriber(config=config)
transcript = transcriber.transcribe(audio_file)
if transcript.status == aai.TranscriptStatus.error:
print(transcript.error)
else:
print(transcript.text)
# Load a pre-trained sentiment analysis model for Tamil
#test_script = "இந்த செய்தி மிகவும் சோகம் மிகுந்தது.இந்த செய்தி நன்றாக உள்ளது."
"""from transformers import pipeline
sentiment_analyzer = pipeline("sentiment-analysis", model="nlptown/bert-base-multilingual-uncased-sentiment")
result = sentiment_analyzer(transcript.text)
print(result)
lines = test_script.split('.') # Split the transcript into lines
sentiment_results = []
for line in lines:
line = line.strip() # Remove leading/trailing whitespace
if line: # Only analyze non-empty lines
sentiment = sentiment_analyzer(line)
sentiment_results.append((line, sentiment))
print(sentiment_results)
# Write the Tamil text to a file
with open("tamil_text1.txt", "w", encoding="utf-8") as file:
file.write(transcript.text)
# Write the sentiment analysis results to a file
# Write the list of dictionaries in a human-readable format
with open("tamil_result.txt", 'w', encoding='utf-8') as file:
for result in sentiment_results:
file.write(f"Label: {result[0]}, Score: {result[1]}\n")
"""
return transcription
# Sentiment analysis function
def analyze_sentiment(text):
sentiment = sentiment_analyzer(text)
return sentiment[0]['label'], sentiment[0]['score']
# Main function to process audio and sentiment analysis
def process_transcription_and_sentiment(audio_file, model_choice):
# Transcription
if model_choice == "Whisper OpenAI":
transcription = whisper_openai_transcribe(audio_file)
elif model_choice == "Deepgram API":
transcription = deepgram_transcribe(audio_file)
elif model_choice == "Assembly AI API":
transcription = assemblyai_transcribe(audio_file)
# Sentiment analysis
sentiment_label, sentiment_score = analyze_sentiment(transcription)
return transcription, f"Sentiment: {sentiment_label} with score {sentiment_score}"
# Gradio interface setup
def create_interface():
with gr.Blocks() as demo:
gr.Markdown("### Audio Transcription and Sentiment Analysis")
with gr.Row():
audio_input = gr.Audio(source="upload", type="file", label="Upload Audio File")
model_choice = gr.Dropdown(
choices=["Whisper OpenAI", "Deepgram API", "Assembly AI API"],
label="Choose Transcription Model",
value="Whisper OpenAI"
)
# Textboxes for transcription and sentiment analysis
transcription_output = gr.Textbox(label="Transcription", lines=5)
sentiment_output = gr.Textbox(label="Sentiment Analysis", lines=5)
# Submit button
submit_button = gr.Button("Process")
# When the button is clicked, call the `process_transcription_and_sentiment` function
submit_button.click(process_transcription_and_sentiment,
inputs=[audio_input, model_choice],
outputs=[transcription_output, sentiment_output])
demo.launch()
if __name__ == "__main__":
create_interface()
|