File size: 6,004 Bytes
221417c
03c97ac
221417c
 
 
 
 
 
 
 
64afdeb
 
 
 
 
 
 
 
 
a5bca4d
64afdeb
 
 
 
 
7cb6b3d
 
221417c
 
790f0aa
0bef3a8
 
 
790f0aa
 
 
 
 
 
0bef3a8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
790f0aa
0bef3a8
4d6ac72
 
 
5459b54
0bef3a8
 
 
 
5459b54
221417c
 
452575b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7cb6b3d
221417c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cb666d9
221417c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a68907d
 
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import gradio as gr

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):
    import whisper
    
    # Load the Whisper model for transcription
    whisper_model = whisper.load_model("large")  # Use 'base', 'small', 'medium', or 'large' depending on your need
    
    """
    Transcribe audio file using Whisper.
    """
    # Transcribe the audio file
    result = whisper_model.transcribe(audio_file)
    
    # Get the transcribed text
    transcribed_text = result['text']
    
    
    
    return transcribed_text

def deepgram_transcribe(audio_file):
    from deepgram import DeepgramClient, PrerecordedOptions, Filesource

    DEEPGRAM_API_KEY = "aeb38b43a17867c59928e6a103ac75c06d4e896d"
    
    with open(audio_file, "rb") as file:
        buffer_data = file.read()

    payload: FileSource = {
            "buffer": buffer_data,
        }
    
    try:
        deepgram = DeepgramClient(DEEPGRAM_API_KEY)
        
        options = PrerecordedOptions(
            model="enhanced",
            language="ta",
            smart_format=True,
            punctuate=True,
            paragraphs=True,
            utterances=True,
            keywords=[":"],
            diarize=True,
        )
    
        response = deepgram.listen.rest.v("1").transcribe_file(payload, options)
        print(response.to_json(indent=4))
        transcript = response['results']['channels'][0]['alternatives'][0]['transcript']
        print(transcript)
        return transcript
        
        
    except Exception as e:
        print(f"Exception: {e}")

    

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 transcript.text

# 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(sources=["upload"], type="filepath", 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, 
                            [audio_input, model_choice], 
                            [transcription_output, sentiment_output])
        
    demo.launch()

if __name__ == "__main__":
    create_interface()