Spaces:
Sleeping
Sleeping
projektkush
commited on
Upload 2 files
Browse files- app.py +87 -0
- requirements.txt +7 -0
app.py
ADDED
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from io import BytesIO
|
3 |
+
|
4 |
+
#image generation stuff
|
5 |
+
from PIL import Image
|
6 |
+
|
7 |
+
# gradio / hf / image gen stuff
|
8 |
+
import gradio as gr
|
9 |
+
from dotenv import load_dotenv
|
10 |
+
from google.cloud import aiplatform
|
11 |
+
import vertexai
|
12 |
+
from vertexai.preview.vision_models import ImageGenerationModel
|
13 |
+
from vertexai import preview
|
14 |
+
|
15 |
+
# GCP credentials stuff
|
16 |
+
|
17 |
+
import json
|
18 |
+
import pybase64
|
19 |
+
from google.oauth2 import service_account
|
20 |
+
import google.auth
|
21 |
+
|
22 |
+
import json
|
23 |
+
import os
|
24 |
+
from dotenv import load_dotenv
|
25 |
+
|
26 |
+
load_dotenv( )
|
27 |
+
|
28 |
+
# Load the decrypted JSON file or its content as environment variable
|
29 |
+
decrypted_service_account_path = "/Users/kush/Desktop/DATA_BACKUP_FINALLY/2024_PROJECTS_SHOW_ALL/VISION_PROJECTS/google_genai_hack/decrypted.json"
|
30 |
+
|
31 |
+
with open(decrypted_service_account_path, 'r') as f:
|
32 |
+
service_account_info = json.load(f)
|
33 |
+
|
34 |
+
|
35 |
+
# service_account_json = pybase64.b64decode(os.getenv("IMAGEN"))
|
36 |
+
# service_account_info = json.loads(service_account_json)
|
37 |
+
credentials = service_account.Credentials.from_service_account_info(service_account_info)
|
38 |
+
project="elemental-shine-437217-t6"
|
39 |
+
aiplatform.init(project=project, credentials=credentials)
|
40 |
+
|
41 |
+
def generate_image(prompt, model_name):
|
42 |
+
try:
|
43 |
+
print(f"Generating image with prompt: {prompt}, model: {model_name}")
|
44 |
+
model = ImageGenerationModel.from_pretrained(model_name)
|
45 |
+
|
46 |
+
response = model.generate_images(
|
47 |
+
prompt=prompt,
|
48 |
+
number_of_images=1
|
49 |
+
)
|
50 |
+
|
51 |
+
print("Received response:", response)
|
52 |
+
|
53 |
+
image_bytes = response[0]._image_bytes
|
54 |
+
image_url = Image.open(BytesIO(image_bytes))
|
55 |
+
|
56 |
+
except Exception as e:
|
57 |
+
print(f"Error: {e}")
|
58 |
+
raise gr.Error(f"An error occurred while generating the image: {str(e)}")
|
59 |
+
|
60 |
+
return image_url
|
61 |
+
|
62 |
+
with gr.Blocks() as demo:
|
63 |
+
|
64 |
+
gr.Markdown("<center> Google Vertex Imagen Generator </center>")
|
65 |
+
|
66 |
+
#instructtons
|
67 |
+
with gr.Accordion("Instructions & Tips",label="instructtons", open=False):
|
68 |
+
with gr.Row():
|
69 |
+
gr.Markdown("***Tips**; Use adjectives (size, color,mood), spectty the visual style (realistic, cartoon, 8-btt), explain the point of vi" )
|
70 |
+
|
71 |
+
|
72 |
+
#prompts
|
73 |
+
with gr.Accordion("prompt",label="prompt",open=True):
|
74 |
+
text = gr.Textbox(label="What do you want to create?", placeholder="Enter your text and then click on the \"Image Generate\" button")
|
75 |
+
|
76 |
+
model = gr.Dropdown(choices=["imagen-3.0-fast-generate-001", "imagen-3.0-generate-001"], label="Model", value="imagen-3.0-generate-001")
|
77 |
+
|
78 |
+
with gr.Row():
|
79 |
+
btn = gr.Button("Generate Images")
|
80 |
+
#output
|
81 |
+
with gr.Accordion("Image Output", label="Image Output",open=True):
|
82 |
+
output_image=gr.Image(label="Image")
|
83 |
+
|
84 |
+
btn.click(fn=generate_image, inputs=[text,model ],outputs=output_image,api_name=False)
|
85 |
+
text.submit(fn=generate_image,inputs=[text,model ],outputs=output_image,api_name="generate_image")
|
86 |
+
|
87 |
+
demo.launch(share=False)
|
requirements.txt
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
gradio==4.2.0
|
2 |
+
python-dotenv==1.0.0
|
3 |
+
pymongo
|
4 |
+
google-cloud-aiplatform>=1.38
|
5 |
+
pillow==10.2.0
|
6 |
+
google==3.0.0
|
7 |
+
pybase64==1.3.2
|