Spaces:
Running
Running
| from dotenv import load_dotenv | |
| import os | |
| import google.generativeai as genai | |
| from groq import Groq | |
| from PIL import Image | |
| import gradio as gr | |
| # Load environment variables from .env | |
| load_dotenv() | |
| from groq import Groq | |
| client = Groq( | |
| api_key=os.environ.get("GROQ_API_KEY"), | |
| ) | |
| # Fetch variables | |
| HF_TOKEN = os.getenv("HF_TOKEN") | |
| #login(token=HF_TOKEN) | |
| def product_identification_response(image_path=r"C:\Users\JoeJo\Downloads\XyAaqBEtYtb8YffjKZ68Gb.jpg"): | |
| # Authenticate | |
| genai.configure(api_key=os.environ.get("GENAI_API_KEY")) | |
| # Load Gemini Pro Vision | |
| model = genai.GenerativeModel('gemini-1.5-flash') | |
| # Load your image | |
| clean_path = image_path.strip('"') | |
| image = Image.open(clean_path) | |
| # Ask Gemini | |
| response = model.generate_content( | |
| ["What product is in this image, and what is the condition of the product?", image] | |
| ) | |
| print(f"gemini-1.5-flash answer is: {response.text}") | |
| 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"}}. | |
| The condition of the product must be one of the following: (*) New, (*) Like New, (*) Good or (*) Poor. | |
| Use the data from {response} as the source for your response | |
| """ | |
| chat_completion = client.chat.completions.create( | |
| messages=[ | |
| { | |
| "role": "system", | |
| "content": prompt | |
| }, | |
| { | |
| "role": "user", | |
| "content": response.text, | |
| } | |
| ], | |
| model="llama-3.3-70b-versatile", | |
| response_format={"type": "json_object"},#and include word 'json' in messages/prompt | |
| ) | |
| print(chat_completion.choices[0].message.content) | |
| return chat_completion.choices[0].message.content | |
| #product_identification_response() | |
| demo = gr.Interface( | |
| fn=product_identification_response, | |
| inputs="text", | |
| outputs="text", | |
| title="identify product and condition", | |
| description="finds info about a product" | |
| ) | |
| demo.launch(share=True) |