can you create an local web interface to do this api call and display the result. Use…
- User
can you create an local web interface to do this api call and display the result. Use PyServer for preventing CORSE errors.
import { Client } from "@gradio/client";
const client = await Client.connect("black-forest-labs/FLUX.1-dev"); const result = await client.predict("/infer", { prompt: "occult vector illustration", seed: 0, randomize_seed: true, width: 1024, height: 1024, guidance_scale: 3, num_inference_steps: 20, });
console.log(result.data);
- Assistant
Certainly! Below is a step-by-step guide to creating a local web interface that performs the specified API call to the Gradio app and displays the result. We'll use a Python Flask server (
PyServer) to prevent CORS errors by acting as a proxy between your frontend and the Gradio API.Overview:
- Set up a Flask server that will handle API requests from your frontend, make the API call to the Gradio app, and return the result.
- Create an HTML page with a form to collect input parameters.
- Use JavaScript to send the form data to your Flask server and display the result on the page.
1. Setup the Flask Server
Install Required Packages:
Make sure you have the following Python packages installed:
pip install flask flask-cors gradio_clientCreate
server.py:from flask import Flask, request, jsonify from gradio_client import Client from flask_cors import CORS app = Flask(__name__) CORS(app) # Enable CORS # Instantiate the Gradio client client = Client("black-forest-labs/FLUX.1-dev") @app.route('/api/infer', methods=['POST']) def infer(): data = request.json # Extract parameters from the request data prompt = data.get('prompt', '') seed = data.get('seed', 0) randomize_seed = data.get('randomize_seed', True) width = data.get('width', 1024) height = data.get('height', 1024) guidance_scale = data.get('guidance_scale', 3) num_inference_steps = data.get('num_inference_steps', 20) # Call the Gradio client result = client.predict( prompt, # str in 'prompt' Textbox component seed, # int | float in 'seed' Number component randomize_seed, # bool in 'randomize_seed' Checkbox component width, # int | float in 'width' Number component height, # int | float in 'height' Number component guidance_scale, # int | float in 'guidance_scale' Number component num_inference_steps, # int | float in 'num_inference_steps' Number component api_name="/infer" ) # Return the result as JSON return jsonify({'data': result}) if __name__ == '__main__': app.run(debug=True)Explanation:
- Flask Server: Handles incoming requests from your web interface.
- CORS:
flask_corsis used to enable Cross-Origin Resource Sharing (CORS) so that your frontend can communicate with the Flask server without CORS issues. - Gradio Client: Uses the
gradio_clientlibrary to interact with the Gradio app.
2. Create the HTML Interface
Create
index.html:<!DOCTYPE html> <html> <head> <title>Gradio API Interface</title> </head> <body> <h1>Gradio API Interface</h1> <form id="infer-form"> <label for="prompt">Prompt:</label> <input type="text" id="prompt" name="prompt" value="occult vector illustration"><br><br> <label for="seed">Seed:</label> <input type="number" id="seed" name="seed" value="0"><br><br> <label for="randomize_seed">Randomize Seed:</label> <input type="checkbox" id="randomize_seed" name="randomize_seed" checked><br><br> <label for="width">Width:</label> <input type="number" id="width" name="width" value="1024"><br><br> <label for="height">Height:</label> <input type="number" id="height" name="height" value="1024"><br><br> <label for="guidance_scale">Guidance Scale:</label> <input type="number" id="guidance_scale" name="guidance_scale" value="3" step="0.1"><br><br> <label for="num_inference_steps">Number of Inference Steps:</label> <input type="number" id="num_inference_steps" name="num_inference_steps" value="20"><br><br> <input type="submit" value="Submit"> </form> <h2>Result:</h2> <div id="result"></div> <script> document.getElementById('infer-form').addEventListener('submit', function(e) { e.preventDefault(); const data = { prompt: document.getElementById('prompt').value, seed: parseInt(document.getElementById('seed').value), randomize_seed: document.getElementById('randomize_seed').checked, width: parseInt(document.getElementById('width').value), height: parseInt(document.getElementById('height').value), guidance_scale: parseFloat(document.getElementById('guidance_scale').value), num_inference_steps: parseInt(document.getElementById('num_inference_steps').value) }; fetch('http://localhost:5000/api/infer', { method: 'POST', headers: {'Content-Type': 'application/json'}, body: JSON.stringify(data) }) .then(response => response.json()) .then(result => { console.log(result); // Display image result if it's an image URL or base64 data if (result.data.startsWith('data:image')) { const img = document.createElement('img'); img.src = result.data; document.getElementById('result').innerHTML = ''; document.getElementById('result').appendChild(img); } else { // Otherwise, display the result as text document.getElementById('result').textContent = JSON.stringify(result.data, null, 2); } }) .catch(error => { console.error('Error:', error); document.getElementById('result').textContent = 'An error occurred.'; }); }); </script> </body> </html>Explanation:
- Form Fields: Collects user input for each parameter.
- JavaScript Fetch API: Sends a POST request to your Flask server with the form data in JSON format.
- Result Display: Displays the result returned by the server. If the result is an image (e.g., base64 encoded), it creates an
<img>element to display it.
Note: The display logic assumes that the result returned is either a URL or base64-encoded image data. Adjust the display logic based on the actual data format.
3. Run the Application
Start the Flask Server:
python server.pyOpen the HTML Page:
Since we're making AJAX calls to
localhost:5000, you'll need to serveindex.htmlfrom a local server to avoid CORS issues with thefile://protocol.Option 1: Use Python's SimpleHTTPServer
If you're using Python 3:
python -m http.server 8000Option 2: Use Live Server Extension (VSCode)
If you're using Visual Studio Code, you can use the Live Server extension to serve the HTML file.
Access the Web Interface:
Open your browser and navigate to:
http://localhost:8000/index.html
4. Test the Application
- Fill in the form fields as desired.
- Click Submit.
- The result will be displayed below the form.
Explanation of How This Prevents CORS Issues
- Frontend to Backend: Your web page (
index.html) and the Flask server are both running onlocalhost, but on different ports (e.g.,8000for the web page and5000for the Flask server). Normally, browsers enforce the Same-Origin Policy, which would prevent the web page from making requests to a different port. - CORS: By using
flask_corsand callingCORS(app), we configure the Flask server to include the appropriate CORS headers (Access-Control-Allow-Origin) in its responses, allowing the browser to make cross-origin requests. - Backend to Gradio API: The Flask server makes the request to the Gradio app server-side, where CORS doesn't apply.
Additional Notes
- Error Handling: The JavaScript code includes basic error handling. You might want to enhance it to handle different types of errors gracefully.
- Security: This setup is suitable for local development and testing. For production use, you should implement proper security measures, such as input validation and error handling.
- Async/Await: You can modernize the JavaScript code using
async/awaitfor better readability.
Modified JavaScript with Async/Await:
<script> document.getElementById('infer-form').addEventListener('submit', async function(e) { e.preventDefault(); const data = { prompt: document.getElementById('prompt').value, seed: parseInt(document.getElementById('seed').value), randomize_seed: document.getElementById('randomize_seed').checked, width: parseInt(document.getElementById('width').value), height: parseInt(document.getElementById('height').value), guidance_scale: parseFloat(document.getElementById('guidance_scale').value), num_inference_steps: parseInt(document.getElementById('num_inference_steps').value) }; try { const response = await fetch('http://localhost:5000/api/infer', { method: 'POST', headers: {'Content-Type': 'application/json'}, body: JSON.stringify(data) }); const result = await response.json(); console.log(result); if (result.data.startsWith('data:image')) { const img = document.createElement('img'); img.src = result.data; document.getElementById('result').innerHTML = ''; document.getElementById('result').appendChild(img); } else { document.getElementById('result').textContent = JSON.stringify(result.data, null, 2); } } catch (error) { console.error('Error:', error); document.getElementById('result').textContent = 'An error occurred.'; } }); </script>
Conclusion
By following these steps, you should have a local web interface that interacts with the Gradio API and displays the result, all while preventing CORS errors using a Flask server as a proxy.
If you have any questions or need further assistance, feel free to ask!