Spaces:
Running
Running
File size: 1,531 Bytes
996debf dfd2a10 996debf dfd2a10 996debf |
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 |
import matplotlib.pyplot as plt
from transcript_processor import query, get_youtube_transcript
def fetch_and_plot_emotions(youtube_id):
"""
Fetches the emotion scores from a YouTube video transcript and plots the data.
Args:
youtube_id (str): The ID of the YouTube video.
Returns:
str: Path to the saved plot image.
"""
try:
transcript = get_youtube_transcript(youtube_id)
if "error" in transcript:
return transcript["error"]
emotion_scores = query({"inputs": transcript})
print(emotion_scores)
if "error" in emotion_scores:
return emotion_scores["error"]
return plot_emotion_scores(emotion_scores)
except Exception as e:
return str(e)
def plot_emotion_scores(data):
"""
Plots emotion scores from the provided JSON data.
Args:
data (list): A list of dictionaries containing emotion labels and scores.
Returns:
str: Path to the saved plot image.
"""
try:
labels = [item['label'] for item in data[0]]
scores = [item['score'] for item in data[0]]
plt.figure(figsize=(10, 8))
plt.barh(labels, scores, color='skyblue')
plt.xlabel('Score')
plt.title('Emotion Scores')
plt.gca().invert_yaxis() # Highest scores at the top
plt.tight_layout()
# Save the plot to a file
plot_path = 'emotion_scores.png'
plt.savefig(plot_path)
plt.close()
return plot_path
except KeyError as e:
return f"Missing expected key in data: {e}"
except Exception as e:
return str(e)
|