Spaces:
Sleeping
Sleeping
File size: 11,481 Bytes
8ba6872 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 |
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from langchain.memory import ConversationTokenBufferMemory
import os
import json
import requests
import time
from PIL import Image
from io import BytesIO
from dotenv import load_dotenv
import tempfile
from fastapi.responses import FileResponse
# Load environment variables
load_dotenv()
# Initialize FastAPI app
app = FastAPI(title="Advanced AI Mock-up FastAPI")
# Configure API Keys and global dependencies
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
url = os.getenv("IMAGE_API_URL")
API_KEY = os.getenv("IMAGE_API_KEY")
if not OPENAI_API_KEY or not API_KEY:
raise EnvironmentError("Missing API keys. Please set OPENAI_API_KEY and IMAGE_API_KEY in the environment variables.")
from openai import OpenAI
# Configure OpenAI client
client = OpenAI()
from langchain_openai import OpenAI
llm = OpenAI()
memory = ConversationTokenBufferMemory(llm=llm, max_token_limit=4000)
# API Key and Headers for image generation
headers = {
"accept": "application/json",
"x-key": API_KEY,
"Content-Type": "application/json"
}
# Pydantic model for input
class ConversationRequest(BaseModel):
question: str
# Function to manage greeting
def Greeting(question, chat_history):
prompt = f"""
You are a professional AI assistant specialized in AI-powered mock-up creation. Start with a warm greeting, ask about the user's well-being, and also ask related to AI-powered mock-up creation for jackets or other apparel. Tailor your conversation to establish a friendly and professional tone.
Chat History:
{chat_history}
"""
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{"role": "system", "content": prompt},
{"role": "user", "content": f"Question: {question}"}
]
)
return response.choices[0].message.content
def select_state(chat_history):
output_format = '''
Answer according to the following JSON format:
{
"State": "Here you will select one state based on chat history: 'greeting', 'gather_info', 'analyze_chat_history', 'generate_images'"
}'''
prompt = f"""
Based on the below chat history, decide the state for the agent. The state can be:
- 'greeting': if the chat history lacks a greeting message.
- 'gather_info': if greeting messages (like 'hi', 'hello', 'how are you') have been successfully executed.
- 'analyze_chat_history': if sufficient information has been gathered, including:
- Team name.
- Colors or style preferences.
- Details about patterns, or any unique requirements.
- 'generate_images': if image prompts are generated.
Chat History:
{chat_history}
""" + output_format
response = client.chat.completions.create(
model="gpt-4o",
response_format={"type": "json_object"},
messages=[
{"role": "system", "content": prompt},
{"role": "user", "content": "Select the next state"}
]
)
json_data = json.loads(response.choices[0].message.content)
return json_data['State']
# Function to gather information
def Gather_info(question, chat_history):
prompt = f"""
You are an information-gathering agent specialized in AI-powered mock-up creation. Your task is to politely gather the following information from the user:
- Team: Ask what team this is for.
- Team colors: Ask for the team colors or other specific colors they want to use.
- Style guide: Inquire if the user can provide a details to the team style guide.
- Additional details: Gather any additional specific information related to the Team, such as patterns, or any unique requirements.
Please ask these questions one by one in a friendly and engaging manner, and ensure you document all the provided details accurately.
Chat History:
{chat_history}
"""
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{"role": "system", "content": prompt},
{"role": "user", "content": f"Question: {question}"}
]
)
return response.choices[0].message.content
def analyze_chat_history(chat_history):
output_format = '''
Answer according to the following JSON format:
{
"Analysis": "Provide a summary analysis of the chat history, focusing on key insights derived from the gathered information.",
"NextAction": "Specify the next logical action: either continue the conversation or conclude it.",
"Prompts": [
"Prompt 1: Detailed prompt for generating the first mock-up.",
"Prompt 2: Detailed prompt for generating the second mock-up.",
"Prompt 3: Detailed prompt for generating the third mock-up.",
"Prompt 4: Detailed prompt for generating the fourth mock-up."
]
}'''
prompt = f"""
You are a highly intelligent and efficient analysis agent tasked with processing the chat history provided below. Based solely on the relevant information gathered by the information-gathering agent, your responsibilities are to:
1. Summarize the user's key points and design requirements with precision, highlighting the essential elements.
2. Generate 4 detailed and creative prompts for image mock-ups tailored to the user's specific needs.
3. In all the prompts the information about the jacket should be same so jacket in all the images are same but have different view.
Ensure that the generated prompts adhere to the following criteria:
- Visually compelling, emphasizing creativity, detail, and storytelling.
- Highly specific, incorporating the following aspects where applicable:
- Key themes, team dynamics, or user-specified concepts.
- Color schemes, textures, and style guidelines.
- Camera and Lens Settings: Recommend camera models (e.g., Canon EOS R5, Nikon Z9), lenses (e.g., 50mm f/1.8 for portraits or 85mm for close-ups), and techniques (e.g., shallow depth of field, macro for texture).
- Artistic Enhancements: Suggest details like angles (e.g., low-angle, top-down), effects (e.g., bokeh, soft focus), or scene accents (e.g., props or natural textures).
- Aspect Ratio and Style Tags: Specify dimensions (e.g., --ar 16:9 for banners or --ar 4:5 for Instagram). Include style tags like --style cinematic, --style raw, or --style editorial.
- Lighting details, including time of day, intensity, direction, and color temperature.
- Composition elements like framing, depth of field, symmetry, and rule of thirds.
- Environmental and contextual details that provide additional realism or artistic flair.
- Clearly structured to provide effective guidance for advanced image generation models.
- Prompts should Focus on the provided color combination. Do not add anything from yourself use all the context that user have provided
- Do not add Humans in the images. Only generate the images of the jackets in the white background
Chat History:
{chat_history}
""" + output_format
response = client.chat.completions.create(
model="gpt-4o",
messages=[
{"role": "system", "content": prompt},
{"role": "user", "content": "Analyze the conversation and generate prompts"}
]
)
# Extract response content
response_content = response.choices[0].message.content.strip()
# Clean and validate response content
if response_content.startswith("```") and response_content.endswith("```"):
response_content = response_content[response_content.find("\n") + 1 : -3].strip()
if not response_content:
raise ValueError("The API returned an empty response.")
try:
json_data = json.loads(response_content)
except json.JSONDecodeError as e:
print("Error parsing JSON:", e)
print("Content causing error:", response_content)
raise
return json_data
# Create a temporary directory to store generated images
temp_dir = tempfile.TemporaryDirectory()
def generate_images(prompts, url, headers):
temp_dir = tempfile.TemporaryDirectory()
image_links = []
for index, prompt in enumerate(prompts, start=1):
print(f"Generating image {index} of {len(prompts)}")
payload = {
"prompt": prompt,
"width": 1024,
"height": 1024,
"guidance_scale": 1,
"num_inference_steps": 50,
"max_sequence_length": 512,
}
response = requests.post(url, headers=headers, json=payload).json()
if "id" not in response:
print("Error in generating image:", response)
continue
request_id = response["id"]
print(f"Image generation request ID for prompt {index}: {request_id}")
while True:
time.sleep(0.5)
result = requests.get(
"https://api.bfl.ml/v1/get_result",
headers=headers,
params={"id": request_id},
).json()
if result["status"] == "Ready":
if "result" in result and "sample" in result["result"]:
image_url = result["result"]["sample"]
print(f"Generated image URL for prompt {index}: {image_url}")
image_links.append(image_url)
else:
print(f"Error: 'sample' key not found in the result for prompt {index}.")
break
else:
print(f"Image generation status for prompt {index}: {result['status']}")
return image_links
def manage_conversation(question, url, headers, memory):
chat_history = memory.load_memory_variables({})
chat_history = chat_history['history']
# Get the current state
state = select_state(chat_history)
if state == "greeting":
response = Greeting(question, chat_history)
elif state == "gather_info":
response = Gather_info(question, chat_history)
elif state == "analyze_chat_history":
response = analyze_chat_history(chat_history)
# Serialize the JSON response to a string if it's a dictionary
response = json.dumps(response, indent=4)
elif state == "generate_images":
prompts = analyze_chat_history(chat_history)['Prompts']
image_links = generate_images(prompts, url=url, headers=headers)
response = json.dumps({"message": "Images generated successfully.", "image_links": image_links}, indent=4)
else:
response = "Conversation ended."
# Save the response to memory as a string
memory.save_context({"input": question}, {"output": response})
return response
# API Endpoint
@app.post("/conversation/")
async def conversation_endpoint(request: ConversationRequest):
try:
response = manage_conversation(request.question, url, headers, memory) # Pass the required arguments
return {"response": response}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.post("/new_chat/")
async def new_chat():
"""
This endpoint resets the memory and starts a new chat session.
"""
try:
global memory
memory = ConversationTokenBufferMemory(llm=llm, max_token_limit=4000)
return {"message": "New chat session started successfully."}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.get("/")
async def root():
return {"message": "API is up and running!"}
|