Open
Description
Hi,
I want to use another language like java or scala to download huggging face model and config.json. but meet connnect error , it is not make sense . so I want to know does huggingface have some more setting to download file ?
package torch.tr
import java.io.FileOutputStream
import java.net.URI
import java.net.http.{HttpClient, HttpRequest, HttpResponse}
import java.time.Duration
object HuggingFaceDownloader {
def main(args: Array[String]): Unit = {
val fileUrl = "https://huggingface.co/codellama/CodeLlama-7b-Instruct-hf/resolve/main/config.json"
val savePath = "config.json"
val headers = Map(
"Accept-Encoding" -> "identity",
// "user-agent" -> "transformers/0.0.1; java/23.0.2+7-58; hf_hub/null; java/23.0.2; file_type/config; from_autoclass/false; session_id/1AC306C59B944E9EA06A482682BE9584; unknown/None",
"authorization" -> "Bearer hf_XXAdogOLotfVSVFMKrWXSITeByDgRe"
)
try {
downloadFile(fileUrl, savePath, headers)
println(s"文件下载成功,保存路径: $savePath")
} catch {
case e: Exception =>
System.err.println(s"文件下载失败: ${e.getMessage}")
e.printStackTrace()
}
}
def downloadFile(fileUrl: String, savePath: String, headers: Map[String, String]): Unit = {
val client = HttpClient.newBuilder()
.connectTimeout(Duration.ofSeconds(10))
.followRedirects(HttpClient.Redirect.NORMAL)
.build()
val requestBuilder = HttpRequest.newBuilder()
.uri(URI.create(fileUrl))
.GET()
headers.foreach { case (key, value) =>
requestBuilder.header(key, value)
}
val request = requestBuilder.build()
val response = client.send(request, HttpResponse.BodyHandlers.ofInputStream())
if (response.statusCode() == 200) {
val inputStream = response.body()
val outputStream = new FileOutputStream(savePath)
try {
val buffer = new Array[Byte](4096)
var bytesRead = inputStream.read(buffer)
while (bytesRead != -1) {
outputStream.write(buffer, 0, bytesRead)
bytesRead = inputStream.read(buffer)
}
} finally {
inputStream.close()
outputStream.close()
}
} else {
throw new Exception(s"下载失败,状态码: ${response.statusCode()}")
}
}
}
```
```
package dev.transformers4j.transformers;
import java.io.BufferedInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
public class HuggingFaceDownloader2 {
public static void main(String[] args) {
String fileUrl = "https://huggingface.co/codellama/CodeLlama-7b-Instruct-hf/resolve/main/config.json";
String savePath = "config.json"; // 本地保存的文件路径
try {
downloadFile(fileUrl, savePath);
System.out.println("文件下载成功,保存路径: " + savePath);
} catch (IOException e) {
System.err.println("文件下载失败: " + e.getMessage());
e.printStackTrace();
}
}
/**
* 从指定 URL 下载文件并保存到本地路径
* @param fileUrl 要下载的文件的 URL
* @param savePath 本地保存的文件路径
* @throws IOException 如果在下载或保存文件过程中发生 I/O 错误
*/
public static void downloadFile(String fileUrl, String savePath) throws IOException {
URL url = new URL(fileUrl);
try (BufferedInputStream in = new BufferedInputStream(url.openStream());
FileOutputStream fileOutputStream = new FileOutputStream(savePath)) {
System.out.println("<UNK>: " + savePath);
byte[] dataBuffer = new byte[1024];
int bytesRead;
while ((bytesRead = in.read(dataBuffer, 0, 1024)) != -1) {
fileOutputStream.write(dataBuffer, 0, bytesRead);
}
}
}
}
```
Metadata
Metadata
Assignees
Labels
No labels