Spaces:
Runtime error
Runtime error
Update app/core/clients.py
Browse files- app/core/clients.py +47 -15
app/core/clients.py
CHANGED
|
@@ -1,30 +1,32 @@
|
|
| 1 |
-
import os
|
| 2 |
import logging
|
|
|
|
|
|
|
| 3 |
from langchain_groq import ChatGroq
|
| 4 |
from langchain_core.prompts import PromptTemplate
|
| 5 |
-
|
| 6 |
from .config import settings
|
| 7 |
|
| 8 |
logger = logging.getLogger(__name__)
|
|
|
|
| 9 |
|
| 10 |
-
# --- LLM (LangChain) Client ---
|
| 11 |
|
|
|
|
| 12 |
def get_llm():
|
| 13 |
-
"""Initializes and returns the ChatGroq LLM instance."""
|
| 14 |
logger.info("Initializing ChatGroq LLM...")
|
| 15 |
return ChatGroq(
|
| 16 |
model="meta-llama/llama-4-scout-17b-16e-instruct",
|
| 17 |
temperature=1,
|
| 18 |
max_tokens=1024,
|
| 19 |
-
api_key=settings.LANGCHAIN_API_KEY
|
| 20 |
)
|
| 21 |
|
|
|
|
| 22 |
def get_prompt_template():
|
| 23 |
"""Returns the prompt enhancement template."""
|
| 24 |
return PromptTemplate.from_template('''
|
| 25 |
You are a Prompt Enhancement AI Assistant. Your task is to take the
|
| 26 |
user's raw image prompt and convert it into a detailed,
|
| 27 |
-
professional prompt optimized for generating high-quality AI
|
| 28 |
Enhance the prompt by including relevant details such as:
|
| 29 |
- Camera specifications (e.g., lens type, aperture, focal length)
|
| 30 |
- Lighting setup (e.g., natural light, studio lighting, soft shadows)
|
|
@@ -37,21 +39,51 @@ Raw Prompt:
|
|
| 37 |
Enhanced Prompt:
|
| 38 |
''')
|
| 39 |
|
| 40 |
-
# --- Google GenAI
|
| 41 |
def get_genai_client():
|
| 42 |
-
"""Initializes and returns the Google GenAI client
|
|
|
|
|
|
|
| 43 |
try:
|
| 44 |
-
logger.info("Initializing Google GenAI
|
| 45 |
-
# Use the genai.Client from your original code
|
| 46 |
client = genai.Client(api_key=settings.GOOGLE_API_KEY)
|
| 47 |
-
logger.info("Google GenAI
|
| 48 |
return client
|
| 49 |
except Exception as e:
|
| 50 |
-
logger.
|
| 51 |
raise
|
| 52 |
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
llm = get_llm()
|
| 55 |
prompt_template = get_prompt_template()
|
| 56 |
-
|
| 57 |
-
|
|
|
|
|
|
|
|
|
| 1 |
import logging
|
| 2 |
+
from google import genai
|
| 3 |
+
from huggingface_hub import InferenceClient
|
| 4 |
from langchain_groq import ChatGroq
|
| 5 |
from langchain_core.prompts import PromptTemplate
|
| 6 |
+
|
| 7 |
from .config import settings
|
| 8 |
|
| 9 |
logger = logging.getLogger(__name__)
|
| 10 |
+
logging.basicConfig(level=logging.INFO)
|
| 11 |
|
|
|
|
| 12 |
|
| 13 |
+
# --- LangChain (Groq LLM) ---
|
| 14 |
def get_llm():
|
|
|
|
| 15 |
logger.info("Initializing ChatGroq LLM...")
|
| 16 |
return ChatGroq(
|
| 17 |
model="meta-llama/llama-4-scout-17b-16e-instruct",
|
| 18 |
temperature=1,
|
| 19 |
max_tokens=1024,
|
| 20 |
+
api_key=settings.LANGCHAIN_API_KEY,
|
| 21 |
)
|
| 22 |
|
| 23 |
+
|
| 24 |
def get_prompt_template():
|
| 25 |
"""Returns the prompt enhancement template."""
|
| 26 |
return PromptTemplate.from_template('''
|
| 27 |
You are a Prompt Enhancement AI Assistant. Your task is to take the
|
| 28 |
user's raw image prompt and convert it into a detailed,
|
| 29 |
+
professional prompt optimized for generating high-quality AI images.
|
| 30 |
Enhance the prompt by including relevant details such as:
|
| 31 |
- Camera specifications (e.g., lens type, aperture, focal length)
|
| 32 |
- Lighting setup (e.g., natural light, studio lighting, soft shadows)
|
|
|
|
| 39 |
Enhanced Prompt:
|
| 40 |
''')
|
| 41 |
|
| 42 |
+
# --- Google GenAI (Gemini) ---
|
| 43 |
def get_genai_client():
|
| 44 |
+
"""Initializes and returns the Google GenAI client."""
|
| 45 |
+
if not settings.GOOGLE_API_KEY:
|
| 46 |
+
raise RuntimeError("Missing GOOGLE_API_KEY in .env")
|
| 47 |
try:
|
| 48 |
+
logger.info("Initializing Google GenAI client...")
|
|
|
|
| 49 |
client = genai.Client(api_key=settings.GOOGLE_API_KEY)
|
| 50 |
+
logger.info("Google GenAI client initialized successfully.")
|
| 51 |
return client
|
| 52 |
except Exception as e:
|
| 53 |
+
logger.exception("Failed to initialize GenAI client.")
|
| 54 |
raise
|
| 55 |
|
| 56 |
+
def get_genai_client1():
|
| 57 |
+
"""Initializes and returns the Google GenAI client."""
|
| 58 |
+
if not settings.GOOGLE_API_KEY:
|
| 59 |
+
raise RuntimeError("Missing GOOGLE_API_KEY in .env")
|
| 60 |
+
try:
|
| 61 |
+
logger.info("Initializing Google GenAI client...")
|
| 62 |
+
client = genai.Client(api_key=settings.GOOGLE_API_KEY1)
|
| 63 |
+
logger.info("Google GenAI client initialized successfully.")
|
| 64 |
+
return client
|
| 65 |
+
except Exception as e:
|
| 66 |
+
logger.exception("Failed to initialize GenAI client.")
|
| 67 |
+
raise
|
| 68 |
+
|
| 69 |
+
# --- Hugging Face (FLUX) ---
|
| 70 |
+
def get_flux_client():
|
| 71 |
+
"""Initializes and returns the Hugging Face FLUX client."""
|
| 72 |
+
if not settings.HF_API_KEY:
|
| 73 |
+
raise RuntimeError("Missing HF_API_KEY in .env")
|
| 74 |
+
try:
|
| 75 |
+
logger.info("Initializing Hugging Face InferenceClient...")
|
| 76 |
+
client = InferenceClient(provider="hf-inference", api_key=settings.HF_API_KEY)
|
| 77 |
+
logger.info("Hugging Face InferenceClient initialized successfully.")
|
| 78 |
+
return client
|
| 79 |
+
except Exception as e:
|
| 80 |
+
logger.exception("Failed to initialize FLUX client.")
|
| 81 |
+
raise
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
# --- Export initialized clients ---
|
| 85 |
llm = get_llm()
|
| 86 |
prompt_template = get_prompt_template()
|
| 87 |
+
genai_client = get_genai_client()
|
| 88 |
+
flux_client = get_flux_client()
|
| 89 |
+
genai_client1 = get_genai_client1()
|