File size: 7,269 Bytes
95d40eb b09dde9 95d40eb b09dde9 6d04fe9 b09dde9 95d40eb b09dde9 6d04fe9 e4fe613 b09dde9 95d40eb cc95a89 95d40eb b09dde9 95d40eb b09dde9 95d40eb e4fe613 95d40eb b09dde9 3cddb1a 95d40eb b09dde9 95d40eb 17589bb 95d40eb b09dde9 e4fe613 b09dde9 ddeb95f e4fe613 b09dde9 3bd4557 6d04fe9 3bd4557 6d04fe9 b09dde9 ddeb95f b09dde9 ddeb95f 95d40eb |
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 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
import gradio as gr
import cv2
import easyocr
import numpy as np
import requests
import os
import whisper
from transformers import pipeline
API_KEY = os.getenv("API_KEY")
IMAGE_API_URL = "https://api-inference.huggingface.co/models/dima806/facial_emotions_image_detection"
headers = {"Authorization": "Bearer "+ API_KEY+""}
EMOTIONS_API_URL = "https://api-inference.huggingface.co/models/SamLowe/roberta-base-go_emotions"
headers = {"Authorization": "Bearer "+ API_KEY+""}
reader = easyocr.Reader(['en'], gpu=False)
model = whisper.load_model("base")
sentiment_analysis = pipeline("sentiment-analysis", framework="pt", model="SamLowe/roberta-base-go_emotions")
def query(image):
image_data = np.array(image, dtype=np.uint8)
_, buffer = cv2.imencode('.jpg', image_data)
binary_data = buffer.tobytes()
response = requests.post(IMAGE_API_URL, headers=headers, data=binary_data)
result = {item['label']: item['score'] for item in response.json()}
return result
def text_extraction(image):
global text_content
text_content = ''
facial_data = query(image)
text_ = reader.readtext(image)
threshold = 0.25
for t_, t in enumerate(text_):
bbox, text, score = t
text_content = text_content + ' ' + ' '.join(text)
if score > threshold:
cv2.rectangle(image, tuple(map(int, bbox[0])), tuple(map(int, bbox[2])), (0, 255, 0), 5)
return image, text_content, facial_data
def analyze_sentiment(text):
results = sentiment_analysis(text)
print(results)
sentiment_results = {result['label']: result['score'] for result in results}
return sentiment_results
def get_sentiment_emoji(sentiment):
emoji_mapping = {
"disappointment": "๐",
"sadness": "๐ข",
"annoyance": "๐ ",
"neutral": "๐",
"disapproval": "๐",
"realization": "๐ฎ",
"nervousness": "๐ฌ",
"approval": "๐",
"joy": "๐",
"anger": "๐ก",
"embarrassment": "๐ณ",
"caring": "๐ค",
"remorse": "๐",
"disgust": "๐คข",
"grief": "๐ฅ",
"confusion": "๐",
"relief": "๐",
"desire": "๐",
"admiration": "๐",
"optimism": "๐",
"fear": "๐จ",
"love": "โค๏ธ",
"excitement": "๐",
"curiosity": "๐ค",
"amusement": "๐",
"surprise": "๐ฒ",
"gratitude": "๐",
"pride": "๐ฆ"
}
return emoji_mapping.get(sentiment, "")
def display_sentiment_results(sentiment_results, option):
sentiment_text = ""
for sentiment, score in sentiment_results.items():
emoji = get_sentiment_emoji(sentiment)
if option == "Sentiment Only":
sentiment_text += f"{sentiment} {emoji}\n"
elif option == "Sentiment + Score":
sentiment_text += f"{sentiment} {emoji}: {score}\n"
return sentiment_text
def inference(image, text, audio, sentiment_option):
extracted_image, extracted_text, extracted_facial_data = text_extraction(image)
audio = whisper.load_audio(audio)
audio = whisper.pad_or_trim(audio)
mel = whisper.log_mel_spectrogram(audio).to(model.device)
_, probs = model.detect_language(mel)
lang = max(probs, key=probs.get)
options = whisper.DecodingOptions(fp16=False)
result = whisper.decode(model, mel, options)
audio_sentiment_results = analyze_sentiment(result.text) # Ta - Text from audio
image_sentiment_results = analyze_sentiment(extracted_text) # Ti - Text from image
text_sentiment_results = analyze_sentiment(text) # T - User defined Text
audio_sentiment_output = display_sentiment_results(audio_sentiment_results, sentiment_option)
image_sentiment_output = display_sentiment_results(image_sentiment_results, sentiment_option)
text_sentiment_output = display_sentiment_results(text_sentiment_results, sentiment_option)
return extracted_image, extracted_facial_data, extracted_text, image_sentiment_output, text_sentiment_output, lang.upper(), result.text, audio_sentiment_output
title = """<h1 align="center">Cross Model Machine Learning (Sentiment Analysis)</h1>"""
image_path = "thmbnail.png"
description = """
๐ป This demo showcases a Cross Model Machine Learning for Sentiment Analysis.<br><br>
<br>
โ๏ธ Components of the tool:<br>
<br>
- Sentiment Analysis of Image<br>
- Text Extraction from Image<br>
- Sentiment analysis of the user given text.<br>
- Real-time multilingual speech recognition<br>
- Language identification<br>
- Sentiment analysis of the transcriptions<br>
<br>
๐ฏ The sentiment analysis results are provided as a dictionary with different emotions and their corresponding scores.<br>
<br>
๐ The sentiment analysis results are displayed with emojis representing the corresponding sentiment.<br>
<br>
โ
The higher the score for a specific emotion, the stronger the presence of that emotion in the transcribed text.<br>
<br>
โ Use the microphone for real-time speech recognition.<br>
<br>
โก๏ธ The model will transcribe the audio and perform sentiment analysis on the transcribed text.<br>
"""
custom_css = """
#banner-image {
display: block;
margin-left: auto;
margin-right: auto;
}
#chat-message {
font-size: 14px;
min-height: 300px;
}
"""
block = gr.Blocks(css=custom_css)
with block:
gr.HTML(title)
with gr.Row():
with gr.Column():
gr.Image(image_path, elem_id="banner-image", show_label=False)
with gr.Column():
gr.HTML(description)
with gr.Blocks():
with gr.Column():
with gr.Row():
image = gr.Image()
image_output = gr.Image()
text_output = gr.Textbox(label="Text Content")
image_text_sentiment = gr.Textbox(label="Image Text Sentiment")
facial_output = gr.Label(label='Facial Data', container=True, scale=2)
with gr.Row():
with gr.Column():
gr.Textbox(label="Text Content")
output_text_sentiment = gr.Textbox(label="Text Sentiment")
with gr.Blocks():
with gr.Row():
audio = gr.Audio(label="Input Audio", show_label=False, type="filepath")
with gr.Row():
sentiment_option = gr.Radio(choices=["Sentiment Only", "Sentiment + Score"], label="Select an option")
lang_str = gr.Textbox(label="Language")
text = gr.Textbox(label="Transcription")
sentiment_output = gr.Textbox(label="Audio Text Sentiment")
btn = gr.Button("Run")
btn.click(inference, inputs=[image, text, audio, sentiment_option], outputs=[image_output, facial_output, text_output, image_text_sentiment, output_text_sentiment, lang_str, text, sentiment_output])
block.launch()
|