victorgeek commited on
Commit
025bafe
·
verified ·
1 Parent(s): 4969292

Upload 2 files

Browse files
Files changed (2) hide show
  1. README.md +8 -7
  2. app.py +176 -0
README.md CHANGED
@@ -1,13 +1,14 @@
1
  ---
2
- title: '2'
3
- emoji: 📉
4
- colorFrom: blue
5
- colorTo: blue
6
  sdk: gradio
7
- sdk_version: 5.27.1
8
  app_file: app.py
9
  pinned: false
10
- license: apache-2.0
 
11
  ---
12
 
13
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
+ title: Gemini 2.0 Image Gen
3
+ emoji: 💻
4
+ colorFrom: red
5
+ colorTo: indigo
6
  sdk: gradio
7
+ sdk_version: 5.23.1
8
  app_file: app.py
9
  pinned: false
10
+ license: mit
11
+ short_description: ♊Generate images with text prompts using Gemini♊
12
  ---
13
 
14
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,176 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import os
3
+ import time
4
+ import uuid
5
+ import tempfile
6
+ from PIL import Image
7
+ import gradio as gr
8
+ import base64
9
+ import mimetypes
10
+ import logging
11
+
12
+ from google import genai
13
+ from google.genai import types
14
+
15
+ # Configure logging
16
+ logging.basicConfig(level=logging.DEBUG,
17
+ format='%(asctime)s - %(levelname)s - %(message)s')
18
+ logger = logging.getLogger(__name__)
19
+
20
+
21
+ def save_binary_file(file_name, data):
22
+ logger.debug(f"Saving binary data to file: {file_name}")
23
+ with open(file_name, "wb") as f:
24
+ f.write(data)
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
+ 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")
35
+ logger.debug(f"Using API Key: {'Provided' if api_key.strip() else 'From Environment Variable'}")
36
+
37
+ if not effective_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
+ contents = [
49
+ types.Content(
50
+ role="user",
51
+ parts=[
52
+ types.Part.from_text(text=text),
53
+ ],
54
+ ),
55
+ ]
56
+ logger.debug(f"Content object created: {contents}")
57
+
58
+ generate_content_config = types.GenerateContentConfig(
59
+ temperature=1,
60
+ top_p=0.95,
61
+ top_k=40,
62
+ max_output_tokens=8192,
63
+ response_modalities=[
64
+ "image",
65
+ "text",
66
+ ],
67
+ response_mime_type="text/plain",
68
+ )
69
+ logger.debug(f"Generate content config: {generate_content_config}")
70
+
71
+ with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp:
72
+ temp_path = tmp.name
73
+ logger.debug(f"Temporary file created: {temp_path}")
74
+
75
+ response_stream = client.models.generate_content_stream(
76
+ model=model,
77
+ contents=contents,
78
+ config=generate_content_config,
79
+ )
80
+
81
+ logger.debug("Starting to process response stream...")
82
+ for chunk in response_stream:
83
+ if not chunk.candidates or not chunk.candidates[0].content or not chunk.candidates[0].content.parts:
84
+ logger.warning("Chunk has no candidates, content, or parts. Skipping.")
85
+ continue
86
+
87
+ inline_data = chunk.candidates[0].content.parts[0].inline_data
88
+ if inline_data:
89
+ save_binary_file(temp_path, inline_data.data)
90
+ logger.info(f"File of mime type {inline_data.mime_type} saved to: {temp_path} and prompt input :{text}")
91
+ else:
92
+ logger.info(f"Received text: {chunk.text}")
93
+ print(chunk.text)
94
+
95
+ # Log the raw chunk for deeper inspection
96
+ logger.debug(f"Raw chunk: {chunk}")
97
+
98
+ logger.debug("Uploaded files deleted.")
99
+ return temp_path
100
+
101
+ except Exception as e:
102
+ logger.exception("An error occurred during generation:")
103
+ return None # Return None when error happens
104
+
105
+
106
+ def generate_image_from_prompt(prompt, gemini_api_key):
107
+ logger.debug(f"Starting generate_image_from_prompt with prompt: '{prompt}'")
108
+ try:
109
+
110
+ input_text = prompt
111
+ model = "gemini-2.0-flash-exp-image-generation"
112
+
113
+ gemma_generated_image_path = generate(text=input_text, api_key=gemini_api_key, model=model)
114
+
115
+ if gemma_generated_image_path:
116
+ logger.debug(f"Image generated at path: {gemma_generated_image_path}")
117
+ result_img = Image.open(gemma_generated_image_path)
118
+ if result_img.mode == "RGBA":
119
+ result_img = result_img.convert("RGB")
120
+ return [result_img]
121
+ else:
122
+ logger.error("generate function returned None.")
123
+ return []
124
+
125
+ except Exception as e:
126
+ logger.exception("Error occurred in generate_image_from_prompt")
127
+ return []
128
+
129
+
130
+ # --- Gradio Interface ---
131
+ with gr.Blocks() as demo:
132
+ gr.HTML(
133
+ """
134
+ <div style='display: flex; align-items: center; justify-content: center; gap: 20px'>
135
+ <div style="background-color: var(--block-background-fill); border-radius: 8px">
136
+ <img src="https://www.gstatic.com/lamda/images/gemini_favicon_f069958c85030456e93de685481c559f160ea06b.png" style="width: 100px; height: 100px;">
137
+ </div>
138
+ <div>
139
+ <h1></h1>
140
+ <p>စာသားမှပုံသို့ပြောင်းပါ</p>
141
+ <p>API Key ကို <a href="https://aistudio.google.com/apikey">ဤနေရာ</a> တွင် ဖန်တီးပါ</p>
142
+ </div>
143
+ </div>
144
+ """
145
+ )
146
+ gr.Markdown("သင်လိုချင်တဲ့ပုံအတွက် စာသား prompt ကိုထည့်ပါ။")
147
+
148
+ with gr.Row():
149
+ with gr.Column():
150
+ gemini_api_key = gr.Textbox(
151
+ lines=1,
152
+ placeholder="Gemini API Key ထည့်ပါ",
153
+ label="Gemini API Key",
154
+ type="password"
155
+ )
156
+ prompt_input = gr.Textbox(
157
+ lines=2,
158
+ placeholder="သင်လိုချင်တာကို ဤနေရာတွင် ရိုက်ထည့်ပါ...",
159
+ label="သင်လိုချင်တာ"
160
+ )
161
+ submit_btn = gr.Button("ထုတ်လုပ်ပါ")
162
+ with gr.Column():
163
+ output_gallery = gr.Gallery(label="ထုတ်လုပ်ပြီးရလဒ်များ")
164
+
165
+ submit_btn.click(
166
+ fn=generate_image_from_prompt,
167
+ inputs=[prompt_input, gemini_api_key],
168
+ outputs=output_gallery,
169
+ )
170
+
171
+ try:
172
+ demo.launch()
173
+ except Exception as e:
174
+ logger.error(f"Failed to launch Gradio app: {e}")
175
+ print(f"Failed to launch Gradio app: {e}")
176
+