Spaces:
Running
Running
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) | |