app updated L202 tokens per chunk
Browse files- app.py +2 -18
- utils/helpers.py +40 -1
app.py
CHANGED
@@ -31,22 +31,6 @@ palm.configure(api_key=api_key)
|
|
31 |
yolo_pipe = pipeline("object-detection", model="hustvl/yolos-small")
|
32 |
|
33 |
|
34 |
-
# Function to draw bounding boxes and labels on image
|
35 |
-
def draw_boxes(image, predictions):
|
36 |
-
draw = ImageDraw.Draw(image)
|
37 |
-
font = ImageFont.load_default()
|
38 |
-
|
39 |
-
for pred in predictions:
|
40 |
-
label = pred["label"]
|
41 |
-
score = pred["score"]
|
42 |
-
box = pred["box"]
|
43 |
-
xmin, ymin, xmax, ymax = box.values()
|
44 |
-
draw.rectangle([xmin, ymin, xmax, ymax], outline="red", width=2)
|
45 |
-
draw.text((xmin, ymin), f"{label} ({score:.2f})", fill="red", font=font)
|
46 |
-
|
47 |
-
return image
|
48 |
-
|
49 |
-
|
50 |
# Main function of the Streamlit app
|
51 |
def main():
|
52 |
st.title("Generative AI Demo on Camera Input/Image/PDF 💻")
|
@@ -215,7 +199,7 @@ def main():
|
|
215 |
# Tokenize it
|
216 |
st.warning("Start tokenzing ...")
|
217 |
token_splitter = SentenceTransformersTokenTextSplitter(
|
218 |
-
chunk_overlap=0, tokens_per_chunk=
|
219 |
)
|
220 |
token_split_texts = []
|
221 |
for text in character_split_texts:
|
@@ -227,7 +211,7 @@ def main():
|
|
227 |
|
228 |
# Generate a random string consisting of 10 uppercase letters and digits.
|
229 |
random_string: str = "".join(
|
230 |
-
np.random.choice(list(string.ascii_uppercase + string.digits), size=
|
231 |
)
|
232 |
|
233 |
# Combine the random number and random string into one identifier.
|
|
|
31 |
yolo_pipe = pipeline("object-detection", model="hustvl/yolos-small")
|
32 |
|
33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
# Main function of the Streamlit app
|
35 |
def main():
|
36 |
st.title("Generative AI Demo on Camera Input/Image/PDF 💻")
|
|
|
199 |
# Tokenize it
|
200 |
st.warning("Start tokenzing ...")
|
201 |
token_splitter = SentenceTransformersTokenTextSplitter(
|
202 |
+
chunk_overlap=0, tokens_per_chunk=20
|
203 |
)
|
204 |
token_split_texts = []
|
205 |
for text in character_split_texts:
|
|
|
211 |
|
212 |
# Generate a random string consisting of 10 uppercase letters and digits.
|
213 |
random_string: str = "".join(
|
214 |
+
np.random.choice(list(string.ascii_uppercase + string.digits), size=20)
|
215 |
)
|
216 |
|
217 |
# Combine the random number and random string into one identifier.
|
utils/helpers.py
CHANGED
@@ -7,7 +7,7 @@ from typing import Any, Dict, List
|
|
7 |
import pandas as pd
|
8 |
import requests
|
9 |
import streamlit as st
|
10 |
-
from PIL import Image
|
11 |
import google.generativeai as palm
|
12 |
from pypdf import PdfReader
|
13 |
from langchain.text_splitter import (
|
@@ -191,3 +191,42 @@ def displayPDF(file: str) -> None:
|
|
191 |
|
192 |
# Using Streamlit to display the HTML embed string as unsafe HTML
|
193 |
st.markdown(pdf_display, unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
import pandas as pd
|
8 |
import requests
|
9 |
import streamlit as st
|
10 |
+
from PIL import Image, ImageDraw, ImageFont
|
11 |
import google.generativeai as palm
|
12 |
from pypdf import PdfReader
|
13 |
from langchain.text_splitter import (
|
|
|
191 |
|
192 |
# Using Streamlit to display the HTML embed string as unsafe HTML
|
193 |
st.markdown(pdf_display, unsafe_allow_html=True)
|
194 |
+
|
195 |
+
|
196 |
+
def draw_boxes(image: Any, predictions: List[Dict[str, Any]]) -> Any:
|
197 |
+
"""
|
198 |
+
Draws bounding boxes and labels onto an image based on provided predictions.
|
199 |
+
|
200 |
+
Parameters:
|
201 |
+
- image (Any): The image to annotate, which should support the PIL drawing interface.
|
202 |
+
- predictions (List[Dict[str, Any]]): A list of predictions where each prediction is a dictionary
|
203 |
+
containing 'label', 'score', and 'box' keys. The 'box' is another dictionary with 'xmin',
|
204 |
+
'ymin', 'xmax', and 'ymax' as keys representing coordinates for the bounding box.
|
205 |
+
|
206 |
+
Returns:
|
207 |
+
- Any: The annotated image with bounding boxes and labels drawn on it.
|
208 |
+
|
209 |
+
Note:
|
210 |
+
- This function assumes that the incoming image supports the PIL ImageDraw interface.
|
211 |
+
- The function directly modifies the input image and returns it.
|
212 |
+
"""
|
213 |
+
# Create a drawing context from the image
|
214 |
+
draw = ImageDraw.Draw(image)
|
215 |
+
# Load a default font for text drawing
|
216 |
+
font = ImageFont.load_default()
|
217 |
+
|
218 |
+
# Loop through all predictions and draw boxes with labels
|
219 |
+
for pred in predictions:
|
220 |
+
# Extracting label and score from the prediction
|
221 |
+
label = pred["label"]
|
222 |
+
score = pred["score"]
|
223 |
+
# Extracting the bounding box coordinates
|
224 |
+
box = pred["box"]
|
225 |
+
xmin, ymin, xmax, ymax = box.values()
|
226 |
+
# Draw a rectangle over the image using the box's coordinates
|
227 |
+
draw.rectangle([xmin, ymin, xmax, ymax], outline="red", width=2)
|
228 |
+
# Annotate the image with label and score at the top-left corner of the bounding box
|
229 |
+
draw.text((xmin, ymin), f"{label} ({score:.2f})", fill="red", font=font)
|
230 |
+
|
231 |
+
# Return the annotated image
|
232 |
+
return image
|