Spaces:
Sleeping
Sleeping
| import pandas as pd | |
| import ast | |
| from groq import Groq | |
| import os | |
| from .prompts import captioning_prompt | |
| from src.genai.utils.models_loader import llm | |
| from langchain_core.messages import FunctionMessage , AIMessage | |
| from .tools import retrieve_data_for_analytics | |
| import re | |
| def caption_image(image_base64,user_input): | |
| if len(image_base64)>0: | |
| print('Captioning image') | |
| client = Groq(api_key=os.environ.get('GROQ_API_KEY')) | |
| chat_completion = client.chat.completions.create( | |
| messages=[ | |
| { | |
| "role": "user", | |
| "content": [ | |
| {"type": "text", "text": captioning_prompt(user_input)}, | |
| { | |
| "type": "image_url", | |
| "image_url": { | |
| "url": f"data:image/jpg;base64,{image_base64[-1]}", | |
| }, | |
| }, | |
| ], | |
| } | |
| ], | |
| model="meta-llama/llama-4-scout-17b-16e-instruct", | |
| max_completion_tokens=50, | |
| temperature = 1 | |
| ) | |
| response=chat_completion.choices[0].message.content | |
| return response | |
| else: | |
| return '' | |
| def show_analytics(business_details): | |
| tool_response = retrieve_data_for_analytics(str(business_details)) | |
| return tool_response | |
| def extract_latest_response_block(response): | |
| latest_block = [] | |
| temp_block = [] | |
| # Reverse iterate through the messages | |
| for message in reversed(response): | |
| if isinstance(message, (FunctionMessage, AIMessage)): | |
| temp_block.insert(0, message.content) | |
| # Once we collect 3 items in correct structure, stop | |
| if len(temp_block) == 3: | |
| if "tool=" in temp_block[1] and "query_response" in temp_block[1]: | |
| latest_block = temp_block | |
| break | |
| else: | |
| temp_block = [] | |
| print('The latest block', latest_block) | |
| return latest_block | |