sjbaek commited on
Commit
ac984b5
1 Parent(s): 89df510

feat : runpod serverless application

Browse files
Files changed (4) hide show
  1. Dockerfile +16 -0
  2. pipeline.py +41 -0
  3. rp_handler.py +42 -0
  4. test_run.py +6 -0
Dockerfile ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.11.1-buster
2
+
3
+ WORKDIR /
4
+
5
+ COPY requirements.txt .
6
+ RUN pip install -r requirements.txt
7
+
8
+ COPY GemmaArte/ GemmaArte/
9
+ COPY paligemma-3b-pt-224/ paligemma-3b-pt-224/
10
+
11
+ #COPY pipeline.py .
12
+ #COPY rp_handler.py .
13
+ COPY test_run.py .
14
+
15
+ #CMD [ "python", "-u" ,"./rp_handler.py" ]
16
+ CMD [ "python", "-u" ,"./test_run.py" ]
pipeline.py ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from transformers import AutoProcessor, PaliGemmaForConditionalGeneration
3
+
4
+ visual_analysis = {
5
+ "General Visual Analysis": "Create a detailed and cohesive analysis paragraph focusing exclusively on the visual characteristics, ensuring clarity and thorough examination.",
6
+ "Form and Shape": "Provide a focused analysis that critically examines the form and shape of the object, highlighting its visual impact and structural elements.",
7
+ "Symbolism and Iconography": "Explore the symbolism and iconography through an in-depth visual analysis, identifying significant symbols and their interpretative meanings.",
8
+ "Composition": "Conduct a visual analysis that emphasizes the compositional elements, examining the arrangement and structural balance of the artwork.",
9
+ "Light and Shadow": "Evaluate the effects of light and shadow through a detailed analysis, focusing on how these elements enhance the visual dynamics.",
10
+ "Texture": "Conduct a visual analysis of texture, emphasizing the surface qualities and tactile illusions presented in the piece.",
11
+ "Movement and Gesture": "Analyze the movement and gesture within the work, highlighting how these visual cues suggest motion and expression.",
12
+ "Color Palette": "Examine the color palette through an exclusive visual analysis, focusing on color harmony and emotional tone.",
13
+ "Line Quality": "Analyze the line quality, exploring the visual characteristics and expressiveness conveyed through line variation.",
14
+ "Perspective": "Conduct a study of perspective, analyzing how depth and spatial relationships are visually represented.",
15
+ "Scale and Proportion": "Evaluate the scale and proportion within the composition, analyzing how size relationships affect the visual coherence."
16
+ }
17
+
18
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
19
+
20
+ model_id = "/paligemma-3b-pt-224"
21
+ model_path = "/GemmArte"
22
+
23
+ print("Loading model...")
24
+ processor = AutoProcessor.from_pretrained(model_id)
25
+ model = PaliGemmaForConditionalGeneration.from_pretrained(model_path)
26
+ print("Model loaded.")
27
+ print("Moving model to device...")
28
+ model.to(device)
29
+
30
+
31
+ def generate(image, category: str, max_new_tokens=512) -> str:
32
+ prompt = visual_analysis.get(category)
33
+ if not prompt:
34
+ # Default to general visual analysis
35
+ prompt = visual_analysis["General Visual Analysis"]
36
+
37
+ inputs = processor(prompt, image, return_tensors="pt").to(device)
38
+ with torch.no_grad():
39
+ output = model.generate(**inputs, max_new_tokens=max_new_tokens)
40
+
41
+ return processor.decode(output[0], skip_special_tokens=True)[len(prompt):]
rp_handler.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import runpod
2
+ import base64
3
+ from io import BytesIO
4
+ from PIL import Image
5
+ from pipeline import generate
6
+
7
+
8
+ def decode_to_image_obj(base64_string):
9
+ return Image.open(BytesIO(base64.b64decode(base64_string)))
10
+
11
+
12
+ def process_input(input):
13
+ """
14
+ Execute the application code
15
+ """
16
+ max_new_tokens = input['max_new_tokens']
17
+ category = input['category']
18
+ base64_string = input['image']
19
+
20
+ image = decode_to_image_obj(base64_string)
21
+
22
+ result = generate(decode_to_image_obj(image), category, max_new_tokens)
23
+ result = "This is a placeholder result."
24
+
25
+ return {
26
+ "result": result
27
+ }
28
+
29
+
30
+ # ---------------------------------------------------------------------------- #
31
+ # RunPod Handler #
32
+ # ---------------------------------------------------------------------------- #
33
+ def handler(event):
34
+ """
35
+ This is the handler function that will be called by RunPod serverless.
36
+ """
37
+ return process_input(event['input'])
38
+
39
+
40
+ if __name__ == '__main__':
41
+ print("Starting RunPod serverless worker.")
42
+ runpod.serverless.start({'handler': handler})
test_run.py ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ import time
2
+
3
+ if __name__ == "__main__":
4
+ while True:
5
+ print("Test Running...")
6
+ time.sleep(3)