Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -25,8 +25,8 @@ def save_binary_file(file_name, data):
|
|
25 |
logger.debug(f"File saved successfully: {file_name}")
|
26 |
|
27 |
|
28 |
-
def generate(text,
|
29 |
-
logger.debug(f"Starting generate function with text: '{text}',
|
30 |
|
31 |
try:
|
32 |
# Initialize client
|
@@ -41,20 +41,10 @@ def generate(text, file_name, api_key, model="gemini-2.0-flash-exp-image-generat
|
|
41 |
logger.debug("Gemini client initialized.")
|
42 |
|
43 |
|
44 |
-
files = [
|
45 |
-
client.files.upload(file=file_name),
|
46 |
-
]
|
47 |
-
logger.debug(f"File uploaded. URI: {files[0].uri}, MIME Type: {files[0].mime_type}")
|
48 |
-
|
49 |
-
|
50 |
contents = [
|
51 |
types.Content(
|
52 |
role="user",
|
53 |
parts=[
|
54 |
-
types.Part.from_uri(
|
55 |
-
file_uri=files[0].uri,
|
56 |
-
mime_type=files[0].mime_type,
|
57 |
-
),
|
58 |
types.Part.from_text(text=text),
|
59 |
],
|
60 |
),
|
@@ -110,23 +100,18 @@ def generate(text, file_name, api_key, model="gemini-2.0-flash-exp-image-generat
|
|
110 |
return None # Return None when error happens
|
111 |
|
112 |
|
113 |
-
def
|
114 |
-
logger.debug(f"Starting
|
115 |
try:
|
116 |
-
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp:
|
117 |
-
composite_path = tmp.name
|
118 |
-
composite_pil.save(composite_path)
|
119 |
-
logger.debug(f"Composite image saved to: {composite_path}")
|
120 |
|
121 |
-
file_name = composite_path
|
122 |
input_text = prompt
|
123 |
model = "gemini-2.0-flash-exp-image-generation" # Consider changing this to "gemini-pro-vision"
|
124 |
|
125 |
-
|
126 |
|
127 |
-
if
|
128 |
-
logger.debug(f"Image generated at path: {
|
129 |
-
result_img = Image.open(
|
130 |
if result_img.mode == "RGBA":
|
131 |
result_img = result_img.convert("RGB")
|
132 |
return [result_img]
|
@@ -135,10 +120,9 @@ def process_image_and_prompt(composite_pil, prompt, gemini_api_key):
|
|
135 |
return [] # Return empty when error
|
136 |
|
137 |
except Exception as e:
|
138 |
-
logger.exception("Error occurred in
|
139 |
return [] # Return empty when error
|
140 |
|
141 |
-
|
142 |
# --- Gradio Interface ---
|
143 |
with gr.Blocks() as demo:
|
144 |
gr.HTML(
|
@@ -149,17 +133,16 @@ with gr.Blocks() as demo:
|
|
149 |
</div>
|
150 |
<div>
|
151 |
<h1></h1>
|
152 |
-
<p
|
153 |
<p>API Key ကို <a href="https://aistudio.google.com/apikey">ဤနေရာ</a> တွင် ဖန်တီးပါ</p>
|
154 |
</div>
|
155 |
</div>
|
156 |
"""
|
157 |
)
|
158 |
-
gr.Markdown("
|
159 |
|
160 |
with gr.Row():
|
161 |
with gr.Column():
|
162 |
-
image_input = gr.Image(type="pil", label="ပုံတင်ရန်", image_mode="RGBA")
|
163 |
gemini_api_key = gr.Textbox(
|
164 |
lines=1,
|
165 |
placeholder="Gemini API Key ထည့်ပါ",
|
@@ -176,8 +159,8 @@ with gr.Blocks() as demo:
|
|
176 |
output_gallery = gr.Gallery(label="ထုတ်လုပ်ပြီးရလဒ်များ")
|
177 |
|
178 |
submit_btn.click(
|
179 |
-
fn=
|
180 |
-
inputs=[
|
181 |
outputs=output_gallery,
|
182 |
)
|
183 |
|
@@ -185,5 +168,5 @@ try:
|
|
185 |
demo.launch(share=True)
|
186 |
except Exception as e:
|
187 |
logger.error(f"Failed to launch Gradio app: {e}")
|
188 |
-
print(f"Failed to launch Gradio app: {e}")
|
189 |
|
|
|
25 |
logger.debug(f"File saved successfully: {file_name}")
|
26 |
|
27 |
|
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
|
|
|
41 |
logger.debug("Gemini client initialized.")
|
42 |
|
43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
contents = [
|
45 |
types.Content(
|
46 |
role="user",
|
47 |
parts=[
|
|
|
|
|
|
|
|
|
48 |
types.Part.from_text(text=text),
|
49 |
],
|
50 |
),
|
|
|
100 |
return None # Return None when error happens
|
101 |
|
102 |
|
103 |
+
def generate_image_from_prompt(prompt, gemini_api_key):
|
104 |
+
logger.debug(f"Starting generate_image_from_prompt with prompt: '{prompt}'")
|
105 |
try:
|
|
|
|
|
|
|
|
|
106 |
|
|
|
107 |
input_text = prompt
|
108 |
model = "gemini-2.0-flash-exp-image-generation" # Consider changing this to "gemini-pro-vision"
|
109 |
|
110 |
+
gemma_generated_image_path = generate(text=input_text, api_key=gemini_api_key, model=model)
|
111 |
|
112 |
+
if gemma_generated_image_path: # Check none or not
|
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":
|
116 |
result_img = result_img.convert("RGB")
|
117 |
return [result_img]
|
|
|
120 |
return [] # Return empty when error
|
121 |
|
122 |
except Exception as e:
|
123 |
+
logger.exception("Error occurred in generate_image_from_prompt")
|
124 |
return [] # Return empty when error
|
125 |
|
|
|
126 |
# --- Gradio Interface ---
|
127 |
with gr.Blocks() as demo:
|
128 |
gr.HTML(
|
|
|
133 |
</div>
|
134 |
<div>
|
135 |
<h1></h1>
|
136 |
+
<p>စာသားမှပုံသို့ပြောင်းပါ</p>
|
137 |
<p>API Key ကို <a href="https://aistudio.google.com/apikey">ဤနေရာ</a> တွင် ဖန်တီးပါ</p>
|
138 |
</div>
|
139 |
</div>
|
140 |
"""
|
141 |
)
|
142 |
+
gr.Markdown("သင်လိုချင်တဲ့ပုံအတွက် စာသား prompt ကိုထည့်ပါ။")
|
143 |
|
144 |
with gr.Row():
|
145 |
with gr.Column():
|
|
|
146 |
gemini_api_key = gr.Textbox(
|
147 |
lines=1,
|
148 |
placeholder="Gemini API Key ထည့်ပါ",
|
|
|
159 |
output_gallery = gr.Gallery(label="ထုတ်လုပ်ပြီးရလဒ်များ")
|
160 |
|
161 |
submit_btn.click(
|
162 |
+
fn=generate_image_from_prompt,
|
163 |
+
inputs=[prompt_input, gemini_api_key],
|
164 |
outputs=output_gallery,
|
165 |
)
|
166 |
|
|
|
168 |
demo.launch(share=True)
|
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 |
|