Spaces:
Running
Running
Rename app.py to synthscope.py
Browse files- app.py +0 -58
- synthscope.py +81 -0
app.py
DELETED
|
@@ -1,58 +0,0 @@
|
|
| 1 |
-
from google import genai
|
| 2 |
-
from google.genai import types
|
| 3 |
-
from PIL import Image
|
| 4 |
-
from io import BytesIO
|
| 5 |
-
import gradio as gr
|
| 6 |
-
import base64
|
| 7 |
-
import os
|
| 8 |
-
|
| 9 |
-
api_key = os.getenv("GEMINI_API_KEY")
|
| 10 |
-
|
| 11 |
-
def generate_image(prompt):
|
| 12 |
-
|
| 13 |
-
"""
|
| 14 |
-
Transforms text into an image.
|
| 15 |
-
|
| 16 |
-
Args:
|
| 17 |
-
prompt: A natural language instruction that is used to generate an image.
|
| 18 |
-
|
| 19 |
-
Returns:
|
| 20 |
-
An image.
|
| 21 |
-
"""
|
| 22 |
-
|
| 23 |
-
client = genai.Client(api_key=api_key)
|
| 24 |
-
|
| 25 |
-
prompt = tuple(prompt)
|
| 26 |
-
|
| 27 |
-
response = client.models.generate_content(
|
| 28 |
-
model="gemini-2.0-flash-preview-image-generation",
|
| 29 |
-
contents=prompt,
|
| 30 |
-
config=types
|
| 31 |
-
.GenerateContentConfig(
|
| 32 |
-
response_modalities=['TEXT', 'IMAGE']
|
| 33 |
-
)
|
| 34 |
-
)
|
| 35 |
-
|
| 36 |
-
for part in response.candidates[0].content.parts:
|
| 37 |
-
if part.text is not None:
|
| 38 |
-
pass
|
| 39 |
-
elif part.inline_data is not None:
|
| 40 |
-
image = Image.open(BytesIO((part.inline_data.data)))
|
| 41 |
-
# image.save('gemini-native-image.png')
|
| 42 |
-
# display(image)
|
| 43 |
-
return image
|
| 44 |
-
|
| 45 |
-
# build gradio interface
|
| 46 |
-
app = gr.Interface(fn = generate_image,
|
| 47 |
-
inputs = gr.Text(label="Prompt",placeholder="Type your prompt here. . ."),
|
| 48 |
-
outputs = gr.Image(label="Generated Image"),
|
| 49 |
-
title="Gemini Image Generator",
|
| 50 |
-
examples=["A 3D rendering of a little black girl wearing a colorful dress and smiling broadly at the camera",
|
| 51 |
-
"Disney and Pixar-style playful bunny skipping about in a garden full of carrots and lettuce",
|
| 52 |
-
"A strong black female superhero with braided hair flying in the bright blue sky",
|
| 53 |
-
"A jazz musician playing his saxophone and surrounded by colorful musical notes."])
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
# launch application
|
| 57 |
-
if __name__ == "__main__":
|
| 58 |
-
app.launch(mcp_server = True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
synthscope.py
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from google import genai
|
| 2 |
+
from google.genai.types import Tool, GenerateContentConfig, GoogleSearch
|
| 3 |
+
from google.colab import userdata
|
| 4 |
+
from google.genai import types
|
| 5 |
+
from PIL import Image
|
| 6 |
+
from io import BytesIO
|
| 7 |
+
|
| 8 |
+
def GoogleSearchImageGen(prompt, image_style):
|
| 9 |
+
# Define style-specific prompts
|
| 10 |
+
style_prompts = {
|
| 11 |
+
"Comic": f"{prompt}. Convert the search result into a well-crafted text-to-image prompt that generates a comic book-style image.",
|
| 12 |
+
"Cartoon": f"{prompt}. Convert the search result into a well-crafted text-to-image prompt that generates a cartoon-style image.",
|
| 13 |
+
"3D": f"{prompt}. Convert the search result into a well-crafted text-to-image prompt that generates a Pixar-style 3D image.",
|
| 14 |
+
"Anime": f"{prompt}. Convert the search result into a well-crafted text-to-image prompt that generates an Anime-style image.",
|
| 15 |
+
"Ghibli": f"{prompt}. Convert the search result into a well-crafted text-to-image prompt that generates a Ghibli-style image.",
|
| 16 |
+
"Victorian": f"{prompt}. Convert the search result into a well-crafted text-to-image prompt that generates a Victorian-era image."
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
# Check if the image_style is supported
|
| 20 |
+
if image_style in style_prompts:
|
| 21 |
+
image_gen_prompt = style_prompts[image_style]
|
| 22 |
+
|
| 23 |
+
# Initialize variables to avoid UnboundLocalError
|
| 24 |
+
search_result = ""
|
| 25 |
+
image = None
|
| 26 |
+
|
| 27 |
+
try:
|
| 28 |
+
# Get search result to be displayed to the user
|
| 29 |
+
response = client.models.generate_content(
|
| 30 |
+
model=model_id,
|
| 31 |
+
contents=prompt,
|
| 32 |
+
config=GenerateContentConfig(
|
| 33 |
+
tools=[google_search_tool],
|
| 34 |
+
response_modalities=["TEXT"],
|
| 35 |
+
)
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
# Extract search result
|
| 39 |
+
for each in response.candidates[0].content.parts:
|
| 40 |
+
if each.text:
|
| 41 |
+
search_result += each.text
|
| 42 |
+
|
| 43 |
+
# Generate image prompt from search result
|
| 44 |
+
output = client.models.generate_content(
|
| 45 |
+
model=model_id,
|
| 46 |
+
contents=image_gen_prompt,
|
| 47 |
+
config=GenerateContentConfig(
|
| 48 |
+
tools=[google_search_tool],
|
| 49 |
+
response_modalities=["TEXT"],
|
| 50 |
+
)
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
prompt_image = ""
|
| 54 |
+
for single in output.candidates[0].content.parts:
|
| 55 |
+
if single.text:
|
| 56 |
+
prompt_image += single.text
|
| 57 |
+
|
| 58 |
+
# Generate image
|
| 59 |
+
if prompt_image: # Only generate image if we have a prompt
|
| 60 |
+
response = client.models.generate_content(
|
| 61 |
+
model="gemini-2.0-flash-preview-image-generation",
|
| 62 |
+
contents=prompt_image,
|
| 63 |
+
config=types.GenerateContentConfig(
|
| 64 |
+
response_modalities=['TEXT', 'IMAGE']
|
| 65 |
+
)
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
# Extract image from response
|
| 69 |
+
for part in response.candidates[0].content.parts:
|
| 70 |
+
if part.text is not None:
|
| 71 |
+
pass # Handle text if needed
|
| 72 |
+
elif part.inline_data is not None:
|
| 73 |
+
image = Image.open(BytesIO(part.inline_data.data))
|
| 74 |
+
break # Exit loop once we find the image
|
| 75 |
+
|
| 76 |
+
except Exception as e:
|
| 77 |
+
print(f"Error occurred: {e}")
|
| 78 |
+
# Return default values in case of error
|
| 79 |
+
return search_result or "No search result available", None
|
| 80 |
+
|
| 81 |
+
return search_result, image
|