Spaces:
Sleeping
Sleeping
Commit
·
5f0eb5c
1
Parent(s):
781cf4f
Add application file
Browse files
app.py
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from dotenv import load_dotenv
|
| 2 |
+
import os
|
| 3 |
+
import google.generativeai as genai
|
| 4 |
+
from groq import Groq
|
| 5 |
+
from PIL import Image
|
| 6 |
+
import gradio as gr
|
| 7 |
+
|
| 8 |
+
# Load environment variables from .env
|
| 9 |
+
load_dotenv()
|
| 10 |
+
|
| 11 |
+
from groq import Groq
|
| 12 |
+
|
| 13 |
+
client = Groq(
|
| 14 |
+
api_key=os.environ.get("GROQ_API_KEY"),
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
# Fetch variables
|
| 21 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
#login(token=HF_TOKEN)
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def product_identification_response(image_path=r"C:\Users\JoeJo\Downloads\XyAaqBEtYtb8YffjKZ68Gb.jpg"):
|
| 28 |
+
# Authenticate
|
| 29 |
+
genai.configure(api_key=os.environ.get("GENAI_API_KEY"))
|
| 30 |
+
|
| 31 |
+
# Load Gemini Pro Vision
|
| 32 |
+
model = genai.GenerativeModel('gemini-1.5-flash')
|
| 33 |
+
|
| 34 |
+
# Load your image
|
| 35 |
+
clean_path = image_path.strip('"')
|
| 36 |
+
image = Image.open(clean_path)
|
| 37 |
+
|
| 38 |
+
# Ask Gemini
|
| 39 |
+
response = model.generate_content(
|
| 40 |
+
["What product is in this image, and what is the condition of the product?", image]
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
print(f"gemini-1.5-flash answer is: {response.text}")
|
| 44 |
+
|
| 45 |
+
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"}}.
|
| 46 |
+
The condition of the product must be one of the following: (*) New, (*) Like New, (*) Good or (*) Poor.
|
| 47 |
+
Use the data from {response} as the source for your response
|
| 48 |
+
"""
|
| 49 |
+
|
| 50 |
+
chat_completion = client.chat.completions.create(
|
| 51 |
+
messages=[
|
| 52 |
+
{
|
| 53 |
+
"role": "system",
|
| 54 |
+
"content": prompt
|
| 55 |
+
},
|
| 56 |
+
{
|
| 57 |
+
"role": "user",
|
| 58 |
+
"content": response.text,
|
| 59 |
+
}
|
| 60 |
+
],
|
| 61 |
+
model="llama-3.3-70b-versatile",
|
| 62 |
+
response_format={"type": "json_object"},#and include word 'json' in messages/prompt
|
| 63 |
+
)
|
| 64 |
+
|
| 65 |
+
print(chat_completion.choices[0].message.content)
|
| 66 |
+
return chat_completion.choices[0].message.content
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
#product_identification_response()
|
| 71 |
+
|
| 72 |
+
demo = gr.Interface(
|
| 73 |
+
fn=product_identification_response,
|
| 74 |
+
inputs="text",
|
| 75 |
+
outputs="text",
|
| 76 |
+
title="identify product and condition",
|
| 77 |
+
description="finds info about a product"
|
| 78 |
+
)
|
| 79 |
+
|
| 80 |
+
demo.launch(share=True)
|