AI-RESEARCHER-2024 commited on
Commit
26f6e80
·
verified ·
1 Parent(s): 64dc004

Upload 9 files

Browse files
springboot-app 2/.gitignore ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Compiled class file
2
+ *.class
3
+
4
+ # Log file
5
+ *.log
6
+
7
+ # BlueJ files
8
+ *.ctxt
9
+
10
+ # Mobile Tools for Java (J2ME)
11
+ .mtj.tmp/
12
+
13
+ # Package Files
14
+ *.jar
15
+ *.war
16
+ *.nar
17
+ *.ear
18
+ *.zip
19
+ *.tar.gz
20
+ *.rar
21
+
22
+ # virtual machine crash logs
23
+ hs_err_pid*
24
+
25
+ # Maven
26
+ target/
27
+ pom.xml.tag
28
+ pom.xml.releaseBackup
29
+ pom.xml.versionsBackup
30
+ pom.xml.next
31
+ release.properties
32
+ dependency-reduced-pom.xml
33
+ buildNumber.properties
34
+ .mvn/timing.properties
35
+ .mvn/wrapper/maven-wrapper.jar
36
+
37
+ # IDE - IntelliJ IDEA
38
+ .idea/
39
+ *.iws
40
+ *.iml
41
+ *.ipr
42
+ out/
43
+
44
+ # IDE - Eclipse
45
+ .apt_generated
46
+ .classpath
47
+ .factorypath
48
+ .project
49
+ .settings
50
+ .springBeans
51
+ .sts4-cache
52
+
53
+ # IDE - VSCode
54
+ .vscode/
55
+
56
+ # IDE - NetBeans
57
+ /nbproject/private/
58
+ /nbbuild/
59
+ /dist/
60
+ /nbdist/
61
+ /.nb-gradle/
62
+ build/
63
+
64
+ # OS Files
65
+ .DS_Store
66
+ Thumbs.db
67
+
68
+ # Application specific
69
+ application-local.properties
70
+ application-dev.properties
springboot-app 2/Dockerfile ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Multi-stage build for optimized image size
2
+ # Stage 1: Build the application
3
+ FROM maven:3.9-openjdk-17 AS builder
4
+
5
+ # Set working directory
6
+ WORKDIR /app
7
+
8
+ # Copy Maven files
9
+ COPY pom.xml .
10
+ COPY src ./src
11
+
12
+ # Build the application
13
+ RUN mvn clean package -DskipTests
14
+
15
+ # Stage 2: Run the application
16
+ FROM openjdk:17-jdk-slim
17
+
18
+ # Set working directory
19
+ WORKDIR /app
20
+
21
+ # Copy the JAR from builder stage
22
+ COPY --from=builder /app/target/*.jar app.jar
23
+
24
+ # Expose the port that Hugging Face expects (7860)
25
+ EXPOSE 7860
26
+
27
+ # Set the user to non-root for security
28
+ RUN useradd -m -u 1000 user && chown -R user:user /app
29
+ USER user
30
+
31
+ # Run the Spring Boot application
32
+ ENTRYPOINT ["java", "-jar", "/app/app.jar"]
springboot-app 2/HUGGINGFACE_DEPLOYMENT.md ADDED
@@ -0,0 +1,133 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Hugging Face Deployment Guide
2
+
3
+ ## Quick Start Deployment Steps
4
+
5
+ ### 1. Create a Hugging Face Account
6
+ - Go to [huggingface.co](https://huggingface.co) and sign up if you haven't already
7
+
8
+ ### 2. Create a New Space
9
+ 1. Click on your profile picture → "New Space"
10
+ 2. Give your Space a name (e.g., "springboot-demo")
11
+ 3. Select **Docker** as the SDK (important!)
12
+ 4. Choose the hardware (free CPU Basic is fine for this demo)
13
+ 5. Set visibility (public or private)
14
+ 6. Click "Create Space"
15
+
16
+ ### 3. Upload Your Files
17
+ You have two options:
18
+
19
+ #### Option A: Using Git
20
+ ```bash
21
+ # Clone your Space repository
22
+ git clone https://huggingface.co/spaces/YOUR_USERNAME/YOUR_SPACE_NAME
23
+
24
+ # Copy all the Spring Boot files to the cloned repository
25
+ cp -r springboot-app/* YOUR_SPACE_NAME/
26
+
27
+ # Navigate to the repository
28
+ cd YOUR_SPACE_NAME
29
+
30
+ # Add, commit, and push
31
+ git add .
32
+ git commit -m "Initial Spring Boot application"
33
+ git push
34
+ ```
35
+
36
+ #### Option B: Using Web Interface
37
+ 1. Go to your Space's "Files" tab
38
+ 2. Click "Add file" → "Upload files"
39
+ 3. Upload the following structure:
40
+ ```
41
+ Dockerfile
42
+ pom.xml
43
+ src/
44
+ ├── main/
45
+ │ ├── java/com/example/demo/
46
+ │ │ ├── DemoApplication.java
47
+ │ │ └── controller/GreetingController.java
48
+ │ └── resources/
49
+ │ └── application.properties
50
+ └── test/java/com/example/demo/
51
+ └── DemoApplicationTests.java
52
+ ```
53
+
54
+ ### 4. Wait for Build and Deployment
55
+ - Hugging Face will automatically detect the Dockerfile and start building
56
+ - You can monitor the build logs in the "Logs" tab
57
+ - The build typically takes 3-5 minutes
58
+
59
+ ### 5. Access Your Application
60
+ Once deployed, your application will be available at:
61
+ ```
62
+ https://YOUR_USERNAME-YOUR_SPACE_NAME.hf.space/api/
63
+ ```
64
+
65
+ ## Testing Your Endpoints
66
+
67
+ Once deployed, you can test your API endpoints:
68
+
69
+ ```bash
70
+ # Test the home endpoint
71
+ curl https://YOUR_USERNAME-YOUR_SPACE_NAME.hf.space/api/
72
+
73
+ # Test the greeting endpoint
74
+ curl https://YOUR_USERNAME-YOUR_SPACE_NAME.hf.space/api/greeting?name=HuggingFace
75
+
76
+ # Test the health endpoint
77
+ curl https://YOUR_USERNAME-YOUR_SPACE_NAME.hf.space/api/health
78
+
79
+ # Test the echo endpoint
80
+ curl -X POST https://YOUR_USERNAME-YOUR_SPACE_NAME.hf.space/api/echo \
81
+ -H "Content-Type: application/json" \
82
+ -d '{"message":"Hello from Hugging Face!"}'
83
+ ```
84
+
85
+ ## Troubleshooting
86
+
87
+ ### Common Issues and Solutions
88
+
89
+ 1. **Port Configuration Error**
90
+ - Ensure `server.port=7860` in application.properties
91
+ - Hugging Face requires port 7860
92
+
93
+ 2. **Build Fails**
94
+ - Check the Dockerfile syntax
95
+ - Ensure all file paths are correct
96
+ - Verify Java version compatibility
97
+
98
+ 3. **Application Doesn't Start**
99
+ - Check logs in the "Logs" tab
100
+ - Verify the JAR file name in Dockerfile matches the build output
101
+ - Ensure proper permissions (running as non-root user)
102
+
103
+ 4. **Slow Response Times**
104
+ - Free tier hardware has limited resources
105
+ - Consider upgrading to better hardware if needed
106
+
107
+ ## Environment Variables (Optional)
108
+
109
+ You can add environment variables in the Space settings:
110
+ 1. Go to Settings → "Repository secrets"
111
+ 2. Add variables like:
112
+ - `SPRING_PROFILES_ACTIVE=production`
113
+ - `JAVA_OPTS=-Xmx512m`
114
+
115
+ ## Updating Your Application
116
+
117
+ To update your deployed application:
118
+ 1. Make changes to your code locally
119
+ 2. Push updates to the Space repository
120
+ 3. Hugging Face will automatically rebuild and redeploy
121
+
122
+ ## Additional Resources
123
+
124
+ - [Hugging Face Spaces Documentation](https://huggingface.co/docs/hub/spaces)
125
+ - [Spring Boot Documentation](https://spring.io/projects/spring-boot)
126
+ - [Docker Documentation](https://docs.docker.com/)
127
+
128
+ ## Support
129
+
130
+ If you encounter issues:
131
+ 1. Check the Space logs first
132
+ 2. Visit Hugging Face forums
133
+ 3. Check Spring Boot community resources
springboot-app 2/README.md ADDED
@@ -0,0 +1,157 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Spring Boot Application for Hugging Face Spaces
2
+
3
+ A simple Spring Boot REST API application designed to be deployed on Hugging Face Spaces.
4
+
5
+ ## Features
6
+
7
+ - REST API with multiple endpoints
8
+ - Health check endpoint for monitoring
9
+ - Containerized with Docker
10
+ - Configured for Hugging Face Spaces deployment (port 7860)
11
+
12
+ ## API Endpoints
13
+
14
+ ### GET /api/
15
+ Returns a welcome message with current status.
16
+
17
+ **Response:**
18
+ ```json
19
+ {
20
+ "message": "Welcome to Spring Boot on Hugging Face!",
21
+ "status": "running",
22
+ "timestamp": "2024-11-13T10:30:00"
23
+ }
24
+ ```
25
+
26
+ ### GET /api/greeting
27
+ Returns a personalized greeting.
28
+
29
+ **Parameters:**
30
+ - `name` (optional): Name for the greeting (default: "World")
31
+
32
+ **Example:** `/api/greeting?name=John`
33
+
34
+ **Response:**
35
+ ```json
36
+ {
37
+ "greeting": "Hello, John!",
38
+ "timestamp": "2024-11-13T10:30:00"
39
+ }
40
+ ```
41
+
42
+ ### GET /api/health
43
+ Health check endpoint.
44
+
45
+ **Response:**
46
+ ```json
47
+ {
48
+ "status": "UP",
49
+ "service": "Spring Boot Demo"
50
+ }
51
+ ```
52
+
53
+ ### POST /api/echo
54
+ Echoes back the received JSON payload.
55
+
56
+ **Request Body:**
57
+ ```json
58
+ {
59
+ "message": "test",
60
+ "data": "example"
61
+ }
62
+ ```
63
+
64
+ **Response:**
65
+ ```json
66
+ {
67
+ "received": {
68
+ "message": "test",
69
+ "data": "example"
70
+ },
71
+ "timestamp": "2024-11-13T10:30:00"
72
+ }
73
+ ```
74
+
75
+ ## Local Development
76
+
77
+ ### Prerequisites
78
+ - Java 17 or higher
79
+ - Maven 3.6 or higher
80
+
81
+ ### Running Locally
82
+
83
+ 1. Clone the repository
84
+ 2. Navigate to the project directory
85
+ 3. Run the application:
86
+ ```bash
87
+ mvn spring-boot:run
88
+ ```
89
+ 4. Access the application at `http://localhost:7860/api/`
90
+
91
+ ### Building the JAR
92
+ ```bash
93
+ mvn clean package
94
+ ```
95
+
96
+ ### Running with Docker
97
+ ```bash
98
+ # Build the Docker image
99
+ docker build -t springboot-hf-demo .
100
+
101
+ # Run the container
102
+ docker run -p 7860:7860 springboot-hf-demo
103
+ ```
104
+
105
+ ## Deploying to Hugging Face Spaces
106
+
107
+ 1. Create a new Space on Hugging Face
108
+ 2. Choose "Docker" as the SDK
109
+ 3. Upload these files to your Space repository:
110
+ - `Dockerfile`
111
+ - `pom.xml`
112
+ - `src/` directory with all Java files and resources
113
+ - `README.md`
114
+
115
+ 4. The Space will automatically build and deploy your application
116
+
117
+ ### Hugging Face Configuration
118
+
119
+ The application is configured to:
120
+ - Listen on port 7860 (required by Hugging Face)
121
+ - Bind to all network interfaces (0.0.0.0)
122
+ - Run as a non-root user for security
123
+
124
+ ## Project Structure
125
+
126
+ ```
127
+ springboot-app/
128
+ ├── src/
129
+ │ └── main/
130
+ │ ├── java/
131
+ │ │ └── com/example/demo/
132
+ │ │ ├── DemoApplication.java
133
+ │ │ └── controller/
134
+ │ │ └── GreetingController.java
135
+ │ └── resources/
136
+ │ └── application.properties
137
+ ├── Dockerfile
138
+ ├── pom.xml
139
+ └── README.md
140
+ ```
141
+
142
+ ## Technologies Used
143
+
144
+ - Spring Boot 3.2.1
145
+ - Java 17
146
+ - Maven
147
+ - Docker
148
+ - Spring Web
149
+ - Spring Boot Actuator
150
+
151
+ ## License
152
+
153
+ This project is open source and available for educational purposes.
154
+
155
+ ## Contributing
156
+
157
+ Feel free to fork this project and customize it for your needs!
springboot-app 2/pom.xml ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project xmlns="http://maven.apache.org/POM/4.0.0"
3
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
5
+ http://maven.apache.org/xsd/maven-4.0.0.xsd">
6
+ <modelVersion>4.0.0</modelVersion>
7
+
8
+ <parent>
9
+ <groupId>org.springframework.boot</groupId>
10
+ <artifactId>spring-boot-starter-parent</artifactId>
11
+ <version>3.2.1</version>
12
+ <relativePath/>
13
+ </parent>
14
+
15
+ <groupId>com.example</groupId>
16
+ <artifactId>demo</artifactId>
17
+ <version>0.0.1-SNAPSHOT</version>
18
+ <name>demo</name>
19
+ <description>Demo Spring Boot project for Hugging Face</description>
20
+
21
+ <properties>
22
+ <java.version>17</java.version>
23
+ </properties>
24
+
25
+ <dependencies>
26
+ <dependency>
27
+ <groupId>org.springframework.boot</groupId>
28
+ <artifactId>spring-boot-starter-web</artifactId>
29
+ </dependency>
30
+
31
+ <dependency>
32
+ <groupId>org.springframework.boot</groupId>
33
+ <artifactId>spring-boot-starter-actuator</artifactId>
34
+ </dependency>
35
+
36
+ <dependency>
37
+ <groupId>org.springframework.boot</groupId>
38
+ <artifactId>spring-boot-devtools</artifactId>
39
+ <scope>runtime</scope>
40
+ <optional>true</optional>
41
+ </dependency>
42
+
43
+ <dependency>
44
+ <groupId>org.springframework.boot</groupId>
45
+ <artifactId>spring-boot-starter-test</artifactId>
46
+ <scope>test</scope>
47
+ </dependency>
48
+ </dependencies>
49
+
50
+ <build>
51
+ <plugins>
52
+ <plugin>
53
+ <groupId>org.springframework.boot</groupId>
54
+ <artifactId>spring-boot-maven-plugin</artifactId>
55
+ </plugin>
56
+ </plugins>
57
+ </build>
58
+ </project>
springboot-app 2/src/main/java/com/example/demo/DemoApplication.java ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package com.example.demo;
2
+
3
+ import org.springframework.boot.SpringApplication;
4
+ import org.springframework.boot.autoconfigure.SpringBootApplication;
5
+
6
+ @SpringBootApplication
7
+ public class DemoApplication {
8
+ public static void main(String[] args) {
9
+ SpringApplication.run(DemoApplication.class, args);
10
+ }
11
+ }
springboot-app 2/src/main/java/com/example/demo/controller/GreetingController.java ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package com.example.demo.controller;
2
+
3
+ import org.springframework.web.bind.annotation.*;
4
+ import java.time.LocalDateTime;
5
+ import java.util.HashMap;
6
+ import java.util.Map;
7
+
8
+ @RestController
9
+ @RequestMapping("/api")
10
+ public class GreetingController {
11
+
12
+ @GetMapping("/")
13
+ public Map<String, String> home() {
14
+ Map<String, String> response = new HashMap<>();
15
+ response.put("message", "Welcome to Spring Boot on Hugging Face!");
16
+ response.put("status", "running");
17
+ response.put("timestamp", LocalDateTime.now().toString());
18
+ return response;
19
+ }
20
+
21
+ @GetMapping("/greeting")
22
+ public Map<String, String> greeting(@RequestParam(value = "name", defaultValue = "World") String name) {
23
+ Map<String, String> response = new HashMap<>();
24
+ response.put("greeting", "Hello, " + name + "!");
25
+ response.put("timestamp", LocalDateTime.now().toString());
26
+ return response;
27
+ }
28
+
29
+ @GetMapping("/health")
30
+ public Map<String, String> health() {
31
+ Map<String, String> response = new HashMap<>();
32
+ response.put("status", "UP");
33
+ response.put("service", "Spring Boot Demo");
34
+ return response;
35
+ }
36
+
37
+ @PostMapping("/echo")
38
+ public Map<String, Object> echo(@RequestBody Map<String, Object> payload) {
39
+ Map<String, Object> response = new HashMap<>();
40
+ response.put("received", payload);
41
+ response.put("timestamp", LocalDateTime.now().toString());
42
+ return response;
43
+ }
44
+ }
springboot-app 2/src/main/resources/application.properties ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Server Configuration
2
+ server.port=7860
3
+ server.address=0.0.0.0
4
+
5
+ # Application Name
6
+ spring.application.name=SpringBoot-HuggingFace-Demo
7
+
8
+ # Actuator Configuration
9
+ management.endpoints.web.exposure.include=health,info
10
+ management.endpoint.health.show-details=always
11
+
12
+ # Logging
13
+ logging.level.root=INFO
14
+ logging.level.com.example.demo=DEBUG
15
+
16
+ # JSON Configuration
17
+ spring.jackson.serialization.indent-output=true
18
+
19
+ # Error Handling
20
+ server.error.include-message=always
21
+ server.error.include-stacktrace=never
springboot-app 2/src/test/java/com/example/demo/DemoApplicationTests.java ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package com.example.demo;
2
+
3
+ import org.junit.jupiter.api.Test;
4
+ import org.springframework.beans.factory.annotation.Autowired;
5
+ import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
6
+ import org.springframework.boot.test.context.SpringBootTest;
7
+ import org.springframework.test.web.servlet.MockMvc;
8
+
9
+ import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
10
+ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
11
+ import static org.hamcrest.Matchers.*;
12
+
13
+ @SpringBootTest
14
+ @AutoConfigureMockMvc
15
+ class DemoApplicationTests {
16
+
17
+ @Autowired
18
+ private MockMvc mockMvc;
19
+
20
+ @Test
21
+ void contextLoads() {
22
+ }
23
+
24
+ @Test
25
+ void testHomeEndpoint() throws Exception {
26
+ mockMvc.perform(get("/api/"))
27
+ .andExpect(status().isOk())
28
+ .andExpect(jsonPath("$.message").value("Welcome to Spring Boot on Hugging Face!"))
29
+ .andExpect(jsonPath("$.status").value("running"));
30
+ }
31
+
32
+ @Test
33
+ void testGreetingEndpoint() throws Exception {
34
+ mockMvc.perform(get("/api/greeting").param("name", "Test"))
35
+ .andExpect(status().isOk())
36
+ .andExpect(jsonPath("$.greeting").value("Hello, Test!"));
37
+ }
38
+
39
+ @Test
40
+ void testHealthEndpoint() throws Exception {
41
+ mockMvc.perform(get("/api/health"))
42
+ .andExpect(status().isOk())
43
+ .andExpect(jsonPath("$.status").value("UP"));
44
+ }
45
+ }