Spaces:
Running
Running
Add app, requirements, gitingore
Browse files- .gitignore +50 -0
- app.py +111 -0
- requirements.txt +3 -0
.gitignore
ADDED
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Environment variables
|
2 |
+
.env
|
3 |
+
.venv
|
4 |
+
env/
|
5 |
+
venv/
|
6 |
+
ENV/
|
7 |
+
env.bak/
|
8 |
+
venv.bak/
|
9 |
+
|
10 |
+
# Python
|
11 |
+
__pycache__/
|
12 |
+
*.py[cod]
|
13 |
+
*$py.class
|
14 |
+
*.so
|
15 |
+
.Python
|
16 |
+
build/
|
17 |
+
develop-eggs/
|
18 |
+
dist/
|
19 |
+
downloads/
|
20 |
+
eggs/
|
21 |
+
.eggs/
|
22 |
+
lib/
|
23 |
+
lib64/
|
24 |
+
parts/
|
25 |
+
sdist/
|
26 |
+
var/
|
27 |
+
wheels/
|
28 |
+
*.egg-info/
|
29 |
+
.installed.cfg
|
30 |
+
*.egg
|
31 |
+
|
32 |
+
# Generated images
|
33 |
+
generated_couple_photo.jpg
|
34 |
+
*.jpg
|
35 |
+
*.jpeg
|
36 |
+
*.png
|
37 |
+
|
38 |
+
# IDE files
|
39 |
+
.idea/
|
40 |
+
.vscode/
|
41 |
+
*.swp
|
42 |
+
*.swo
|
43 |
+
|
44 |
+
# OS specific files
|
45 |
+
.DS_Store
|
46 |
+
Thumbs.db
|
47 |
+
|
48 |
+
# API keys and sensitive data
|
49 |
+
*.pem
|
50 |
+
*.key
|
app.py
ADDED
@@ -0,0 +1,111 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import gradio as gr
|
3 |
+
from dotenv import load_dotenv
|
4 |
+
from google import genai
|
5 |
+
from google.genai import types
|
6 |
+
|
7 |
+
load_dotenv()
|
8 |
+
|
9 |
+
def save_binary_file(file_name, data):
|
10 |
+
with open(file_name, "wb") as f:
|
11 |
+
f.write(data)
|
12 |
+
return file_name
|
13 |
+
|
14 |
+
def generate_couple_photo(input_image):
|
15 |
+
# Check if API key is set
|
16 |
+
api_key = os.getenv("GEMINI_API_KEY")
|
17 |
+
if not api_key:
|
18 |
+
return None, "Error: GEMINI_API_KEY environment variable not set"
|
19 |
+
|
20 |
+
client = genai.Client(api_key=api_key)
|
21 |
+
|
22 |
+
try:
|
23 |
+
# Upload the input image
|
24 |
+
uploaded_file = client.files.upload(file=input_image)
|
25 |
+
|
26 |
+
# Default prompt if none provided
|
27 |
+
|
28 |
+
prompt_text = os.getenv("PROMPT")
|
29 |
+
|
30 |
+
model = "gemini-2.0-flash-exp-image-generation"
|
31 |
+
contents = [
|
32 |
+
types.Content(
|
33 |
+
role="user",
|
34 |
+
parts=[
|
35 |
+
types.Part.from_uri(
|
36 |
+
file_uri=uploaded_file.uri,
|
37 |
+
mime_type=uploaded_file.mime_type,
|
38 |
+
),
|
39 |
+
types.Part.from_text(text=prompt_text),
|
40 |
+
],
|
41 |
+
),
|
42 |
+
]
|
43 |
+
|
44 |
+
generate_content_config = types.GenerateContentConfig(
|
45 |
+
temperature=1,
|
46 |
+
top_p=0.95,
|
47 |
+
top_k=40,
|
48 |
+
max_output_tokens=8192,
|
49 |
+
response_modalities=["image", "text"],
|
50 |
+
safety_settings=[
|
51 |
+
types.SafetySetting(
|
52 |
+
category="HARM_CATEGORY_CIVIC_INTEGRITY",
|
53 |
+
threshold="OFF",
|
54 |
+
),
|
55 |
+
],
|
56 |
+
response_mime_type="text/plain",
|
57 |
+
)
|
58 |
+
|
59 |
+
# Generate content
|
60 |
+
response = client.models.generate_content(
|
61 |
+
model=model,
|
62 |
+
contents=contents,
|
63 |
+
config=generate_content_config,
|
64 |
+
)
|
65 |
+
|
66 |
+
# Process response
|
67 |
+
output_text = ""
|
68 |
+
output_image_path = None
|
69 |
+
|
70 |
+
for part in response.candidates[0].content.parts:
|
71 |
+
if part.text is not None:
|
72 |
+
output_text += part.text
|
73 |
+
elif part.inline_data is not None:
|
74 |
+
# Save the generated image
|
75 |
+
output_image_path = save_binary_file(
|
76 |
+
"generated_couple_photo.jpg",
|
77 |
+
part.inline_data.data
|
78 |
+
)
|
79 |
+
|
80 |
+
return output_image_path, output_text
|
81 |
+
|
82 |
+
except Exception as e:
|
83 |
+
return None, f"Error: {str(e)}"
|
84 |
+
|
85 |
+
# Create Gradio interface
|
86 |
+
def create_interface():
|
87 |
+
with gr.Blocks(title="Couple Photo Generator") as demo:
|
88 |
+
gr.Markdown("# Couple Photo Generator")
|
89 |
+
gr.Markdown("Upload an image and get a generated couple photo version of it.")
|
90 |
+
|
91 |
+
with gr.Row():
|
92 |
+
with gr.Column():
|
93 |
+
input_image = gr.Image(type="filepath", label="Upload Image")
|
94 |
+
submit_btn = gr.Button("Generate Couple Photo")
|
95 |
+
|
96 |
+
with gr.Column():
|
97 |
+
output_image = gr.Image(label="Generated Couple Photo")
|
98 |
+
output_text = gr.Textbox(label="Generation Notes")
|
99 |
+
|
100 |
+
submit_btn.click(
|
101 |
+
fn=generate_couple_photo,
|
102 |
+
inputs=[input_image],
|
103 |
+
outputs=[output_image, output_text]
|
104 |
+
)
|
105 |
+
|
106 |
+
return demo
|
107 |
+
|
108 |
+
if __name__ == "__main__":
|
109 |
+
# Launch the app
|
110 |
+
demo = create_interface()
|
111 |
+
demo.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
gradio
|
2 |
+
python-dotenv
|
3 |
+
google-genai
|