admin08077 commited on
Commit
39ec408
·
verified ·
1 Parent(s): 5f9f87b

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -0
app.py ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from google import genai
3
+ from google.genai import types
4
+ from PIL import Image
5
+ from io import BytesIO
6
+ import os
7
+
8
+ # Make sure your GEMINI_API_KEY is set in Hugging Face secrets/environment
9
+ GEMINI_API_KEY = os.environ.get("GEMINI_API_KEY")
10
+ if not GEMINI_API_KEY:
11
+ raise ValueError("GEMINI_API_KEY not found in environment variables.")
12
+
13
+ # Initialize Gemini client
14
+ client = genai.Client(api_key=GEMINI_API_KEY)
15
+
16
+ # Gradio function
17
+ def generate_image_and_caption(prompt):
18
+ try:
19
+ response = client.models.generate_content(
20
+ model="gemini-2.0-flash-preview-image-generation",
21
+ contents=prompt,
22
+ config=types.GenerateContentConfig(
23
+ response_modalities=['TEXT', 'IMAGE']
24
+ )
25
+ )
26
+
27
+ caption = "No text response."
28
+ img = None
29
+
30
+ for part in response.candidates[0].content.parts:
31
+ if part.text:
32
+ caption = part.text
33
+ elif part.inline_data:
34
+ img = Image.open(BytesIO(part.inline_data.data))
35
+
36
+ return caption, img
37
+
38
+ except Exception as e:
39
+ return f"Error: {str(e)}", None
40
+
41
+ # Gradio UI
42
+ with gr.Blocks(title="Gemini 2.0 Flash Preview Image Generator") as demo:
43
+ gr.Markdown("## 🧠✨ Gemini 2.0 Flash Preview Image Generator")
44
+ gr.Markdown("Enter a prompt and get a 3D rendered image + text generated by Google Gemini.")
45
+
46
+ with gr.Row():
47
+ prompt = gr.Textbox(label="Your Prompt", placeholder="Describe the scene...")
48
+ submit = gr.Button("Generate")
49
+
50
+ output_text = gr.Textbox(label="Generated Description")
51
+ output_image = gr.Image(label="Generated Image")
52
+
53
+ submit.click(fn=generate_image_and_caption, inputs=prompt, outputs=[output_text, output_image])
54
+
55
+ # Run the app
56
+ if __name__ == "__main__":
57
+ demo.launch()