Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
public class TinyLlama extends OllamaContainer {
|
2 |
+
|
3 |
+
private final String imageName;
|
4 |
+
|
5 |
+
public TinyLlama(String imageName) {
|
6 |
+
super(DockerImageName.parse(imageName)
|
7 |
+
.asCompatibleSubstituteFor("ollama/ollama:0.1.44"));
|
8 |
+
this.imageName = imageName;
|
9 |
+
}
|
10 |
+
|
11 |
+
public void createImage(String imageName) {
|
12 |
+
var ollama = new OllamaContainer("ollama/ollama:0.1.44");
|
13 |
+
ollama.start();
|
14 |
+
try {
|
15 |
+
ollama.execInContainer("apt-get", "update");
|
16 |
+
ollama.execInContainer("apt-get", "upgrade", "-y");
|
17 |
+
ollama.execInContainer("apt-get", "install", "-y", "python3-pip");
|
18 |
+
ollama.execInContainer("pip", "install", "huggingface-hub");
|
19 |
+
ollama.execInContainer(
|
20 |
+
"huggingface-cli",
|
21 |
+
"download",
|
22 |
+
"DavidAU/DistiLabelOrca-TinyLLama-1.1B-Q8_0-GGUF",
|
23 |
+
"distilabelorca-tinyllama-1.1b.Q8_0.gguf",
|
24 |
+
"--local-dir",
|
25 |
+
"."
|
26 |
+
);
|
27 |
+
ollama.execInContainer(
|
28 |
+
"sh",
|
29 |
+
"-c",
|
30 |
+
String.format("echo '%s' > Modelfile", "FROM distilabelorca-tinyllama-1.1b.Q8_0.gguf")
|
31 |
+
);
|
32 |
+
ollama.execInContainer("ollama", "create", "distilabelorca-tinyllama-1.1b.Q8_0.gguf", "-f", "Modelfile");
|
33 |
+
ollama.execInContainer("rm", "distilabelorca-tinyllama-1.1b.Q8_0.gguf");
|
34 |
+
ollama.commitToImage(imageName);
|
35 |
+
} catch (IOException | InterruptedException e) {
|
36 |
+
throw new ContainerFetchException(e.getMessage());
|
37 |
+
}
|
38 |
+
}
|
39 |
+
|
40 |
+
public String getModelName() {
|
41 |
+
return "distilabelorca-tinyllama-1.1b.Q8_0.gguf";
|
42 |
+
}
|
43 |
+
|
44 |
+
@Override
|
45 |
+
public void start() {
|
46 |
+
try {
|
47 |
+
super.start();
|
48 |
+
} catch (ContainerFetchException ex) {
|
49 |
+
// If image doesn't exist, create it. Subsequent runs will reuse the image.
|
50 |
+
createImage(imageName);
|
51 |
+
super.start();
|
52 |
+
}
|
53 |
+
}
|
54 |
+
}
|
55 |
+
|
56 |
+
var tinyLlama = new TinyLlama("faq-ai");
|
57 |
+
tinyLlama.start();
|
58 |
+
String response = given()
|
59 |
+
.baseUri(tinyLlama.getEndpoint())
|
60 |
+
.header(new Header("Content-Type", "application/json"))
|
61 |
+
.body(new CompletionRequest(tinyLlama.getModelName() + ":latest", List.of(new Message("user", "What is the capital of France?")), false))
|
62 |
+
.post("/api/chat")
|
63 |
+
.getBody().as(ChatResponse.class).message.content;
|
64 |
+
System.out.println("Response from LLM " + response);
|