Spaces:
Runtime error

File size: 4,934 Bytes
90b3952
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { handleLocalStorage } from "./storageService";
import { fetchRandomText } from "./apiService.js";

const openAIKey = localStorage.getItem("openAIKey");
const huggingFaceKey = localStorage.getItem("huggingFaceKey");

/**
 * Analyzes the current transcription by fetching emotion and sentiment data.
 * @param {Object} app - The Alpine component instance.
 */
export async function analyzeTranscription(app) {
  if (!app.textToAnalyze) {
    app.logMessage("No text available to analyze.");
    return;
  }

  try {
    const { emotions, sentiment, feedback } = await fetchAnalysisData(
      app,
      app.textToAnalyze
    );
    processAnalysisResults(app, emotions, sentiment, feedback);
  } catch (error) {
    app.logMessage(`Error connecting to the server: ${error.message}`);
  }
}

/**
 * Fetches a random text and analyzes it.
 * @param {Object} app - The Alpine component instance.
 */
export async function analyzeRandomText(app) {
  await fetchRandomText(app);
  await analyzeTranscription(app);
}

/**
 * Sends the transcription to the analysis APIs and retrieves emotion, sentiment, and feedback data.
 * @param {string} text - The transcription text to analyze.
 * @returns {Promise<Object>} An object containing emotions, sentiment, and feedback.
 */
async function fetchAnalysisData(app, text) {
  app.logMessage("Ping: /api/analyze", true);
  // First, fetch emotions and sentiment sequentially
  const emotionResponse = await fetch("/api/analyze", {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
      huggingFaceKey: huggingFaceKey,
    },
    body: JSON.stringify({ transcription: text }),
  });

  if (!emotionResponse.ok) {
    throw new Error(`Emotion API error: ${emotionResponse.statusText}`);
  }
  const emotionResult = await emotionResponse.json();

  app.logMessage("Ping: /api/sentiment", true);
  const sentimentResponse = await fetch("/api/sentiment", {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
      huggingFaceKey: huggingFaceKey,
    },
    body: JSON.stringify({ transcription: text }),
  });

  if (!sentimentResponse.ok) {
    throw new Error(`Sentiment API error: ${sentimentResponse.statusText}`);
  }
  const sentimentResult = await sentimentResponse.json();

  const formattedEmotions = (emotionResult.emotions?.significantEmotions || [])
    .map(({ emotion, score }) => `${emotion} ${score.toFixed(2)}`)
    .join(", ");

  const formattedSentiment = `${
    sentimentResult.sentiment?.sentiment
  } ${sentimentResult.sentiment?.score.toFixed(2)}`;

  const customPrompt =
    localStorage.getItem("userPrompt") !== localStorage.getItem("defaultPrompt")
      ? localStorage.getItem("userPrompt")
      : null;

  app.logMessage("Ping: /api/feedback");

  const feedbackResponse = await fetch("/api/feedback", {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
      huggingFaceKey: huggingFaceKey,
    },
    body: JSON.stringify({
      transcription: text,
      emotions: formattedEmotions,
      sentiment: formattedSentiment,
      customPrompt,
    }),
  });

  if (!feedbackResponse.ok) {
    throw new Error(`Feedback API error: ${feedbackResponse.statusText}`);
  }
  const feedbackResult = await feedbackResponse.json();

  return {
    emotions: emotionResult.emotions?.significantEmotions || [],
    sentiment: sentimentResult.sentiment || {},
    feedback: feedbackResult.feedback || "",
  };
}

/**
 * Processes the analysis results and updates the Alpine component.
 * @param {Object} app - The Alpine component instance.
 * @param {Array<Object>} emotions - List of detected emotions.
 * @param {Object} sentimentScores - Sentiment analysis scores.
 * @param {string} feedback - AI-generated feedback based on emotions and sentiment.
 */
function processAnalysisResults(app, emotions, sentimentScores, feedback) {
  if (emotions.length === 0) {
    app.logMessage("No significant emotions detected.");
  }

  const emotionList = emotions.map((e) => e.emotion).join(", ");
  const sentimentList = Object.entries(sentimentScores)
    .map(([key, value]) =>
      typeof value === "number"
        ? `${key} (${value.toFixed(2)})`
        : `${key} (${value})`
    )
    .join(", ");

  const feedbackText = feedback || "No feedback available.";

  app.logMessage(`Pong: ${emotionList}`);
  app.logMessage(`Pong: ${sentimentList}`);
  app.logMessage(`Pong: ${feedbackText}`);

  app.echoMessage(
    `${app.textToAnalyze} —<em>${emotionList}</em> [${sentimentList}]<br><strong>Feedback:</strong> ${feedbackText}`
  );

  const transcriptionData = {
    transcription: app.textToAnalyze,
    emotions: emotions.map((e) => ({ emotion: e.emotion, score: e.score })),
    sentiment: sentimentScores,
    feedback: feedbackText,
    date: new Date().toISOString(),
  };

  handleLocalStorage(app, "save", "transcriptionData", transcriptionData);

  app.textToAnalyze = "";
}