File size: 8,857 Bytes
a3f6c9a
 
 
 
 
 
 
 
 
f187bf2
 
 
 
a3f6c9a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f187bf2
 
 
d06b97c
 
 
 
f187bf2
 
a3f6c9a
 
 
 
 
 
 
 
 
 
 
 
 
f187bf2
a3f6c9a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d06b97c
a3f6c9a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f187bf2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a3f6c9a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d06b97c
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
import base64
import os
import gradio as gr
from google import genai
from google.genai import types
from google.genai.types import HarmBlockThreshold
from PIL import Image
from io import BytesIO
import tempfile
from dotenv import load_dotenv

# Load environment variables from .env file
load_dotenv()

def generate_image(input_image, partner_type):
    """
    Generate an image with a girlfriend or boyfriend added to the input image.
    
    Args:
        input_image: The uploaded image file
        partner_type: Either "Girlfriend" or "Boyfriend"
    
    Returns:
        The generated image with the partner added
    """
    # Check if input image is provided
    if input_image is None:
        return None, "Please upload an image first."
    
    # Get API key from environment variables
    api_key = os.environ.get("GEMINI_API_KEY")
    if not api_key:
        # For Hugging Face Spaces, try to get from HF_TOKEN
        api_key = os.environ.get("HF_TOKEN")
        if not api_key:
            return None, "GEMINI_API_KEY not found in environment variables."
    
    client = genai.Client(api_key=api_key)
    
    # Save the uploaded image to a temporary file
    with tempfile.NamedTemporaryFile(suffix=".jpg", delete=False) as temp_file:
        input_image_path = temp_file.name
        input_image.save(input_image_path)
    
    # Upload the temporary file to Gemini
    try:
        files = [
            client.files.upload(file=input_image_path),
        ]
        
        # Create the prompt based on the partner type selection
        prompt = f"add a {partner_type.lower()} beside the person in the picture. Do not change the environment or background."
        
        contents = [
            types.Content(
                role="user",
                parts=[
                    types.Part.from_uri(
                        file_uri=files[0].uri,
                        mime_type=files[0].mime_type,
                    ),
                    types.Part.from_text(text=prompt),
                ],
            ),
        ]
        
        generate_content_config = types.GenerateContentConfig(
            temperature=0.2,
            top_p=0.95,
            top_k=40,
            max_output_tokens=8192,
            response_modalities=[
                "image",
                "text",
            ],
            safety_settings=[
                types.SafetySetting(
                    category="HARM_CATEGORY_HARASSMENT",
                    threshold=HarmBlockThreshold.BLOCK_NONE ,
                ),
                types.SafetySetting(
                    category="HARM_CATEGORY_HATE_SPEECH",
                    threshold=HarmBlockThreshold.BLOCK_NONE,
                ),
                types.SafetySetting(
                    category="HARM_CATEGORY_SEXUALLY_EXPLICIT",
                    threshold=HarmBlockThreshold.BLOCK_NONE,
                ),
                types.SafetySetting(
                    category="HARM_CATEGORY_DANGEROUS_CONTENT",
                    threshold=HarmBlockThreshold.BLOCK_NONE,
                ),
                types.SafetySetting(
                    category="HARM_CATEGORY_CIVIC_INTEGRITY",
                    threshold=HarmBlockThreshold.BLOCK_NONE,
                ),
            ],
            response_mime_type="text/plain",
        )

        try:
            response = client.models.generate_content(
                model="models/gemini-2.0-flash-exp",
                contents=contents,
                config=generate_content_config,
            )

            output_image = None
            output_text = ""
            
            # Check if response has candidates
            if response and hasattr(response, 'candidates') and response.candidates:
                candidate = response.candidates[0]
                if hasattr(candidate, 'content') and candidate.content:
                    for part in candidate.content.parts:
                        if part.text is not None:
                            output_text += part.text + "\n"
                        elif part.inline_data is not None:
                            try:
                                # Add debug information
                                # output_text += f"Received image data of type: {type(part.inline_data.data)}\n"
                                # output_text += f"MIME type: {part.inline_data.mime_type}\n"
                                
                                # Try different approaches to handle the image data
                                if isinstance(part.inline_data.data, bytes):
                                    # If it's already bytes, use it directly
                                    image_data = part.inline_data.data
                                else:
                                    # Otherwise, try to decode it from base64
                                    image_data = base64.b64decode(part.inline_data.data)
                                
                                # Save to a temporary file first
                                with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as temp_file:
                                    temp_file.write(image_data)
                                    temp_file_path = temp_file.name
                                
                                # Open the image from the temporary file
                                output_image = Image.open(temp_file_path)
                                
                                # Clean up the temporary file
                                os.unlink(temp_file_path)
                                
                            except Exception as img_error:
                                output_text += f"Error processing image: {str(img_error)}\n"
                                output_text += f"Error type: {type(img_error).__name__}\n"
                
                # Check if there are finish details to display
                if hasattr(candidate, 'finish_details') and candidate.finish_details:
                    output_text += f"\nFinish reason: {candidate.finish_details.finish_reason}\n"
                    
                # Check if there are safety ratings to display
                if hasattr(candidate, 'safety_ratings') and candidate.safety_ratings:
                    output_text += "\nSafety ratings:\n"
                    for rating in candidate.safety_ratings:
                        output_text += f"- {rating.category}: {rating.probability}\n"
            else:
                output_text = "The model did not generate a valid response. Please try again with a different image."
                if hasattr(response, 'prompt_feedback') and response.prompt_feedback:
                    output_text += f"\nPrompt feedback: {response.prompt_feedback.block_reason}"
        
        except Exception as api_error:
            output_text = f"API Error: {str(api_error)}\n\nDetails: {type(api_error).__name__}"
            # Clean up the temporary file in case of error
            if os.path.exists(input_image_path):
                os.unlink(input_image_path)
            return None, output_text
        
        # Clean up the temporary file
        os.unlink(input_image_path)
        
        return output_image, output_text
    
    except Exception as e:
        # Clean up the temporary file in case of error
        if os.path.exists(input_image_path):
            os.unlink(input_image_path)
        
        error_details = f"Error: {str(e)}\n\nType: {type(e).__name__}"
        print(f"Exception occurred: {error_details}")
        return None, error_details

# Create the Gradio interface
def create_interface():
    with gr.Blocks(title="Single No More") as app:
        gr.Markdown("# Single No More")
        gr.Markdown("Stop annoying family members pestering you for being single!")
        gr.Markdown("Upload your photo and choose to add a girlfriend or boyfriend!")
        
        with gr.Row():
            with gr.Column():
                input_image = gr.Image(label="Your Photo", type="pil", image_mode="RGB")
                partner_type = gr.Radio(
                    ["Girlfriend", "Boyfriend"], 
                    label="Choose Partner Type", 
                    value="Girlfriend"
                )
                submit_btn = gr.Button("Generate")
            
            with gr.Column():
                output_image = gr.Image(label="Result", type="pil")
                output_text = gr.Textbox(label="Response", lines=3)
        
        submit_btn.click(
            fn=generate_image,
            inputs=[input_image, partner_type],
            outputs=[output_image, output_text]
        )
    
    return app

if __name__ == "__main__":
    app = create_interface()
    app.launch(server_name="0.0.0.0", server_port=7860)