osbm commited on
Commit
a6c2dde
·
1 Parent(s): 2abc55e
Files changed (4) hide show
  1. Dockerfile +11 -1
  2. app.py +36 -0
  3. flake.lock +27 -0
  4. flake.nix +94 -0
Dockerfile CHANGED
@@ -1,3 +1,13 @@
1
  FROM nixos/nix:2.31.2
2
 
3
- ADD . .
 
 
 
 
 
 
 
 
 
 
 
1
  FROM nixos/nix:2.31.2
2
 
3
+ # Enable experimental features for flakes
4
+ RUN echo "experimental-features = nix-command flakes" >> /etc/nix/nix.conf
5
+
6
+ # Copy the project files
7
+ COPY . /workspace
8
+ WORKDIR /workspace
9
+
10
+ RUN nix build .#default --out-link /workspace/result
11
+
12
+ # Run the startup script
13
+ CMD ["/workspace/result/bin/run-gradio"]
app.py ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import os
3
+ import sys
4
+
5
+ def greet(name):
6
+ return f"Hello, {name}!"
7
+
8
+ if __name__ == "__main__":
9
+ # Get port from environment or default to 7860
10
+ port = int(os.environ.get("PORT", "7860"))
11
+
12
+ # Parse command line arguments
13
+ server_name = "0.0.0.0"
14
+ server_port = port
15
+
16
+ for i, arg in enumerate(sys.argv[1:]):
17
+ if arg == "--server-port" and i + 1 < len(sys.argv) - 1:
18
+ server_port = int(sys.argv[i + 2])
19
+ elif arg == "--server-name" and i + 1 < len(sys.argv) - 1:
20
+ server_name = sys.argv[i + 2]
21
+
22
+ print(f"Launching Gradio app on {server_name}:{server_port}")
23
+
24
+ iface = gr.Interface(
25
+ fn=greet,
26
+ inputs="text",
27
+ outputs="text",
28
+ title="Greeting App",
29
+ description="Enter your name to receive a greeting."
30
+ )
31
+
32
+ iface.launch(
33
+ server_name=server_name,
34
+ server_port=server_port,
35
+ share=False
36
+ )
flake.lock ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "nodes": {
3
+ "nixpkgs": {
4
+ "locked": {
5
+ "lastModified": 1759417375,
6
+ "narHash": "sha256-O7eHcgkQXJNygY6AypkF9tFhsoDQjpNEojw3eFs73Ow=",
7
+ "owner": "nixos",
8
+ "repo": "nixpkgs",
9
+ "rev": "dc704e6102e76aad573f63b74c742cd96f8f1e6c",
10
+ "type": "github"
11
+ },
12
+ "original": {
13
+ "owner": "nixos",
14
+ "ref": "nixpkgs-unstable",
15
+ "repo": "nixpkgs",
16
+ "type": "github"
17
+ }
18
+ },
19
+ "root": {
20
+ "inputs": {
21
+ "nixpkgs": "nixpkgs"
22
+ }
23
+ }
24
+ },
25
+ "root": "root",
26
+ "version": 7
27
+ }
flake.nix ADDED
@@ -0,0 +1,94 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ description = "Huggingface space flake";
3
+ inputs.nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable";
4
+ outputs = { self, nixpkgs }:
5
+ let
6
+ supportedSystems = [ "x86_64-linux" "x86_64-darwin" "aarch64-linux" "aarch64-darwin" ];
7
+ forAllSystems = nixpkgs.lib.genAttrs supportedSystems;
8
+ genPkgs = forAllSystems (system: nixpkgs.legacyPackages.${system});
9
+
10
+ # Extract values from README.md front matter
11
+ readmeContent = builtins.readFile ./README.md;
12
+ lines = builtins.split "\n" readmeContent;
13
+
14
+ # Helper function to extract YAML front matter values
15
+ extractYamlValue = key:
16
+ let
17
+ matchingLines = builtins.filter (line:
18
+ builtins.isString line && builtins.match "${key}: .*" line != null
19
+ ) lines;
20
+ in
21
+ if matchingLines != [] then
22
+ let match = builtins.match "${key}: (.*)" (builtins.head matchingLines);
23
+ in if match != null then builtins.head match else null
24
+ else null;
25
+
26
+ sdk = extractYamlValue "sdk";
27
+ appPort = extractYamlValue "app_port";
28
+ in
29
+ {
30
+ packages = forAllSystems (system: let
31
+ pkgs = genPkgs.${system};
32
+
33
+ pythonEnv = pkgs.python3.withPackages (ps: with ps; [
34
+ gradio
35
+ ]);
36
+ in {
37
+ default = pkgs.stdenv.mkDerivation {
38
+ pname = "gradio-app";
39
+ version = "0.1.0";
40
+ src = ./.;
41
+ buildInputs = with pkgs; [
42
+ pythonEnv
43
+ ];
44
+ dontBuild = true;
45
+ installPhase = ''
46
+ mkdir -p $out/bin
47
+ cp ${./app.py} $out/bin/app.py
48
+
49
+ # Create bash script to run gradio with python
50
+ cat > $out/bin/run-gradio << 'EOF'
51
+ #!/usr/bin/env bash
52
+ set -euo pipefail
53
+
54
+ # Set default port if not provided
55
+ PORT=''${PORT:-${appPort}}
56
+
57
+ echo "Starting Gradio app on port $PORT..."
58
+ cd "$(dirname "$0")"
59
+ ${pythonEnv}/bin/python app.py --server-port "$PORT" --server-name 0.0.0.0
60
+ EOF
61
+
62
+ chmod +x $out/bin/run-gradio
63
+ '';
64
+ meta.mainProgram = "run-gradio";
65
+ };
66
+ gradio-docker = pkgs.dockerTools.buildImage {
67
+ name = "gradio-app-image";
68
+ tag = "latest";
69
+ contents = [ self.packages.${system}.default ];
70
+ config = {
71
+ Cmd = [ "${self.packages.${system}.default}/bin/run-gradio" ];
72
+ ExposedPorts = {
73
+ "${appPort}/tcp" = {};
74
+ };
75
+ Env = [
76
+ "PORT=${appPort}"
77
+ "GRADIO_SERVER_NAME=0.0.0.0"
78
+ ];
79
+ };
80
+ };
81
+ });
82
+
83
+ # devShells = forAllSystems (system: let
84
+ # in {
85
+ # default = pkgs.${system}.mkShell {
86
+ # buildInputs = with pkgs.${system}; [
87
+ # (python3.withPackages (ps: with ps; [
88
+ # gradio
89
+ # ]))
90
+ # ];
91
+ # };
92
+ # });
93
+ };
94
+ }