Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -28,6 +28,7 @@ def save_binary_file(file_name, data):
|
|
28 |
def generate(text, api_key, model="gemini-2.0-flash-exp-image-generation"):
|
29 |
logger.debug(f"Starting generate function with text: '{text}', model: '{model}'")
|
30 |
|
|
|
31 |
try:
|
32 |
# Initialize client
|
33 |
effective_api_key = api_key.strip() if api_key and api_key.strip() != "" else os.environ.get("GEMINI_API_KEY")
|
@@ -37,9 +38,21 @@ def generate(text, api_key, model="gemini-2.0-flash-exp-image-generation"):
|
|
37 |
logger.error("No API key provided or found in environment variable.")
|
38 |
raise ValueError("API key is required.")
|
39 |
|
40 |
-
|
41 |
-
|
42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
|
44 |
contents = [
|
45 |
types.Content(
|
@@ -86,18 +99,20 @@ def generate(text, api_key, model="gemini-2.0-flash-exp-image-generation"):
|
|
86 |
logger.info(f"File of mime type {inline_data.mime_type} saved to: {temp_path} and prompt input :{text}")
|
87 |
else:
|
88 |
logger.info(f"Received text: {chunk.text}")
|
89 |
-
print(chunk.text)
|
90 |
|
91 |
# Log the raw chunk for deeper inspection
|
92 |
logger.debug(f"Raw chunk: {chunk}")
|
93 |
|
94 |
-
|
|
|
|
|
95 |
logger.debug("Uploaded files deleted.")
|
96 |
return temp_path
|
97 |
|
98 |
except Exception as e:
|
99 |
-
logger.exception("An error occurred during generation:")
|
100 |
-
return None
|
101 |
|
102 |
|
103 |
def generate_image_from_prompt(prompt, gemini_api_key):
|
@@ -105,11 +120,11 @@ def generate_image_from_prompt(prompt, gemini_api_key):
|
|
105 |
try:
|
106 |
|
107 |
input_text = prompt
|
108 |
-
model = "gemini-2.0-flash-exp-image-generation"
|
109 |
|
110 |
gemma_generated_image_path = generate(text=input_text, api_key=gemini_api_key, model=model)
|
111 |
|
112 |
-
if gemma_generated_image_path:
|
113 |
logger.debug(f"Image generated at path: {gemma_generated_image_path}")
|
114 |
result_img = Image.open(gemma_generated_image_path)
|
115 |
if result_img.mode == "RGBA":
|
@@ -117,11 +132,11 @@ def generate_image_from_prompt(prompt, gemini_api_key):
|
|
117 |
return [result_img]
|
118 |
else:
|
119 |
logger.error("generate function returned None.")
|
120 |
-
return []
|
121 |
|
122 |
except Exception as e:
|
123 |
logger.exception("Error occurred in generate_image_from_prompt")
|
124 |
-
return []
|
125 |
|
126 |
# --- Gradio Interface ---
|
127 |
with gr.Blocks() as demo:
|
@@ -165,8 +180,7 @@ with gr.Blocks() as demo:
|
|
165 |
)
|
166 |
|
167 |
try:
|
168 |
-
demo.launch(
|
169 |
except Exception as e:
|
170 |
logger.error(f"Failed to launch Gradio app: {e}")
|
171 |
print(f"Failed to launch Gradio app: {e}")
|
172 |
-
|
|
|
28 |
def generate(text, api_key, model="gemini-2.0-flash-exp-image-generation"):
|
29 |
logger.debug(f"Starting generate function with text: '{text}', model: '{model}'")
|
30 |
|
31 |
+
files = None # Initialize 'files' to None
|
32 |
try:
|
33 |
# Initialize client
|
34 |
effective_api_key = api_key.strip() if api_key and api_key.strip() != "" else os.environ.get("GEMINI_API_KEY")
|
|
|
38 |
logger.error("No API key provided or found in environment variable.")
|
39 |
raise ValueError("API key is required.")
|
40 |
|
41 |
+
try:
|
42 |
+
client = genai.Client(api_key=effective_api_key)
|
43 |
+
logger.debug("Gemini client initialized.")
|
44 |
+
except Exception as e:
|
45 |
+
logger.error(f"Error initializing Gemini client: {e}")
|
46 |
+
return None # Return None if client initialization fails
|
47 |
+
|
48 |
+
try:
|
49 |
+
files = [
|
50 |
+
client.files.upload(file=text), # Changed file=text
|
51 |
+
]
|
52 |
+
logger.debug(f"File uploaded. URI: {files[0].uri}, MIME Type: {files[0].mime_type}")
|
53 |
+
except Exception as e:
|
54 |
+
logger.error(f"Error uploading file: {e}")
|
55 |
+
return None # Return None if file upload fails
|
56 |
|
57 |
contents = [
|
58 |
types.Content(
|
|
|
99 |
logger.info(f"File of mime type {inline_data.mime_type} saved to: {temp_path} and prompt input :{text}")
|
100 |
else:
|
101 |
logger.info(f"Received text: {chunk.text}")
|
102 |
+
print(chunk.text)
|
103 |
|
104 |
# Log the raw chunk for deeper inspection
|
105 |
logger.debug(f"Raw chunk: {chunk}")
|
106 |
|
107 |
+
# Check if files was assigned before deleting
|
108 |
+
if files:
|
109 |
+
del files
|
110 |
logger.debug("Uploaded files deleted.")
|
111 |
return temp_path
|
112 |
|
113 |
except Exception as e:
|
114 |
+
logger.exception("An error occurred during generation:")
|
115 |
+
return None # Return None when error happens
|
116 |
|
117 |
|
118 |
def generate_image_from_prompt(prompt, gemini_api_key):
|
|
|
120 |
try:
|
121 |
|
122 |
input_text = prompt
|
123 |
+
model = "gemini-2.0-flash-exp-image-generation"
|
124 |
|
125 |
gemma_generated_image_path = generate(text=input_text, api_key=gemini_api_key, model=model)
|
126 |
|
127 |
+
if gemma_generated_image_path:
|
128 |
logger.debug(f"Image generated at path: {gemma_generated_image_path}")
|
129 |
result_img = Image.open(gemma_generated_image_path)
|
130 |
if result_img.mode == "RGBA":
|
|
|
132 |
return [result_img]
|
133 |
else:
|
134 |
logger.error("generate function returned None.")
|
135 |
+
return []
|
136 |
|
137 |
except Exception as e:
|
138 |
logger.exception("Error occurred in generate_image_from_prompt")
|
139 |
+
return []
|
140 |
|
141 |
# --- Gradio Interface ---
|
142 |
with gr.Blocks() as demo:
|
|
|
180 |
)
|
181 |
|
182 |
try:
|
183 |
+
demo.launch()
|
184 |
except Exception as e:
|
185 |
logger.error(f"Failed to launch Gradio app: {e}")
|
186 |
print(f"Failed to launch Gradio app: {e}")
|
|