Brunwo commited on
Commit
7071579
·
1 Parent(s): dfb37e3

working gradio js client, local & HF

Browse files
Files changed (7) hide show
  1. .gitignore +1 -0
  2. README.md +3 -0
  3. build.sh +3 -0
  4. frontendsetup.sh +4 -0
  5. index.html +3 -1
  6. script.js +47 -28
  7. vite.config.js +9 -0
.gitignore CHANGED
@@ -7,3 +7,4 @@ flagged/log.csv
7
  .aider*
8
  locales/fr/LC_MESSAGES/messages.mo
9
  locales/en/LC_MESSAGES/messages.mo
 
 
7
  .aider*
8
  locales/fr/LC_MESSAGES/messages.mo
9
  locales/en/LC_MESSAGES/messages.mo
10
+ node_modules/
README.md CHANGED
@@ -35,3 +35,6 @@ ngrok http 8000
35
 
36
  sudo apt-get install gettext
37
 
 
 
 
 
35
 
36
  sudo apt-get install gettext
37
 
38
+
39
+
40
+ npx vite
build.sh CHANGED
@@ -1,3 +1,6 @@
1
  msgfmt locales/fr/LC_MESSAGES/messages.po -o locales/fr/LC_MESSAGES/messages.mo
2
  msgfmt locales/es/LC_MESSAGES/messages.po -o locales/es/LC_MESSAGES/messages.mo
3
  msgfmt locales/en/LC_MESSAGES/messages.po -o locales/en/LC_MESSAGES/messages.mo
 
 
 
 
1
  msgfmt locales/fr/LC_MESSAGES/messages.po -o locales/fr/LC_MESSAGES/messages.mo
2
  msgfmt locales/es/LC_MESSAGES/messages.po -o locales/es/LC_MESSAGES/messages.mo
3
  msgfmt locales/en/LC_MESSAGES/messages.po -o locales/en/LC_MESSAGES/messages.mo
4
+
5
+
6
+ npx vite build
frontendsetup.sh ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+
2
+ npm init -y
3
+ npm install vite @gradio/client
4
+ touch vite.config.js
index.html CHANGED
@@ -32,6 +32,8 @@
32
  </div>
33
  </div>
34
 
35
- <script src="/script.js"></script>
 
 
36
  </body>
37
  </html>
 
32
  </div>
33
  </div>
34
 
35
+
36
+ <script type="module" src="script.js"></script>
37
+ <!-- <script src="/script.js"></script> -->
38
  </body>
39
  </html>
script.js CHANGED
@@ -1,3 +1,5 @@
 
 
1
  document.addEventListener("DOMContentLoaded", function() {
2
  const audioPlayer = document.getElementById('player');
3
  const settingsBtn = document.getElementById('settingsBtn');
@@ -86,9 +88,13 @@ document.addEventListener("DOMContentLoaded", function() {
86
 
87
  // Function to fetch MP3 from API endpoint when a link is shared
88
  async function fetchMp3(link) {
 
89
  try {
90
  const apiKey = localStorage.getItem('openaiApiKey');
91
  const apiServer = localStorage.getItem('apiServer');
 
 
 
92
  if (!apiKey) {
93
  throw new Error("API key not set. Please set your OpenAI API key in the settings.");
94
  }
@@ -96,51 +102,64 @@ document.addEventListener("DOMContentLoaded", function() {
96
  throw new Error("API server not set. Please set the API server in the settings.");
97
  }
98
 
99
- const response = await fetch(`${apiServer}/api/predict`, {
100
- method: 'POST',
101
- headers: {
102
- 'Content-Type': 'application/json',
103
- },
104
- body: JSON.stringify({
105
- data: [
106
- link,
107
- apiKey,
108
- "gpt-4o-mini",
109
- "tts-1",
110
- "alloy",
111
- "echo",
112
- null, // api_base
113
- "", // edited_transcript
114
- "", // user_feedback
115
- "summary" // original_text
116
- ]
117
- }),
118
- });
119
-
120
- if (!response.ok) {
121
- throw new Error(`HTTP error! status: ${response.status}`);
122
- }
123
 
124
- const result = await response.json();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
125
 
126
  // Assuming the audio file URL is the first item in the result
127
  const audioFileUrl = result.data[0];
 
128
 
129
  // Set the audio player source
130
  audioPlayer.src = audioFileUrl;
131
  audioPlayer.play();
 
132
  } catch (error) {
133
- console.error('Error fetching MP3:', error);
134
- alert(error.message);
 
135
  }
136
  }
137
 
138
  // Get the link from the shared URL
139
  const queryParams = new URLSearchParams(window.location.search);
140
  const sharedLink = queryParams.get('url');
141
-
 
 
142
  // Only call the API to get MP3 if a valid URL is provided
143
  if (sharedLink) {
 
144
  fetchMp3(sharedLink);
145
  } else {
146
  console.log("No URL provided. Waiting for user input.");
 
1
+ import { Client } from "@gradio/client";
2
+
3
  document.addEventListener("DOMContentLoaded", function() {
4
  const audioPlayer = document.getElementById('player');
5
  const settingsBtn = document.getElementById('settingsBtn');
 
88
 
89
  // Function to fetch MP3 from API endpoint when a link is shared
90
  async function fetchMp3(link) {
91
+ console.log('Starting fetchMp3 function with link:', link);
92
  try {
93
  const apiKey = localStorage.getItem('openaiApiKey');
94
  const apiServer = localStorage.getItem('apiServer');
95
+ console.log('Retrieved API key and server from localStorage');
96
+ console.log('API Server:', apiServer);
97
+
98
  if (!apiKey) {
99
  throw new Error("API key not set. Please set your OpenAI API key in the settings.");
100
  }
 
102
  throw new Error("API server not set. Please set the API server in the settings.");
103
  }
104
 
105
+ console.log('Attempting to connect to Gradio app...');
106
+
107
+ // Connect to local Gradio app
108
+ const client = await Client.connect(apiServer);
109
+
110
+ //connect to HF deployed one OK
111
+ //const client = await Client.connect("Mightypeacock/webtoaudio");
112
+
113
+ console.log('Gradio client created successfully');
114
+
115
+ console.log(await client.view_api())
116
+
117
+ console.log('Preparing to make prediction...');
118
+ // Make the prediction
 
 
 
 
 
 
 
 
 
 
119
 
120
+ const result = await client.predict("/generate_audio", {
121
+ url:link,
122
+ openai_api_key: apiKey,
123
+ text_model: "gpt-4o-mini",
124
+ audio_model: "tts-1",
125
+ speaker_1_voice: "alloy",
126
+ speaker_2_voice: "echo",
127
+ api_base: null, // api_base
128
+ edited_transcript: "", // edited_transcript
129
+ user_feedback: "", // user_feedback
130
+ original_text: "summary" // original_text
131
+ // debug: true,
132
+ });
133
+
134
+ console.log(result.data);
135
+
136
+
137
+ console.log('Prediction made successfully');
138
 
139
  // Assuming the audio file URL is the first item in the result
140
  const audioFileUrl = result.data[0];
141
+ console.log('Received audio file URL:', audioFileUrl);
142
 
143
  // Set the audio player source
144
  audioPlayer.src = audioFileUrl;
145
  audioPlayer.play();
146
+ console.log('Audio playback started');
147
  } catch (error) {
148
+ console.error('Error in fetchMp3:', error);
149
+ console.error('Error stack:', error.stack);
150
+ alert(`Error fetching MP3: ${error.message}`);
151
  }
152
  }
153
 
154
  // Get the link from the shared URL
155
  const queryParams = new URLSearchParams(window.location.search);
156
  const sharedLink = queryParams.get('url');
157
+
158
+ console.log('Shared link from URL:', sharedLink);
159
+
160
  // Only call the API to get MP3 if a valid URL is provided
161
  if (sharedLink) {
162
+ console.log('Valid URL provided, calling fetchMp3');
163
  fetchMp3(sharedLink);
164
  } else {
165
  console.log("No URL provided. Waiting for user input.");
vite.config.js ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+
2
+ // vite.config.js
3
+ import { defineConfig } from 'vite';
4
+
5
+ export default defineConfig({
6
+ build: {
7
+ outDir: 'dist', // The output directory for bundled files
8
+ },
9
+ });