Spaces:
Running
Running
File size: 4,628 Bytes
41a4a34 f39cc1d 41a4a34 e8bf349 41a4a34 e8bf349 27a7b61 f39cc1d e8bf349 f39cc1d 41a4a34 f39cc1d 41a4a34 f39cc1d 27a7b61 f39cc1d e8bf349 f39cc1d e8bf349 f39cc1d e8bf349 f39cc1d e8bf349 b4ff9c5 e8bf349 f39cc1d 41a4a34 e8bf349 41a4a34 e8bf349 41a4a34 e8bf349 27a7b61 e8bf349 153d837 f39cc1d e8bf349 153d837 f39cc1d e8bf349 f39cc1d e8bf349 f39cc1d 41a4a34 e8bf349 41a4a34 e8bf349 f39cc1d e8bf349 f39cc1d 41a4a34 f39cc1d |
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 |
import json
import os
import time
import uuid
import tempfile
from PIL import Image
import gradio as gr
import base64
import mimetypes
from google import genai
from google.genai import types
# Aapki Gemini API Key yahan daal di gayi hai.
GEMINI_API_KEY = "AIzaSyCrhWiAEQmCidtE2QZw3CTiLt7F8yv5M7A"
def save_binary_file(file_name, data):
with open(file_name, "wb") as f:
f.write(data)
def generate(text, file_name, model="gemini-2.0-flash-exp"):
# Client ko hardcoded API key se initialize karein.
client = genai.Client(api_key=GEMINI_API_KEY)
files = [
client.files.upload(file=file_name),
]
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=text),
],
),
]
generate_content_config = types.GenerateContentConfig(
temperature=1,
top_p=0.95,
top_k=40,
max_output_tokens=8192,
response_modalities=[
"image",
"text",
],
response_mime_type="text/plain",
)
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp:
temp_path = tmp.name
for chunk in client.models.generate_content_stream(
model=model,
contents=contents,
config=generate_content_config,
):
if not chunk.candidates or not chunk.candidates[0].content or not chunk.candidates[0].content.parts:
continue
inline_data = chunk.candidates[0].content.parts[0].inline_data
if inline_data:
save_binary_file(temp_path, inline_data.data)
print(
"File of mime type "
f"{inline_data.mime_type} saved to: {temp_path} and prompt input :{text}"
)
else:
print(chunk.text)
del files
return temp_path
def process_image_and_prompt(composite_pil, prompt):
# Save the composite image to a temporary file.
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp:
composite_path = tmp.name
composite_pil.save(composite_path)
file_name = composite_path
input_text = prompt
model = "gemini-2.0-flash-exp"
gemma_edited_image_path = generate(text=input_text, file_name=file_name, model=model)
print("image_path ", gemma_edited_image_path)
result_img = Image.open(gemma_edited_image_path)
if result_img.mode == "RGBA":
result_img = result_img.convert("RGB")
return [result_img]
# Build a Blocks-based interface.
with gr.Blocks() as demo:
# HTML Header ko yahan se hata diya gaya hai.
gr.Markdown("## Gen AI Image Editing")
gr.Markdown("Upload an image and enter a prompt to generate outputs in the gallery. Do not Use NFSW Images")
with gr.Row():
with gr.Column():
image_input = gr.Image(
type="pil",
label="Upload Image",
image_mode="RGBA"
)
# API Key Textbox ko yahan se hata diya gaya hai
prompt_input = gr.Textbox(
lines=2,
placeholder="Enter prompt here...",
label="Prompt"
)
submit_btn = gr.Button("Generate")
with gr.Column():
output_gallery = gr.Gallery(label="Generated Outputs")
# Define examples to be shown within the Gradio interface
examples = [
# Each example is a list corresponding to the inputs:
# [Input Image, Prompt]
["data/1.webp", 'change text to "AMEER"'],
["data/2.webp", "remove the spoon from hand only"],
["data/3.webp", 'change text to "Make it "'],
["data/1.jpg", "add joker style only on face"],
["data/1777043.jpg", "add joker style only on face"],
["data/2807615.jpg","add lipstick on lip only "],
["data/76860.jpg", "add lipstick on lip only "],
["data/2807615.jpg", "make it happy looking face only"],
]
# Set up the interaction.
submit_btn.click(
fn=process_image_and_prompt,
inputs=[image_input, prompt_input], # Inputs se API key hata di gayi hai
outputs=output_gallery,
)
gr.Examples(
examples=examples,
inputs=[image_input, prompt_input], # Inputs se API key hata di gayi hai
label="Try these examples"
)
demo.launch(share=True) |