Spaces:
Sleeping
Sleeping
Commit
·
6d1b873
1
Parent(s):
671d33c
removed 2 LLM calls, replaced with 1
Browse files
app.py
CHANGED
|
@@ -6,7 +6,7 @@ from PIL import Image
|
|
| 6 |
import gradio as gr
|
| 7 |
import requests
|
| 8 |
from io import BytesIO
|
| 9 |
-
|
| 10 |
|
| 11 |
# Load environment variables from .env
|
| 12 |
load_dotenv()
|
|
@@ -16,8 +16,13 @@ from groq import Groq
|
|
| 16 |
client = Groq(
|
| 17 |
api_key=os.environ.get("GROQ_API_KEY"),
|
| 18 |
)
|
|
|
|
| 19 |
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
|
| 23 |
# Fetch variables
|
|
@@ -28,8 +33,6 @@ HF_TOKEN = os.getenv("HF_TOKEN")
|
|
| 28 |
|
| 29 |
|
| 30 |
def product_identification_response(image_path=r"C:\Users\JoeJo\Downloads\XyAaqBEtYtb8YffjKZ68Gb.jpg"):
|
| 31 |
-
# Authenticate
|
| 32 |
-
genai.configure(api_key=os.environ.get("GENAI_API_KEY"))
|
| 33 |
|
| 34 |
# Load Gemini Pro Vision
|
| 35 |
model = genai.GenerativeModel('gemini-2.5-flash')
|
|
@@ -45,21 +48,42 @@ def product_identification_response(image_path=r"C:\Users\JoeJo\Downloads\XyAaqB
|
|
| 45 |
else:
|
| 46 |
image = Image.open(clean_path)
|
| 47 |
|
| 48 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
response = model.generate_content(
|
| 50 |
-
["What product is in this image, and what is the condition of the product?", image]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
-
print(f"gemini-1.5-flash answer is: {response.text}")
|
| 54 |
|
| 55 |
-
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"}}.
|
| 56 |
-
The condition of the product must be one of the following: (*) New, (*) Like New, (*) Good or (*) Poor.
|
| 57 |
-
Use the data from {response} as the source for your response
|
| 58 |
-
"""
|
| 59 |
prompt2 = 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"}}.
|
| 60 |
The condition of the product must be one of the following: (*) New or (*) Used.
|
| 61 |
Use the data from {response} as the source for your response"""
|
| 62 |
-
|
|
|
|
|
|
|
| 63 |
chat_completion = client.chat.completions.create(
|
| 64 |
messages=[
|
| 65 |
{
|
|
|
|
| 6 |
import gradio as gr
|
| 7 |
import requests
|
| 8 |
from io import BytesIO
|
| 9 |
+
import json
|
| 10 |
|
| 11 |
# Load environment variables from .env
|
| 12 |
load_dotenv()
|
|
|
|
| 16 |
client = Groq(
|
| 17 |
api_key=os.environ.get("GROQ_API_KEY"),
|
| 18 |
)
|
| 19 |
+
genai.configure(api_key=os.environ.get("GENAI_API_KEY"))
|
| 20 |
|
| 21 |
+
#I'm using a virtual environment for this locally
|
| 22 |
+
#python -m venv eccomercespace
|
| 23 |
+
#source eccomercespace/Scripts/activate (for windows)
|
| 24 |
+
#pip install -r requirements.txt
|
| 25 |
+
#python app.py
|
| 26 |
|
| 27 |
|
| 28 |
# Fetch variables
|
|
|
|
| 33 |
|
| 34 |
|
| 35 |
def product_identification_response(image_path=r"C:\Users\JoeJo\Downloads\XyAaqBEtYtb8YffjKZ68Gb.jpg"):
|
|
|
|
|
|
|
| 36 |
|
| 37 |
# Load Gemini Pro Vision
|
| 38 |
model = genai.GenerativeModel('gemini-2.5-flash')
|
|
|
|
| 48 |
else:
|
| 49 |
image = Image.open(clean_path)
|
| 50 |
|
| 51 |
+
#structured output
|
| 52 |
+
schema = {
|
| 53 |
+
"type": "object",
|
| 54 |
+
"properties": {
|
| 55 |
+
"product": {"type": "string", "description": "Name of the product in the image"},
|
| 56 |
+
"condition": {"type": "string", "enum": ["new", "like new", "good", "fair", "poor"], "description": "Condition of the product"},
|
| 57 |
+
},
|
| 58 |
+
"required": ["product", "condition"]
|
| 59 |
+
}
|
| 60 |
+
|
| 61 |
response = model.generate_content(
|
| 62 |
+
contents=["What product is in this image, and what is the condition of the product?", image],
|
| 63 |
+
generation_config={
|
| 64 |
+
"response_mime_type": "application/json",
|
| 65 |
+
"response_schema": schema
|
| 66 |
+
}
|
| 67 |
)
|
| 68 |
+
print(f"response is: {response}")
|
| 69 |
+
|
| 70 |
+
data = json.loads(response.text)
|
| 71 |
+
return data
|
| 72 |
+
|
| 73 |
+
# Ask Gemini
|
| 74 |
+
#response = model.generate_content(
|
| 75 |
+
#["What product is in this image, and what is the condition of the product?", image]
|
| 76 |
+
#)
|
| 77 |
+
|
| 78 |
+
#print(f"gemini-2.5-flash answer is: {response.text}")
|
| 79 |
|
|
|
|
| 80 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
prompt2 = 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"}}.
|
| 82 |
The condition of the product must be one of the following: (*) New or (*) Used.
|
| 83 |
Use the data from {response} as the source for your response"""
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
#this is a second LLM call, to LLama using Grok, to format identified image data - need to remove this unneccesary call
|
| 87 |
chat_completion = client.chat.completions.create(
|
| 88 |
messages=[
|
| 89 |
{
|