Spaces:
Runtime error
Runtime error
TYH71
commited on
Commit
·
b6faa5c
1
Parent(s):
ed045b2
add: initial commit
Browse files- .gitignore +2 -0
- Dockerfile +22 -0
- app.py +20 -0
- requirements.txt +7 -0
- src/__init__.py +0 -0
- src/core/__init__.py +0 -0
- src/core/logger.py +8 -0
- src/core/singleton.py +8 -0
- src/interface/__init__.py +0 -0
- src/interface/clip.py +34 -0
.gitignore
CHANGED
@@ -158,3 +158,5 @@ cython_debug/
|
|
158 |
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
159 |
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
160 |
#.idea/
|
|
|
|
|
|
158 |
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
159 |
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
160 |
#.idea/
|
161 |
+
|
162 |
+
model/
|
Dockerfile
ADDED
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
FROM python:3.10.8-slim-buster
|
2 |
+
|
3 |
+
ARG DEBIAN_FRONTEND=noninteractive
|
4 |
+
|
5 |
+
WORKDIR /app
|
6 |
+
COPY . /app
|
7 |
+
|
8 |
+
RUN apt-get update && apt-get install -y \
|
9 |
+
ffmpeg libsm6 libxext6
|
10 |
+
RUN apt-get clean && rm -rf /tmp/* /var/tmp/*
|
11 |
+
|
12 |
+
RUN python -m pip install --upgrade pip
|
13 |
+
RUN python -m pip install -r requirements.txt
|
14 |
+
|
15 |
+
ENV LC_ALL C.UTF-8
|
16 |
+
ENV LANG C.UTF-8
|
17 |
+
|
18 |
+
# container listens to port 7860
|
19 |
+
EXPOSE 7860
|
20 |
+
|
21 |
+
# Start Gradio application
|
22 |
+
CMD ["python", "app.py", "--host", "0.0.0.0", "--port", "7860"]
|
app.py
ADDED
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
'''
|
2 |
+
main script for gradio application
|
3 |
+
'''
|
4 |
+
import argparse
|
5 |
+
import gradio as gr
|
6 |
+
|
7 |
+
# modules
|
8 |
+
from src.core.logger import logger
|
9 |
+
from src.interface.clip import demo_interface
|
10 |
+
|
11 |
+
demo = demo_interface
|
12 |
+
|
13 |
+
if __name__ == "__main__":
|
14 |
+
parser = argparse.ArgumentParser()
|
15 |
+
parser.add_argument("--host", default="0.0.0.0", type=str)
|
16 |
+
parser.add_argument(
|
17 |
+
"--port", help="will start gradio app on this port (if available)", default=7860, type=int)
|
18 |
+
args_all = parser.parse_args()
|
19 |
+
logger.info("Gradio Application live and running !")
|
20 |
+
demo.queue().launch(share=False, server_name=args_all.host, server_port=args_all.port)
|
requirements.txt
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
gradio==3.36.1
|
2 |
+
open-clip-torch==2.20.0
|
3 |
+
Pillow==9.5.0
|
4 |
+
|
5 |
+
--find-links https://download.pytorch.org/whl/torch_stable.html
|
6 |
+
torch==2.0.1+cpu
|
7 |
+
torchvision==0.15.2+cpu
|
src/__init__.py
ADDED
File without changes
|
src/core/__init__.py
ADDED
File without changes
|
src/core/logger.py
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
'''
|
2 |
+
Setting up a logger.
|
3 |
+
'''
|
4 |
+
import logging
|
5 |
+
|
6 |
+
logger = logging.getLogger(__name__)
|
7 |
+
logger.setLevel(logging.DEBUG)
|
8 |
+
logger.addHandler(logging.StreamHandler())
|
src/core/singleton.py
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
class SingletonMeta(type):
|
2 |
+
_instances = {}
|
3 |
+
|
4 |
+
def __call__(cls, *args, **kwargs):
|
5 |
+
if cls not in cls._instances:
|
6 |
+
instance = super().__call__(*args, **kwargs)
|
7 |
+
cls._instances[cls] = instance
|
8 |
+
return cls._instances[cls]
|
src/interface/__init__.py
ADDED
File without changes
|
src/interface/clip.py
ADDED
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# libraries
|
2 |
+
from typing import List, Dict
|
3 |
+
import gradio as gr
|
4 |
+
from PIL import Image
|
5 |
+
|
6 |
+
# modules
|
7 |
+
from src.core.logger import logger
|
8 |
+
from src.model.clip import CLIP_Model
|
9 |
+
|
10 |
+
MODEL = CLIP_Model(jit=True)
|
11 |
+
|
12 |
+
def clean_text(text: str) -> List[str]:
|
13 |
+
"""function to clean gradio input text, remove the trailing and leading spaces"""
|
14 |
+
return list(map(lambda x: x.strip(), text.split(",")))
|
15 |
+
|
16 |
+
def clip_demo_fn(image: Image.Image, text: str) -> Dict[str, float]:
|
17 |
+
try:
|
18 |
+
logger.info("demo function invoked")
|
19 |
+
text = clean_text(text)
|
20 |
+
logger.debug("clean text: %s", text)
|
21 |
+
return MODEL(image, text)
|
22 |
+
except Exception as error_msg:
|
23 |
+
logger.error("Error caught: %s", error_msg)
|
24 |
+
finally:
|
25 |
+
logger.info("demo function completed")
|
26 |
+
|
27 |
+
demo_interface = gr.Interface(
|
28 |
+
fn=clip_demo_fn,
|
29 |
+
inputs=[
|
30 |
+
gr.Image(type='pil', label="Image"),
|
31 |
+
"text"
|
32 |
+
],
|
33 |
+
outputs=gr.Label()
|
34 |
+
)
|