Hammad712 commited on
Commit
ec29232
·
verified ·
1 Parent(s): 26398a9

Upload 4 files

Browse files
Files changed (4) hide show
  1. Dockerfile +25 -0
  2. README.md +4 -4
  3. main.py +177 -0
  4. requirements.txt +6 -0
Dockerfile ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Base image using Python 3.9
2
+ FROM python:3.9
3
+
4
+ # Create a new user to run the app
5
+ RUN useradd -m -u 1000 user
6
+ USER user
7
+
8
+ # Set environment variables
9
+ ENV PATH="/home/user/.local/bin:$PATH"
10
+
11
+ # Set the working directory
12
+ WORKDIR /app
13
+
14
+ # Copy the requirements and install dependencies
15
+ COPY --chown=user ./requirements.txt requirements.txt
16
+ RUN pip install --no-cache-dir --upgrade -r requirements.txt
17
+
18
+ # Copy the rest of the application
19
+ COPY --chown=user . /app
20
+
21
+ # Expose port 7860 for the application
22
+ EXPOSE 7860
23
+
24
+ # Command to run the FastAPI app using uvicorn
25
+ CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
README.md CHANGED
@@ -1,8 +1,8 @@
1
  ---
2
- title: Poster
3
- emoji: 📊
4
- colorFrom: red
5
- colorTo: indigo
6
  sdk: docker
7
  pinned: false
8
  ---
 
1
  ---
2
+ title: Fashion Designer
3
+ emoji: 📚
4
+ colorFrom: yellow
5
+ colorTo: red
6
  sdk: docker
7
  pinned: false
8
  ---
main.py ADDED
@@ -0,0 +1,177 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from fastapi import FastAPI, HTTPException
3
+ from pydantic import BaseModel
4
+ from io import BytesIO
5
+ import base64
6
+
7
+ # Import the required packages for the language model and prompts
8
+ from langchain_groq import ChatGroq
9
+ from langchain_core.prompts import PromptTemplate
10
+
11
+ # Import packages for image processing and Google GenAI client
12
+ from google import genai
13
+ from google.genai import types
14
+ from PIL import Image
15
+
16
+ # --- Global Initialization ---
17
+
18
+ # Get API keys from environment variables
19
+ LANGCHAIN_API_KEY = os.getenv("LANGCHAIN_API_KEY")
20
+ GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
21
+
22
+ if not LANGCHAIN_API_KEY or not GOOGLE_API_KEY:
23
+ raise EnvironmentError("API keys for LANGCHAIN_API_KEY and GOOGLE_API_KEY must be set as environment variables.")
24
+
25
+ def get_llm(api_key: str):
26
+ """
27
+ Returns the ChatGroq LLM instance.
28
+ """
29
+ llm = ChatGroq(
30
+ model="meta-llama/llama-4-scout-17b-16e-instruct",
31
+ temperature=1,
32
+ max_tokens=1024,
33
+ api_key=api_key
34
+ )
35
+ return llm
36
+
37
+ # Initialize the LLM instance
38
+ llm = get_llm(LANGCHAIN_API_KEY)
39
+
40
+ # Define the prompt template for enhancing raw prompts.
41
+ prompt_template = PromptTemplate.from_template('''
42
+ You are a Prompt Enhancement AI Assistant specialized in crafting marketing posters that effectively communicate a brand's message and capture audience attention. Using the provided business description and the user's raw poster concept, generate a detailed, professional prompt optimized for AI-driven poster creation.
43
+
44
+ Enhance the prompt by including:
45
+ - Visual Style: (e.g., flat design, isometric, photorealistic, illustrative)
46
+ - Color Scheme & Branding: (e.g., vibrant brand colors, pastel accents, monochrome palette)
47
+ - Typography: (e.g., bold sans-serif headline, elegant serif subtext)
48
+ - Composition & Layout: (e.g., focal point placement, use of negative space, layered elements)
49
+ - Imagery Elements: (e.g., product shots, icons, background patterns, textures)
50
+ - Lighting & Mood: (e.g., bright, dramatic shadows, soft ambient glow)
51
+ - Contextual Details: (e.g., logo placement, tagline integration, call-to-action button)
52
+
53
+ Raw Poster Concept:
54
+ {Raw_Prompt}
55
+
56
+ Enhanced Poster Prompt:
57
+
58
+ ''')
59
+
60
+ # Initialize the Google GenAI client.
61
+ client1 = genai.Client(api_key=GOOGLE_API_KEY)
62
+
63
+ # --- Request Models ---
64
+
65
+ class EnhancePromptRequest(BaseModel):
66
+ raw_prompt: str
67
+
68
+ class GenerateImageRequest(BaseModel):
69
+ # If both are provided, enhanced_prompt takes priority.
70
+ raw_prompt: str = None
71
+ enhanced_prompt: str = None
72
+
73
+ class UpdateImageRequest(BaseModel):
74
+ text_instruction: str
75
+ # Image encoded in base64
76
+ image_base64: str
77
+
78
+ # --- FastAPI Initialization ---
79
+
80
+ app = FastAPI(title="Image Generation & Update API")
81
+
82
+ # 1. Root endpoint
83
+ @app.get("/")
84
+ async def root():
85
+ return {"message": "Welcome to the Image Generation API!"}
86
+
87
+ # 2. Enhance Prompt endpoint
88
+ @app.post("/enhance-prompt")
89
+ async def enhance_prompt(request: EnhancePromptRequest):
90
+ try:
91
+ # Prepare the prompt using the template.
92
+ formatted_prompt = prompt_template.invoke({"Raw_Prompt": request.raw_prompt})
93
+ # Call the LLM to enhance the prompt.
94
+ response = llm.invoke(formatted_prompt)
95
+ # Assume the enhanced prompt is in the response.content.
96
+ enhanced_prompt = response.content
97
+ return {"raw_prompt": request.raw_prompt, "enhanced_prompt": enhanced_prompt}
98
+ except Exception as e:
99
+ raise HTTPException(status_code=500, detail=str(e))
100
+
101
+ # Helper function to generate image content using GenAI client.
102
+ def generate_image_from_prompt(image_prompt: str):
103
+ response = client1.models.generate_content(
104
+ model="gemini-2.0-flash-exp-image-generation",
105
+ contents=image_prompt,
106
+ config=types.GenerateContentConfig(
107
+ response_modalities=['Text', 'Image']
108
+ )
109
+ )
110
+ # Prepare a dict to hold results.
111
+ result = {"text": None, "image_base64": None}
112
+ # Process the response parts.
113
+ for part in response.candidates[0].content.parts:
114
+ if part.text is not None:
115
+ result["text"] = part.text
116
+ elif part.inline_data is not None:
117
+ # Open the image from bytes.
118
+ image = Image.open(BytesIO(part.inline_data.data))
119
+ # Convert image to bytes.
120
+ buffered = BytesIO()
121
+ image.save(buffered, format="PNG")
122
+ # Encode to base64.
123
+ img_str = base64.b64encode(buffered.getvalue()).decode("utf-8")
124
+ result["image_base64"] = img_str
125
+ return result
126
+
127
+ # 3. Generate Image endpoint
128
+ @app.post("/generate-poster")
129
+ async def generate_image(request: GenerateImageRequest):
130
+ try:
131
+ # Decide which prompt to use.
132
+ if request.enhanced_prompt:
133
+ image_prompt = request.enhanced_prompt
134
+ elif request.raw_prompt:
135
+ image_prompt = request.raw_prompt
136
+ else:
137
+ raise HTTPException(status_code=400, detail="Either raw_prompt or enhanced_prompt must be provided.")
138
+
139
+ result = generate_image_from_prompt(image_prompt)
140
+ return result
141
+ except Exception as e:
142
+ raise HTTPException(status_code=500, detail=str(e))
143
+
144
+ # 4. Update Image endpoint
145
+ @app.post("/update-poster")
146
+ async def update_image(request: UpdateImageRequest):
147
+ try:
148
+ # Decode the incoming base64 image.
149
+ image_bytes = base64.b64decode(request.image_base64)
150
+ image = Image.open(BytesIO(image_bytes))
151
+
152
+ # The contents for the update API include the text instruction and the current image.
153
+ response = client1.models.generate_content(
154
+ model="gemini-2.0-flash-exp-image-generation",
155
+ contents=[request.text_instruction, image],
156
+ config=types.GenerateContentConfig(
157
+ response_modalities=['Text', 'Image']
158
+ )
159
+ )
160
+ updated_result = {"text": None, "updated_image_base64": None}
161
+ for part in response.candidates[0].content.parts:
162
+ if part.text is not None:
163
+ updated_result["text"] = part.text
164
+ elif part.inline_data is not None:
165
+ updated_image = Image.open(BytesIO(part.inline_data.data))
166
+ buffered = BytesIO()
167
+ updated_image.save(buffered, format="PNG")
168
+ updated_img_str = base64.b64encode(buffered.getvalue()).decode("utf-8")
169
+ updated_result["updated_image_base64"] = updated_img_str
170
+ return updated_result
171
+ except Exception as e:
172
+ raise HTTPException(status_code=500, detail=str(e))
173
+
174
+ # --- Run the app ---
175
+ if __name__ == "__main__":
176
+ import uvicorn
177
+ uvicorn.run(app, host="0.0.0.0", port=8000)
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ langchain_groq
2
+ langchain_core
3
+ google-genai
4
+ fastapi
5
+ uvicorn
6
+ Pillow