Hammad712 commited on
Commit
8ba6872
·
verified ·
1 Parent(s): a8d2020

Upload 2 files

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