Spaces:
Sleeping
Sleeping
kwabs22
commited on
Commit
•
f03a15c
1
Parent(s):
69d6dab
initial commit
Browse files- Dockerfile +20 -0
- app.py +56 -0
- requirements.txt +7 -0
Dockerfile
ADDED
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Use an official Python runtime as a parent image
|
2 |
+
FROM python:3.8-slim
|
3 |
+
|
4 |
+
# Set the working directory in the container
|
5 |
+
WORKDIR /usr/src/app
|
6 |
+
|
7 |
+
# Copy the current directory contents into the container at /usr/src/app
|
8 |
+
COPY . .
|
9 |
+
|
10 |
+
# Install any needed packages specified in requirements.txt
|
11 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
12 |
+
|
13 |
+
# Make port 7860 available to the world outside this container
|
14 |
+
EXPOSE 7860
|
15 |
+
|
16 |
+
# Define environment variable
|
17 |
+
ENV NAME World
|
18 |
+
|
19 |
+
# Run app.py when the container launches
|
20 |
+
CMD ["python", "app.py"]
|
app.py
ADDED
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import AutoModelForCausalLM, CodeGenTokenizerFast as Tokenizer
|
3 |
+
from PIL import Image
|
4 |
+
import gc
|
5 |
+
from pympler import asizeof
|
6 |
+
|
7 |
+
# Function to clear model from memory
|
8 |
+
def clear_model(model):
|
9 |
+
del model
|
10 |
+
gc.collect()
|
11 |
+
|
12 |
+
|
13 |
+
def process_image_and_question(image, question):
|
14 |
+
# Placeholder for your image processing and question answering
|
15 |
+
# Replace this with your actual model processing
|
16 |
+
# For example:
|
17 |
+
# enc_image = model.encode_image(image)
|
18 |
+
# answer = model.answer_question(enc_image, question, tokenizer)
|
19 |
+
# return answer
|
20 |
+
|
21 |
+
FinalOutput = ""
|
22 |
+
model_id = "vikhyatk/moondream1"
|
23 |
+
|
24 |
+
if question == "":
|
25 |
+
question = "describe this image?"
|
26 |
+
|
27 |
+
# Check if the model is already loaded
|
28 |
+
try:
|
29 |
+
model
|
30 |
+
except NameError:
|
31 |
+
# clear_model(model) # Example of clearing the model
|
32 |
+
model = AutoModelForCausalLM.from_pretrained(model_id, trust_remote_code=True)
|
33 |
+
tokenizer = Tokenizer.from_pretrained(model_id)
|
34 |
+
|
35 |
+
# Assuming you have a correct way to process the image
|
36 |
+
#image = Image.open('/content/_57e22ed5-217c-4004-a279-eeecc18cbd55.jpg') #/content/Bard_Generated_Image (3).jpg')
|
37 |
+
# This part of the code is incorrect for a standard transformers model
|
38 |
+
enc_image = model.encode_image(image)
|
39 |
+
FinalOutput += model.answer_question(enc_image, "how many people are there? also explain if the image is weird?", tokenizer)
|
40 |
+
|
41 |
+
model_size = asizeof.asizeof(model)
|
42 |
+
tokenizer_size = asizeof.asizeof(tokenizer)
|
43 |
+
FinalOutput += f"\nModel size in RAM: {model_size} bytes, Tokenizer size in RAM: {tokenizer_size} bytes"
|
44 |
+
|
45 |
+
#model load and set-up = 1 min and inference on CPU = 2 min
|
46 |
+
return FinalOutput
|
47 |
+
|
48 |
+
# Define Gradio interface
|
49 |
+
iface = gr.Interface(fn=process_image_and_question,
|
50 |
+
inputs=[gr.Image(type="pil"), gr.Textbox(lines=2, placeholder="Ask a question about the image...")],
|
51 |
+
outputs="text",
|
52 |
+
title="Image Question Answering",
|
53 |
+
description="Upload an image and ask a question about it. ( 2 - 3 min response time expected )")
|
54 |
+
|
55 |
+
# Launch the interface
|
56 |
+
iface.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
gradio
|
2 |
+
transformers
|
3 |
+
torch
|
4 |
+
Pillow
|
5 |
+
einops
|
6 |
+
timm #dependencies of moondream1 (Trust remote code is on)
|
7 |
+
pympler #analyze the memory behavior of Python objects
|