ananyagm commited on
Commit
c93cfd2
·
1 Parent(s): 7d847eb

Context-Informed-Descriptions

Browse files
Files changed (3) hide show
  1. Dockerfile +11 -0
  2. main.py +51 -0
  3. requirements.txt +2 -0
Dockerfile ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.9
2
+
3
+ WORKDIR /code
4
+
5
+ COPY ./requirements.txt /code/requirements.txt
6
+
7
+ RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
8
+
9
+ COPY . .
10
+
11
+ CMD ["gunicorn", "-b", "0.0.0.0:7860", "main:app", ]
main.py ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import tiktoken
3
+ import requests
4
+ from flask_cors import CORS, cross_origin
5
+ from PIL import Image
6
+ from transformers import CLIPProcessor, CLIPModel, pipeline, AutoTokenizer
7
+ from flask import Flask, request, jsonify
8
+
9
+ app = Flask(__name__)
10
+ CORS(app)
11
+
12
+ encoding = tiktoken.get_encoding("cl100k_base")
13
+ model = CLIPModel.from_pretrained("openai/clip-vit-base-patch32")
14
+ tokenizer = AutoTokenizer.from_pretrained("facebook/bart-large-cnn")
15
+ processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32")
16
+ summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
17
+
18
+ @app.route("/")
19
+
20
+ @app.route('/clip-scores', methods=['POST'])
21
+ @cross_origin(origin='chrome-extension://caglaokjfpffmkcbjknpolcjgjomamea')
22
+ def getClipScores():
23
+ if request.method == 'POST':
24
+ data = request.get_json()
25
+ scores = data.get('scores')
26
+ imageUrl = data.get('imageUrl')
27
+ image = Image.open(requests.get(imageUrl, stream=True).raw)
28
+ for entry in scores:
29
+ entry["summary"] = summarize_text(entry["text"])
30
+ texts = [entry['summary'] for entry in scores]
31
+ inputs = processor(text=texts, images=image, return_tensors="pt", truncation=True, padding=True)
32
+ outputs = model(**inputs)
33
+ logits_per_image = outputs.logits_per_image
34
+ probs = logits_per_image.softmax(dim=1).detach().cpu().numpy()
35
+ for i, entry in enumerate(scores):
36
+ entry['clip_score'] = float(probs[0, i])
37
+ sorted_scores = sorted(scores, key=lambda x: x['clip_score'], reverse= True)
38
+ print(jsonify("Sorted scores json", sorted_scores))
39
+ return jsonify(sorted_scores)
40
+
41
+ def summarize_text (text):
42
+ tokens = tokenizer(text, return_tensors='pt', truncation=True, max_length=tokenizer.model_max_length, padding="max_length")
43
+ input_ids = tokens.input_ids
44
+ if input_ids.size(1) > tokenizer.model_max_length:
45
+ summary = summarizer(text, max_length=77, min_length=20, do_sample=False)
46
+ return summary[0]['summary_text']
47
+ else:
48
+ return text
49
+
50
+ if __name__ == "__main__":
51
+ app.run(debug = True)
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ flask
2
+ gunicorn