mirror of
https://github.com/SirBlobby/Hoya26.git
synced 2026-02-03 19:24:34 -05:00
CV brand detector +query
This commit is contained in:
BIN
backend/captures/capture_20260124_175952.jpg
Normal file
BIN
backend/captures/capture_20260124_175952.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 72 KiB |
BIN
backend/captures/capture_20260124_180320.jpg
Normal file
BIN
backend/captures/capture_20260124_180320.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 66 KiB |
BIN
backend/captures/capture_20260124_180515.jpg
Normal file
BIN
backend/captures/capture_20260124_180515.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 62 KiB |
BIN
backend/captures/capture_20260124_180753.jpg
Normal file
BIN
backend/captures/capture_20260124_180753.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 65 KiB |
@@ -1,6 +1,7 @@
|
||||
import os
|
||||
from google import genai
|
||||
from src.chroma.vector_store import search_documents
|
||||
from src.rag.embeddings import get_embedding
|
||||
|
||||
GREENWASHING_ANALYSIS_PROMPT = """
|
||||
You are an expert Environmental, Social, and Governance (ESG) Analyst specialized in detecting 'Greenwashing'.
|
||||
@@ -22,15 +23,22 @@ Based on the context provided, give a final verdict:
|
||||
|
||||
def ask(prompt):
|
||||
client = genai.Client(api_key=os.environ.get("GOOGLE_API_KEY"))
|
||||
return client.models.generate_content(model="gemini-2.0-flash-exp", contents=prompt).text
|
||||
return client.models.generate_content(model="gemini-2.0-flash", contents=prompt).text
|
||||
|
||||
def analyze(query, query_embedding, num_results=5, num_alternatives=3):
|
||||
results = search_documents(query_embedding, num_results=num_results + num_alternatives + 5)
|
||||
try:
|
||||
results = search_documents(query_embedding, num_results=num_results + num_alternatives + 5)
|
||||
except Exception as e:
|
||||
print(f"Chroma error: {e}")
|
||||
results = []
|
||||
|
||||
context = "--- START OF REPORT CONTEXT ---\n"
|
||||
for res in results[:num_results]:
|
||||
context += f"RELEVANT DATA CHUNK: {res['text']}\n\n"
|
||||
context += "--- END OF REPORT CONTEXT ---\n"
|
||||
if not results:
|
||||
context = "No data found in database for this brand."
|
||||
else:
|
||||
context = "--- START OF REPORT CONTEXT ---\n"
|
||||
for res in results[:num_results]:
|
||||
context += f"RELEVANT DATA CHUNK: {res['text']}\n\n"
|
||||
context += "--- END OF REPORT CONTEXT ---\n"
|
||||
|
||||
full_prompt = f"{GREENWASHING_ANALYSIS_PROMPT}\n\n{context}\n\nUSER QUERY/COMPANY FOCUS: {query}"
|
||||
analysis_text = ask(full_prompt)
|
||||
@@ -46,3 +54,64 @@ def analyze(query, query_embedding, num_results=5, num_alternatives=3):
|
||||
break
|
||||
|
||||
return {"analysis": analysis_text, "alternatives": alternatives}
|
||||
|
||||
def ask_gemini_with_rag(query, category=None, num_results=5):
|
||||
embedding = get_embedding(query)
|
||||
result = analyze(query, embedding, num_results=num_results)
|
||||
return result["analysis"]
|
||||
|
||||
def analyze_brand(brand_name):
|
||||
print(f"\n{'='*60}")
|
||||
print(f"Analyzing brand: {brand_name}")
|
||||
print('='*60)
|
||||
|
||||
try:
|
||||
print("\n[1/3] Getting embedding for brand...")
|
||||
embedding = get_embedding(brand_name)
|
||||
|
||||
print("[2/3] Querying Chroma database...")
|
||||
result = analyze(brand_name, embedding)
|
||||
|
||||
print("[3/3] Gemini Analysis Complete!\n")
|
||||
print("-"*60)
|
||||
print("ANALYSIS:")
|
||||
print("-"*60)
|
||||
print(result["analysis"])
|
||||
|
||||
print("\n" + "-"*60)
|
||||
print("ALTERNATIVES FROM DATABASE:")
|
||||
print("-"*60)
|
||||
if result["alternatives"]:
|
||||
for i, alt in enumerate(result["alternatives"], 1):
|
||||
print(f"\n{i}. {alt['summary']}...")
|
||||
else:
|
||||
print("No alternatives found in database.")
|
||||
|
||||
print("\n" + "="*60)
|
||||
return result
|
||||
except Exception as e:
|
||||
print(f"\nError during analysis: {e}")
|
||||
return None
|
||||
|
||||
def scan_and_analyze():
|
||||
from src.cv.scanner import capture_and_analyze as cv_capture
|
||||
|
||||
print("\n" + "="*60)
|
||||
print("CV + Gemini Greenwashing Scanner")
|
||||
print("="*60)
|
||||
print("Using camera to detect brands...")
|
||||
|
||||
cv_result = cv_capture()
|
||||
|
||||
logos = cv_result.get("logos_detected", [])
|
||||
if not logos:
|
||||
print("No brands detected. Try again!")
|
||||
return None
|
||||
|
||||
brand = logos[0].get("brand", "Unknown")
|
||||
print(f"\nDetected brand: {brand}")
|
||||
|
||||
return analyze_brand(brand)
|
||||
|
||||
if __name__ == "__main__":
|
||||
scan_and_analyze()
|
||||
|
||||
4
backend/src/gemini/__main__.py
Normal file
4
backend/src/gemini/__main__.py
Normal file
@@ -0,0 +1,4 @@
|
||||
from . import scan_and_analyze
|
||||
|
||||
if __name__ == "__main__":
|
||||
scan_and_analyze()
|
||||
Reference in New Issue
Block a user