Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,212 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import base64
|
2 |
+
from openai import OpenAI
|
3 |
+
from dotenv import load_dotenv
|
4 |
+
import fal_client
|
5 |
+
import os
|
6 |
+
from datetime import datetime
|
7 |
+
import requests
|
8 |
+
import gradio as gr
|
9 |
+
|
10 |
+
load_dotenv()
|
11 |
+
|
12 |
+
client = OpenAI()
|
13 |
+
|
14 |
+
# Function to encode the image
|
15 |
+
def encode_image(image_path):
|
16 |
+
with open(image_path, "rb") as image_file:
|
17 |
+
return base64.b64encode(image_file.read()).decode("utf-8")
|
18 |
+
|
19 |
+
def generate_description(image):
|
20 |
+
"""Generate description for uploaded image using OpenAI vision"""
|
21 |
+
if image is None:
|
22 |
+
return "Please upload an image first."
|
23 |
+
|
24 |
+
try:
|
25 |
+
# Convert PIL image to base64
|
26 |
+
import io
|
27 |
+
buffered = io.BytesIO()
|
28 |
+
image.save(buffered, format="JPEG")
|
29 |
+
base64_image = base64.b64encode(buffered.getvalue()).decode("utf-8")
|
30 |
+
|
31 |
+
response = client.responses.create(
|
32 |
+
model="gpt-4.1",
|
33 |
+
input=[
|
34 |
+
{
|
35 |
+
"role": "user",
|
36 |
+
"content": [
|
37 |
+
{ "type": "input_text", "text": "Describe the image in rich, natural detail for txt2img model. Focus on the scene, the person, their casual clothes, accessories, makeup, the everyday environment, and what they're doing. Capture the emotions, expressions, mood, and candid vibe as if it were a real Instagram Reel, TikTok, or Pinterest-style snapshot. Mention the camera angle, framing, depth of field, shadows, colors, and textures in a way that feels spontaneous and unpolished, with natural light and subtle motion blur. Convey the overall feeling, atmosphere, and sense of being in that moment. Describe the angle of the camera and pose of the model precisely. The result will be used to generate a similar image with a txt2img model, emphasizing realism, casualness, and lived-in authenticity rather than studio-perfect or commercialized style. Highlight the photo taken with iPhone front camera in-the-moment." },
|
38 |
+
{
|
39 |
+
"type": "input_image",
|
40 |
+
"image_url": f"data:image/jpeg;base64,{base64_image}",
|
41 |
+
},
|
42 |
+
],
|
43 |
+
}
|
44 |
+
],
|
45 |
+
)
|
46 |
+
|
47 |
+
description = response.output_text + "\n Photo taken with iPhone front camera in-the-moment."
|
48 |
+
return description
|
49 |
+
|
50 |
+
except Exception as e:
|
51 |
+
return f"Error generating description: {str(e)}"
|
52 |
+
|
53 |
+
|
54 |
+
model = "fal-ai/gemini-25-flash-image" # , "fal-ai/nano-banana",
|
55 |
+
|
56 |
+
def on_queue_update(update):
|
57 |
+
if isinstance(update, fal_client.InProgress):
|
58 |
+
for log in update.logs:
|
59 |
+
print(log["message"])
|
60 |
+
|
61 |
+
def generate_avatar(prompt, selected_model):
|
62 |
+
"""Generate avatar image with given prompt and selected model"""
|
63 |
+
if not prompt or prompt.strip() == "":
|
64 |
+
return None, "Please provide a prompt for avatar generation."
|
65 |
+
|
66 |
+
try:
|
67 |
+
result = fal_client.subscribe(
|
68 |
+
selected_model,
|
69 |
+
arguments={
|
70 |
+
"prompt": prompt,
|
71 |
+
"num_images": 1,
|
72 |
+
"output_format": "png"
|
73 |
+
},
|
74 |
+
with_logs=True,
|
75 |
+
on_queue_update=on_queue_update,
|
76 |
+
)
|
77 |
+
|
78 |
+
# Return the first generated image
|
79 |
+
if 'images' in result and len(result['images']) > 0:
|
80 |
+
image_url = result['images'][0]['url']
|
81 |
+
|
82 |
+
# Download the image
|
83 |
+
response = requests.get(image_url)
|
84 |
+
if response.status_code == 200:
|
85 |
+
# Convert to PIL Image for Gradio display
|
86 |
+
from PIL import Image
|
87 |
+
import io
|
88 |
+
image = Image.open(io.BytesIO(response.content))
|
89 |
+
return image, "Avatar generated successfully!"
|
90 |
+
else:
|
91 |
+
return None, f"Failed to download image: {response.status_code}"
|
92 |
+
else:
|
93 |
+
return None, "No images found in result"
|
94 |
+
|
95 |
+
except Exception as e:
|
96 |
+
return None, f"Error generating avatar: {str(e)}"
|
97 |
+
|
98 |
+
# Gradio Interface
|
99 |
+
def process_image_and_generate_avatar(image):
|
100 |
+
"""Process uploaded image and generate avatar"""
|
101 |
+
if image is None:
|
102 |
+
return None, "Please upload an image first.", "Upload an image to get started!"
|
103 |
+
|
104 |
+
# Generate description
|
105 |
+
description = generate_description(image)
|
106 |
+
|
107 |
+
# Generate avatar
|
108 |
+
avatar_image, message = generate_avatar(description)
|
109 |
+
|
110 |
+
return avatar_image, description, message
|
111 |
+
|
112 |
+
# Available models
|
113 |
+
available_models = [
|
114 |
+
"fal-ai/gemini-25-flash-image",
|
115 |
+
"fal-ai/nano-banana",
|
116 |
+
"fal-ai/flux-pro",
|
117 |
+
"fal-ai/stable-diffusion-v35-large",
|
118 |
+
"fal-ai/qwen-image",
|
119 |
+
"fal-ai/imagen4-preview",
|
120 |
+
"fal-ai/imagen4-preview-fast",
|
121 |
+
"fal-ai/imagen4-preview-ultra"
|
122 |
+
]
|
123 |
+
|
124 |
+
# Create Gradio interface
|
125 |
+
with gr.Blocks(title="Avatar Clone Generator") as demo:
|
126 |
+
gr.Markdown("# Avatar Clone Generator")
|
127 |
+
gr.Markdown("Upload a photo to generate a description and create an avatar!")
|
128 |
+
|
129 |
+
with gr.Row():
|
130 |
+
with gr.Column():
|
131 |
+
# Image upload
|
132 |
+
input_image = gr.Image(
|
133 |
+
label="Upload Photo",
|
134 |
+
type="pil",
|
135 |
+
height=300
|
136 |
+
)
|
137 |
+
|
138 |
+
# Model selection
|
139 |
+
model_dropdown = gr.Dropdown(
|
140 |
+
choices=available_models,
|
141 |
+
value="fal-ai/gemini-25-flash-image",
|
142 |
+
label="Select Vision Model",
|
143 |
+
info="Choose the AI model for generating your avatar"
|
144 |
+
)
|
145 |
+
|
146 |
+
# Generate description button
|
147 |
+
generate_desc_btn = gr.Button("Generate Description", variant="primary")
|
148 |
+
|
149 |
+
# Description text area (editable)
|
150 |
+
description_text = gr.Textbox(
|
151 |
+
label="Generated Description (You can edit this)",
|
152 |
+
lines=8,
|
153 |
+
placeholder="Description will appear here after uploading an image..."
|
154 |
+
)
|
155 |
+
|
156 |
+
# Generate avatar button
|
157 |
+
generate_avatar_btn = gr.Button("Generate Avatar", variant="secondary")
|
158 |
+
|
159 |
+
# Status message
|
160 |
+
status_text = gr.Textbox(
|
161 |
+
label="Status",
|
162 |
+
lines=2,
|
163 |
+
interactive=False
|
164 |
+
)
|
165 |
+
|
166 |
+
with gr.Column():
|
167 |
+
# Generated avatar display
|
168 |
+
output_image = gr.Image(
|
169 |
+
label="Generated Avatar",
|
170 |
+
height=400
|
171 |
+
)
|
172 |
+
|
173 |
+
# Event handlers
|
174 |
+
def on_image_upload(image):
|
175 |
+
if image is not None:
|
176 |
+
description = generate_description(image)
|
177 |
+
return description, "Image uploaded! Click 'Generate Description' to proceed."
|
178 |
+
return "", "Please upload an image."
|
179 |
+
|
180 |
+
def on_generate_description(image):
|
181 |
+
if image is None:
|
182 |
+
return "", "Please upload an image first."
|
183 |
+
description = generate_description(image)
|
184 |
+
return description, "Description generated! You can edit it if needed."
|
185 |
+
|
186 |
+
def on_generate_avatar(description, selected_model):
|
187 |
+
if not description or description.strip() == "":
|
188 |
+
return None, "Please generate a description first."
|
189 |
+
avatar_image, message = generate_avatar(description, selected_model)
|
190 |
+
return avatar_image, message
|
191 |
+
|
192 |
+
# Connect events
|
193 |
+
input_image.change(
|
194 |
+
fn=on_image_upload,
|
195 |
+
inputs=[input_image],
|
196 |
+
outputs=[description_text, status_text]
|
197 |
+
)
|
198 |
+
|
199 |
+
generate_desc_btn.click(
|
200 |
+
fn=on_generate_description,
|
201 |
+
inputs=[input_image],
|
202 |
+
outputs=[description_text, status_text]
|
203 |
+
)
|
204 |
+
|
205 |
+
generate_avatar_btn.click(
|
206 |
+
fn=on_generate_avatar,
|
207 |
+
inputs=[description_text, model_dropdown],
|
208 |
+
outputs=[output_image, status_text]
|
209 |
+
)
|
210 |
+
|
211 |
+
if __name__ == "__main__":
|
212 |
+
demo.launch(share=True)
|