Virtual-try-on / app /core /clients.py
Hammad712's picture
Update app/core/clients.py
064e50b verified
import logging
from google import genai
from huggingface_hub import InferenceClient
from langchain_groq import ChatGroq
from langchain_core.prompts import PromptTemplate
from .config import settings
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)
# --- LangChain (Groq LLM) ---
def get_llm():
logger.info("Initializing ChatGroq LLM...")
return ChatGroq(
model="meta-llama/llama-4-scout-17b-16e-instruct",
temperature=1,
max_tokens=1024,
api_key=settings.LANGCHAIN_API_KEY,
)
def get_prompt_template():
"""Returns the prompt enhancement template."""
return PromptTemplate.from_template('''
You are a Prompt Enhancement AI Assistant. Your task is to take the
user's raw image prompt and convert it into a detailed,
professional prompt optimized for generating high-quality AI images.
Enhance the prompt by including relevant details such as:
- Do not add anything except the final prompt. for example: do not write starting like here is the enhanced prompt or something like this.
- Camera specifications (e.g., lens type, aperture, focal length)
- Lighting setup (e.g., natural light, studio lighting, soft shadows)
- Camera angle (e.g., top-down, macro, isometric, side view)
- Background style (e.g., plain white, minimalistic, outdoor, studio backdrop)
- Scene composition (e.g., centered product, depth of field, reflections)
Focus only on *Poster photography* — do not include humans or models.
Raw Prompt:
{Raw_Prompt}
Enhanced Prompt:
''')
# --- Google GenAI (Gemini) ---
def get_genai_client():
"""Initializes and returns the Google GenAI client."""
if not settings.GOOGLE_API_KEY:
raise RuntimeError("Missing GOOGLE_API_KEY in .env")
try:
logger.info("Initializing Google GenAI client...")
client = genai.Client(api_key=settings.GOOGLE_API_KEY)
logger.info("Google GenAI client initialized successfully.")
return client
except Exception as e:
logger.exception("Failed to initialize GenAI client.")
raise
def get_genai_client1():
"""Initializes and returns the Google GenAI client."""
if not settings.GOOGLE_API_KEY:
raise RuntimeError("Missing GOOGLE_API_KEY in .env")
try:
logger.info("Initializing Google GenAI client...")
client = genai.Client(api_key=settings.GOOGLE_API_KEY1)
logger.info("Google GenAI client initialized successfully.")
return client
except Exception as e:
logger.exception("Failed to initialize GenAI client.")
raise
# --- Hugging Face (FLUX) ---
def get_flux_client():
"""Initializes and returns the Hugging Face FLUX client."""
if not settings.HF_API_KEY:
raise RuntimeError("Missing HF_API_KEY in .env")
try:
logger.info("Initializing Hugging Face InferenceClient...")
client = InferenceClient(provider="hf-inference", api_key=settings.HF_API_KEY)
logger.info("Hugging Face InferenceClient initialized successfully.")
return client
except Exception as e:
logger.exception("Failed to initialize FLUX client.")
raise
# --- Export initialized clients ---
llm = get_llm()
prompt_template = get_prompt_template()
genai_client = get_genai_client()
flux_client = get_flux_client()
genai_client1 = get_genai_client1()