Commit
·
b4df4ef
1
Parent(s):
4e3c349
changed getReg from 2 LLM calls to 1 LLM call
Browse files
app.py
CHANGED
|
@@ -8,7 +8,7 @@ import requests
|
|
| 8 |
from io import BytesIO
|
| 9 |
import base64
|
| 10 |
import re
|
| 11 |
-
|
| 12 |
|
| 13 |
# Load environment variables from .env
|
| 14 |
load_dotenv()
|
|
@@ -28,6 +28,11 @@ HF_TOKEN = os.getenv("HF_TOKEN")
|
|
| 28 |
|
| 29 |
#login(token=HF_TOKEN)
|
| 30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
|
| 33 |
|
|
@@ -56,8 +61,9 @@ def load_image(image_input):
|
|
| 56 |
raise TypeError("Expected image input to be a string")
|
| 57 |
|
| 58 |
|
| 59 |
-
def product_identification_response(image_path=r"C:\Users\JoeJo\Downloads\
|
| 60 |
-
|
|
|
|
| 61 |
# Authenticate
|
| 62 |
genai.configure(api_key=os.environ.get("GENAI_API_KEY"))
|
| 63 |
|
|
@@ -66,13 +72,30 @@ def product_identification_response(image_path=r"C:\Users\JoeJo\Downloads\XyAaqB
|
|
| 66 |
|
| 67 |
image = load_image(image_path)
|
| 68 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
# Ask Gemini
|
| 71 |
response = model.generate_content(
|
| 72 |
-
["What is the registration number of the vehicle in this image", image]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
)
|
| 74 |
|
| 75 |
print(f"gemini-1.5-flash answer is: {response.text}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
prompt = f"""Your task is to returned structured JSON of product and condition in the following format: {{ "product": "the identity of the product", "condition": "the condition of the product"}}.
|
| 78 |
The condition of the product must be one of the following: (*) New, (*) Like New, (*) Good or (*) Poor.
|
|
@@ -107,7 +130,7 @@ def product_identification_response(image_path=r"C:\Users\JoeJo\Downloads\XyAaqB
|
|
| 107 |
demo = gr.Interface(
|
| 108 |
fn=product_identification_response,
|
| 109 |
inputs="text",
|
| 110 |
-
outputs="
|
| 111 |
title="identify registration number",
|
| 112 |
description="finds info about a product"
|
| 113 |
)
|
|
|
|
| 8 |
from io import BytesIO
|
| 9 |
import base64
|
| 10 |
import re
|
| 11 |
+
import json
|
| 12 |
|
| 13 |
# Load environment variables from .env
|
| 14 |
load_dotenv()
|
|
|
|
| 28 |
|
| 29 |
#login(token=HF_TOKEN)
|
| 30 |
|
| 31 |
+
#I'm using a virtual environment for this locally
|
| 32 |
+
#python -m venv getReg
|
| 33 |
+
#source getReg/Scripts/activate (for windows)
|
| 34 |
+
#pip install -r requirements.txt
|
| 35 |
+
#python app.py
|
| 36 |
|
| 37 |
|
| 38 |
|
|
|
|
| 61 |
raise TypeError("Expected image input to be a string")
|
| 62 |
|
| 63 |
|
| 64 |
+
def product_identification_response(image_path=r"C:\Users\JoeJo\Downloads\batty car front.jpg"):
|
| 65 |
+
#"C:\Users\JoeJo\Downloads\batty car front.jpg"
|
| 66 |
+
|
| 67 |
# Authenticate
|
| 68 |
genai.configure(api_key=os.environ.get("GENAI_API_KEY"))
|
| 69 |
|
|
|
|
| 72 |
|
| 73 |
image = load_image(image_path)
|
| 74 |
|
| 75 |
+
schema = {
|
| 76 |
+
"type": "object",
|
| 77 |
+
"properties": {
|
| 78 |
+
"registration_number": {"type": "string", "description": "registration number of the vehicle in this image"}
|
| 79 |
+
},
|
| 80 |
+
"required": ["registration_number"]
|
| 81 |
+
}
|
| 82 |
|
| 83 |
# Ask Gemini
|
| 84 |
response = model.generate_content(
|
| 85 |
+
contents=["What is the registration number of the vehicle in this image", image],
|
| 86 |
+
generation_config={
|
| 87 |
+
"response_mime_type": "application/json",
|
| 88 |
+
"response_schema": schema
|
| 89 |
+
}
|
| 90 |
)
|
| 91 |
|
| 92 |
print(f"gemini-1.5-flash answer is: {response.text}")
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
data = json.loads(response.text)
|
| 96 |
+
print(f"data after pushing response into JSON is: {data}")
|
| 97 |
+
return data
|
| 98 |
+
|
| 99 |
|
| 100 |
prompt = f"""Your task is to returned structured JSON of product and condition in the following format: {{ "product": "the identity of the product", "condition": "the condition of the product"}}.
|
| 101 |
The condition of the product must be one of the following: (*) New, (*) Like New, (*) Good or (*) Poor.
|
|
|
|
| 130 |
demo = gr.Interface(
|
| 131 |
fn=product_identification_response,
|
| 132 |
inputs="text",
|
| 133 |
+
outputs="json",
|
| 134 |
title="identify registration number",
|
| 135 |
description="finds info about a product"
|
| 136 |
)
|