HuggingFace-SK commited on
Commit
185303a
·
1 Parent(s): e2bab42

add LLM summarisation

Browse files
Files changed (3) hide show
  1. app.py +23 -2
  2. static/index.js +2 -1
  3. templates/index.html +1 -0
app.py CHANGED
@@ -7,10 +7,20 @@ from flask import Flask, jsonify, render_template, request, send_file
7
  import base64
8
  import numpy as np
9
  import easyocr
 
 
10
  app = Flask(__name__)
11
 
12
  reader = easyocr.Reader(['en'])
13
 
 
 
 
 
 
 
 
 
14
  @app.route("/")
15
  def index():
16
  return render_template("index.html")
@@ -19,6 +29,7 @@ def index():
19
  @app.route('/analyse', methods=['POST'])
20
  def analyse():
21
  frame = request.json.get('key')
 
22
  cv_frame = data_uri_to_image(frame)
23
  gray_image = cv2.cvtColor(cv_frame, cv2.COLOR_BGR2GRAY)
24
  blur = cv2.bilateralFilter(gray_image,4,20,20)
@@ -32,7 +43,7 @@ def analyse():
32
  results = reader.readtext(equalized)
33
  results_array =[]
34
  for (bbox, text, prob) in results:
35
- results_array.append(f"Detected text: {text} (Confidence: {prob})")
36
 
37
  # Iterate through the results and format them
38
  #cv2.imshow("threash", thresh_image)
@@ -40,8 +51,18 @@ def analyse():
40
  #cv2.imshow("blur", blur)
41
  #cv2.imshow("orig", gray_image)
42
  #cv2.waitKey(0)
 
 
 
 
 
43
 
44
- return str(results_array)
 
 
 
 
 
45
 
46
 
47
  def data_uri_to_image(data_uri):
 
7
  import base64
8
  import numpy as np
9
  import easyocr
10
+ from huggingface_hub import InferenceClient
11
+
12
  app = Flask(__name__)
13
 
14
  reader = easyocr.Reader(['en'])
15
 
16
+ HF_API_KEY=os.getenv("HF_API_KEY")
17
+
18
+ client = InferenceClient(api_key=HF_API_KEY)
19
+
20
+
21
+
22
+
23
+
24
  @app.route("/")
25
  def index():
26
  return render_template("index.html")
 
29
  @app.route('/analyse', methods=['POST'])
30
  def analyse():
31
  frame = request.json.get('key')
32
+ user_item_info = request.json.get('user_item_info')
33
  cv_frame = data_uri_to_image(frame)
34
  gray_image = cv2.cvtColor(cv_frame, cv2.COLOR_BGR2GRAY)
35
  blur = cv2.bilateralFilter(gray_image,4,20,20)
 
43
  results = reader.readtext(equalized)
44
  results_array =[]
45
  for (bbox, text, prob) in results:
46
+ results_array.append(f"[{text} (Confidence: {prob})]")
47
 
48
  # Iterate through the results and format them
49
  #cv2.imshow("threash", thresh_image)
 
51
  #cv2.imshow("blur", blur)
52
  #cv2.imshow("orig", gray_image)
53
  #cv2.waitKey(0)
54
+ item=user_item_info
55
+ messages = [
56
+ { "role": "system", "content": "You are a helpful nutritional information summarizer who is being used in a live application. You will be given the output of an OCR stage which has detected the text on a particular packaged food's label. You are tasked to give a comprehensive summary of the nutritional value, and ingredients if present. Comment on the health of the product.\nIf there are words you do not recognize, make educated guesses. At no point shall you refer to the inputted text or make users aware of any mistakes in character recognition. DO NOT REFER TO INPUT TEXT OR OCR " },
57
+ { "role": "user", "content": f"{item}: {results_array}" }
58
+ ]
59
 
60
+ completion = client.chat.completions.create(
61
+ model="Qwen/Qwen2.5-Coder-32B-Instruct",
62
+ messages=messages,
63
+ max_tokens=500
64
+ )
65
+ return str(completion.choices[0].message.content)
66
 
67
 
68
  def data_uri_to_image(data_uri):
static/index.js CHANGED
@@ -104,7 +104,8 @@ function clear_pic(){
104
 
105
  function send_frame(dataUrl) {
106
  const dataToSend = {
107
- key: dataUrl
 
108
  };
109
 
110
  fetch('/analyse', {
 
104
 
105
  function send_frame(dataUrl) {
106
  const dataToSend = {
107
+ key: dataUrl,
108
+ user_item_info: document.getElementById("user_item_info").value
109
  };
110
 
111
  fetch('/analyse', {
templates/index.html CHANGED
@@ -29,6 +29,7 @@ box-shadow: 0px 4px 20px 4px rgba(0, 0, 0, 0.38); z-index:100">
29
  </div>
30
  </div>
31
  <center>
 
32
  <button class="btn" id="take-pic">Click</button>
33
  <button class="btn" id="clear-pic" style="display:none">Clear ✖</button>
34
  <button class="btn" id="send-pic" style="display:none">Send ➤</button>
 
29
  </div>
30
  </div>
31
  <center>
32
+ <input class="btn" type="text" id="user_item_info" placeholder="Enter item name...">
33
  <button class="btn" id="take-pic">Click</button>
34
  <button class="btn" id="clear-pic" style="display:none">Clear ✖</button>
35
  <button class="btn" id="send-pic" style="display:none">Send ➤</button>