Spaces:
Build error
Build error
Commit
·
a6f672d
1
Parent(s):
3df0e86
fix paths and ports for HF setup
Browse files- app.py +0 -52
- log.txt +12 -0
- mmapi/api.js +4 -4
- mmapi/image.js +3 -4
- mmapi/public/index.html +114 -0
app.py
CHANGED
|
@@ -42,55 +42,3 @@ else:
|
|
| 42 |
# Log the process spawn success
|
| 43 |
with open(log_file_path, 'a') as log_file:
|
| 44 |
log_file.write(f"[{datetime.now()}] API service started and running in the background.\n")
|
| 45 |
-
|
| 46 |
-
# Gradio app to display the HTML documentation
|
| 47 |
-
documentation_html = """
|
| 48 |
-
<!DOCTYPE html>
|
| 49 |
-
<html lang="en">
|
| 50 |
-
<head>
|
| 51 |
-
<meta charset="UTF-8">
|
| 52 |
-
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 53 |
-
<title>API Documentation</title>
|
| 54 |
-
<style>
|
| 55 |
-
body {
|
| 56 |
-
font-family: Arial, sans-serif;
|
| 57 |
-
line-height: 1.6;
|
| 58 |
-
max-width: 800px;
|
| 59 |
-
margin: 20px auto;
|
| 60 |
-
padding: 0 20px;
|
| 61 |
-
color: #333;
|
| 62 |
-
}
|
| 63 |
-
h1 {
|
| 64 |
-
color: #0056b3;
|
| 65 |
-
}
|
| 66 |
-
code {
|
| 67 |
-
background: #f4f4f4;
|
| 68 |
-
padding: 2px 6px;
|
| 69 |
-
border-radius: 4px;
|
| 70 |
-
}
|
| 71 |
-
pre {
|
| 72 |
-
background: #f4f4f4;
|
| 73 |
-
padding: 10px;
|
| 74 |
-
border-radius: 4px;
|
| 75 |
-
overflow-x: auto;
|
| 76 |
-
}
|
| 77 |
-
</style>
|
| 78 |
-
</head>
|
| 79 |
-
<body>
|
| 80 |
-
<h1>API Documentation</h1>
|
| 81 |
-
<p>This page documents the available APIs for the Node.js service, detailing endpoints, parameters, and expected responses.</p>
|
| 82 |
-
<!-- Content omitted for brevity -->
|
| 83 |
-
</body>
|
| 84 |
-
</html>
|
| 85 |
-
"""
|
| 86 |
-
|
| 87 |
-
# Create a Gradio interface that displays the HTML documentation
|
| 88 |
-
def show_documentation():
|
| 89 |
-
return documentation_html
|
| 90 |
-
|
| 91 |
-
doc_app = gr.Blocks()
|
| 92 |
-
with doc_app:
|
| 93 |
-
gr.HTML(show_documentation())
|
| 94 |
-
|
| 95 |
-
# Launch the Gradio app
|
| 96 |
-
doc_app.launch(server_port=7861, server_name="0.0.0.0")
|
|
|
|
| 42 |
# Log the process spawn success
|
| 43 |
with open(log_file_path, 'a') as log_file:
|
| 44 |
log_file.write(f"[{datetime.now()}] API service started and running in the background.\n")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
log.txt
CHANGED
|
@@ -24,3 +24,15 @@ Emitted 'error' event on Server instance at:
|
|
| 24 |
}
|
| 25 |
|
| 26 |
Node.js v20.18.0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
}
|
| 25 |
|
| 26 |
Node.js v20.18.0
|
| 27 |
+
|
| 28 |
+
[2024-10-25 05:17:24.382930] Script started.
|
| 29 |
+
[2024-10-25 05:17:24.383335] API service already running on port 7860, exiting script.
|
| 30 |
+
|
| 31 |
+
[2024-10-25 05:18:50.285983] Script started.
|
| 32 |
+
[2024-10-25 05:18:50.286348] API service already running on port 7860, exiting script.
|
| 33 |
+
|
| 34 |
+
[2024-10-25 05:19:34.842210] Script started.
|
| 35 |
+
[2024-10-25 05:19:34.842602] API service already running on port 7860, exiting script.
|
| 36 |
+
|
| 37 |
+
[2024-10-25 05:24:34.969499] Script started.
|
| 38 |
+
[2024-10-25 05:24:34.969881] API service already running on port 7860, exiting script.
|
mmapi/api.js
CHANGED
|
@@ -15,8 +15,8 @@ app.use(cors()); // Enable CORS for all routes
|
|
| 15 |
require('dotenv').config()
|
| 16 |
|
| 17 |
|
| 18 |
-
const MEDIA_FOLDER = process.env.
|
| 19 |
-
const BASE_URL = process.env.MMAPI_BASE_URL || 'http://localhost:
|
| 20 |
const OPENAI_API_KEY = process.env.OPENAI_API_KEY
|
| 21 |
|
| 22 |
// Ensure the MEDIA_FOLDER directory exists
|
|
@@ -274,8 +274,8 @@ app.post("/generate/image", async(req, res)=> {
|
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
-
//
|
| 278 |
-
app.use('/
|
| 279 |
|
| 280 |
const port = 7860;
|
| 281 |
app.listen(port, () => {
|
|
|
|
| 15 |
require('dotenv').config()
|
| 16 |
|
| 17 |
|
| 18 |
+
const MEDIA_FOLDER = `${process.env.STATIC_SITE_ROOT}/media`
|
| 19 |
+
const BASE_URL = process.env.MMAPI_BASE_URL || 'http://localhost:7860';
|
| 20 |
const OPENAI_API_KEY = process.env.OPENAI_API_KEY
|
| 21 |
|
| 22 |
// Ensure the MEDIA_FOLDER directory exists
|
|
|
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
+
// Client webpages and storage for generated content
|
| 278 |
+
app.use('/', express.static(process.env.STATIC_SITE_ROOT));
|
| 279 |
|
| 280 |
const port = 7860;
|
| 281 |
app.listen(port, () => {
|
mmapi/image.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
| 1 |
-
const express = require('express');
|
| 2 |
const axios = require('axios');
|
| 3 |
const crypto = require('crypto');
|
| 4 |
const fs = require('fs');
|
|
@@ -6,12 +5,12 @@ const path = require('path');
|
|
| 6 |
const { createCanvas, loadImage } = require('canvas');
|
| 7 |
const { HfInference } = require("@huggingface/inference");
|
| 8 |
|
| 9 |
-
// Initialize the Express app
|
| 10 |
-
const app = express();
|
| 11 |
require("dotenv").config()
|
| 12 |
|
| 13 |
// Cache directory to store generated images
|
| 14 |
-
const CACHE_DIR = process.env.
|
|
|
|
|
|
|
| 15 |
if (!fs.existsSync(CACHE_DIR)) {
|
| 16 |
fs.mkdirSync(CACHE_DIR);
|
| 17 |
}
|
|
|
|
|
|
|
| 1 |
const axios = require('axios');
|
| 2 |
const crypto = require('crypto');
|
| 3 |
const fs = require('fs');
|
|
|
|
| 5 |
const { createCanvas, loadImage } = require('canvas');
|
| 6 |
const { HfInference } = require("@huggingface/inference");
|
| 7 |
|
|
|
|
|
|
|
| 8 |
require("dotenv").config()
|
| 9 |
|
| 10 |
// Cache directory to store generated images
|
| 11 |
+
const CACHE_DIR = `${process.env.STATIC_SITE_ROOT}/image_out`
|
| 12 |
+
|
| 13 |
+
|
| 14 |
if (!fs.existsSync(CACHE_DIR)) {
|
| 15 |
fs.mkdirSync(CACHE_DIR);
|
| 16 |
}
|
mmapi/public/index.html
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<!DOCTYPE html>
|
| 2 |
+
<html lang="en">
|
| 3 |
+
<head>
|
| 4 |
+
<meta charset="UTF-8">
|
| 5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 6 |
+
<title>API Documentation</title>
|
| 7 |
+
<style>
|
| 8 |
+
body {
|
| 9 |
+
font-family: Arial, sans-serif;
|
| 10 |
+
line-height: 1.6;
|
| 11 |
+
max-width: 800px;
|
| 12 |
+
margin: 20px auto;
|
| 13 |
+
padding: 0 20px;
|
| 14 |
+
color: #333;
|
| 15 |
+
}
|
| 16 |
+
h1 {
|
| 17 |
+
color: #0056b3;
|
| 18 |
+
}
|
| 19 |
+
code {
|
| 20 |
+
background: #f4f4f4;
|
| 21 |
+
padding: 2px 6px;
|
| 22 |
+
border-radius: 4px;
|
| 23 |
+
}
|
| 24 |
+
pre {
|
| 25 |
+
background: #f4f4f4;
|
| 26 |
+
padding: 10px;
|
| 27 |
+
border-radius: 4px;
|
| 28 |
+
overflow-x: auto;
|
| 29 |
+
}
|
| 30 |
+
</style>
|
| 31 |
+
</head>
|
| 32 |
+
<body>
|
| 33 |
+
<h1>API Documentation</h1>
|
| 34 |
+
<p>This page documents the available APIs for the Node.js service, detailing endpoints, parameters, and expected responses.</p>
|
| 35 |
+
|
| 36 |
+
<h2>1. GET /list-models</h2>
|
| 37 |
+
<p>Retrieves a list of available models.</p>
|
| 38 |
+
<strong>URL:</strong>
|
| 39 |
+
<pre>GET /list-models</pre>
|
| 40 |
+
<strong>Response:</strong>
|
| 41 |
+
<pre>["Model 1", "Model 2", "Model 3"]</pre>
|
| 42 |
+
|
| 43 |
+
<h2>2. GET /generate/speech</h2>
|
| 44 |
+
<p>Generates speech audio based on the provided script.</p>
|
| 45 |
+
<strong>URL:</strong>
|
| 46 |
+
<pre>GET /generate/speech?api_key=<code>your_api_key</code>&payload=<code>script_payload</code></pre>
|
| 47 |
+
<strong>Parameters:</strong>
|
| 48 |
+
<ul>
|
| 49 |
+
<li><strong>api_key</strong> (required): Your API key for authentication.</li>
|
| 50 |
+
<li><strong>payload</strong> (required): The script payload containing speaker names and speech content.</li>
|
| 51 |
+
</ul>
|
| 52 |
+
<strong>Response:</strong>
|
| 53 |
+
<pre>Audio file in MPEG format (served as a response file).</pre>
|
| 54 |
+
|
| 55 |
+
<h2>3. POST /generate/speech/stream</h2>
|
| 56 |
+
<p>Generates speech audio and streams the response via Server-Sent Events (SSE).</p>
|
| 57 |
+
<strong>URL:</strong>
|
| 58 |
+
<pre>POST /generate/speech/stream?api_key=<code>your_api_key</code></pre>
|
| 59 |
+
<strong>Headers:</strong>
|
| 60 |
+
<ul>
|
| 61 |
+
<li><strong>Content-Type</strong>: application/json</li>
|
| 62 |
+
</ul>
|
| 63 |
+
<strong>Body:</strong>
|
| 64 |
+
<pre>{
|
| 65 |
+
"payload": "script_payload"
|
| 66 |
+
}</pre>
|
| 67 |
+
<strong>Response:</strong>
|
| 68 |
+
<ul>
|
| 69 |
+
<li><code>audio_segment</code>: URL to generated audio segment.</li>
|
| 70 |
+
<li><code>audio_complete</code>: URL to the final combined audio file.</li>
|
| 71 |
+
</ul>
|
| 72 |
+
|
| 73 |
+
<h2>4. GET /generate/image</h2>
|
| 74 |
+
<p>Generates an image based on the provided prompt.</p>
|
| 75 |
+
<strong>URL:</strong>
|
| 76 |
+
<pre>GET /generate/image?prompt=<code>image_prompt</code>&width=<code>image_width</code>&height=<code>image_height</code>&response_format=<code>image</code> (default)</pre>
|
| 77 |
+
<strong>Parameters:</strong>
|
| 78 |
+
<ul>
|
| 79 |
+
<li><strong>prompt</strong> (required): Description of the image to generate.</li>
|
| 80 |
+
<li><strong>width</strong> (optional): Width of the generated image (default is 1024).</li>
|
| 81 |
+
<li><strong>height</strong> (optional): Height of the generated image (default is 1024).</li>
|
| 82 |
+
<li><strong>response_format</strong> (optional): Format of the response, can be "image" or "url".</li>
|
| 83 |
+
</ul>
|
| 84 |
+
<strong>Response:</strong>
|
| 85 |
+
<pre>Generated image or URL to the image.</pre>
|
| 86 |
+
|
| 87 |
+
<h2>5. POST /generate/image</h2>
|
| 88 |
+
<p>Generates an image based on a prompt passed in the body of the request.</p>
|
| 89 |
+
<strong>URL:</strong>
|
| 90 |
+
<pre>POST /generate/image?response_format=<code>url</code> (default)</pre>
|
| 91 |
+
<strong>Headers:</strong>
|
| 92 |
+
<ul>
|
| 93 |
+
<li><strong>Content-Type</strong>: application/json</li>
|
| 94 |
+
</ul>
|
| 95 |
+
<strong>Body:</strong>
|
| 96 |
+
<pre>{
|
| 97 |
+
"prompt": "A large hamster"
|
| 98 |
+
}</pre>
|
| 99 |
+
<strong>Response:</strong>
|
| 100 |
+
<pre>{ "imageUrl": "https://example.com/generated_image.jpg" }</pre>
|
| 101 |
+
|
| 102 |
+
<h2>6. Serving Static Media Files</h2>
|
| 103 |
+
<p>All generated media (audio and images) can be accessed via:</p>
|
| 104 |
+
<strong>URL:</strong>
|
| 105 |
+
<pre>GET /media/<code>filename</code></pre>
|
| 106 |
+
|
| 107 |
+
<h2>Notes</h2>
|
| 108 |
+
<ul>
|
| 109 |
+
<li>Ensure that your API key is provided when required.</li>
|
| 110 |
+
<li>Audio and image generation requires a valid script payload or image prompt respectively.</li>
|
| 111 |
+
<li>The system uses OpenAI for TTS, and appropriate API keys need to be set in the environment variables.</li>
|
| 112 |
+
</ul>
|
| 113 |
+
</body>
|
| 114 |
+
</html>
|