I am developing llm for inference locally, but OSError: Cannot find empty port in range: 7860-7959. You can specify a different port by setting the GRADIO_SERVER_PORT environment variable or passing the server_port parameter to launch().
How to fix?
Assistant
The error message you're encountering indicates that the specific port range from 7860 to 7959 is fully occupied, preventing the Gradio server from starting. Here are several ways you can fix this issue:
User
Am I using gradio server? But I am running locally
Assistant
Gradio is commonly used to create user interfaces that allow interactive use of machine learning models, and it can run both locally and on remote servers. If you're seeing an error related to port availability, it indicates that Gradio is trying to start up a local web server to serve your application. Even when you're working locally, Gradio employs a web server architecture to create a user interface accessible through a web browser.
1. Specify a Different Port:
You can specify a port that you know is free by setting the GRADIO_SERVER_PORT environment variable or by passing the server_port parameter in the launch() method call.
Example using launch() method:
python
import gradio as gr
def your_function():
# Your inference code here
pass
gr.Interface(fn=your_function, inputs="text", outputs="text").launch(server_port=8080)
Example setting an environment variable on Unix/Linux/Mac:
bash
export GRADIO_SERVER_PORT=8080
Example setting an environment variable on Windows:
cmd
set GRADIO_SERVER_PORT=8080
2. Free Up Ports:
If you want to keep using the default port range (7860-7959), you’ll need to identify and terminate processes that may be using those ports:
On Windows:
Open Command Prompt and run:
cmd
netstat -aon | findstr :<port_number>
This will show you the process using a specific port. You can kill the process by using:
cmd
taskkill /PID <PID> /F
On Linux/Mac:
Open a terminal and run:
bash
lsof -i :<port_number>
This will show you the process ID (PID). You can terminate it with:
bash
kill -9 <PID>
3. Use a Different Port Range:
If you have multiple applications or instances using Gradio, consider using a larger or different range by modifying the Gradio source code where the default port range is defined (not recommended, as it requires code modification). Instead, just choose a port outside of the default range as mentioned above.
4. Check for Background Processes:
Sometimes, background processes or server instances might still be running. You may want to check your task manager (Windows), Activity Monitor (Mac), or terminal processes (Linux) to ensure there are no lingering applications using those ports.
5. Network Conflicts:
Occasionally, firewall settings or network configurations might block access to certain ports. Ensure you don’t have any network configurations that may prevent your application from initiating server connections.
By following these steps, you should be able to resolve the port conflict and run your gradio application for local inference successfully.
The error you encountered (OSError: Cannot find empty port in range: 7860-7959) confirms that Gradio is indeed attempting to bind to a port in your local environment for that web server. This means it needs an available port to function correctly, whether you're running it on your machine or on a server.
Solutions Recap:
Use a Different Port: Specify an alternate port either by modifying your code or setting an environment variable.
Free Up Ports: Terminate any processes currently using the default port range by finding and killing them.
Use the launch() method appropriately: Set the server_port parameter in the launch() method of the Gradio interface.
Example Code Snippet
Make sure your code looks similar to this, where you're assigning an available port:
python
import gradio as gr
def your_function(input_text):
# Placeholder function logic
return f"Processed: {input_text}"
gr.Interface(fn=your_function, inputs="text", outputs="text").launch(server_port=8080)
Running Locally
When you run your Gradio application locally, by default it should open up in a web browser, typically at http://localhost:7860 (or the port you set). If you specify a different port (e.g., 8080), you will access it via http://localhost:8080.
Summary
Even when working locally, Gradio runs a server to handle incoming requests and return responses for your machine learning model. That's why it needs a port available. The solutions provided should effectively resolve the issue and enable you to continue developing your LLM (Large Language Model) interface locally.