Athspi commited on
Commit
c90c576
·
verified ·
1 Parent(s): fea0355

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -18
app.py CHANGED
@@ -9,8 +9,7 @@ from google.genai import types
9
  # Initialize Flask app
10
  app = Flask(__name__)
11
 
12
- # Set your Gemini API key via Hugging Face Spaces environment variables.
13
- # Do not include a default fallback; the environment must supply GEMINI_API_KEY.
14
  GEMINI_API_KEY = os.environ["GEMINI_API_KEY"]
15
  client = genai.Client(api_key=GEMINI_API_KEY)
16
 
@@ -25,7 +24,7 @@ def generate_gemini_output(object_type, image_data_url):
25
  Generate output from Gemini by removing the specified object.
26
  Expects the image_data_url to be a base64 data URL.
27
  """
28
- model = "gemini-2.0-flash-exp-image-generation"
29
  files = []
30
 
31
  # Decode the image data from the data URL
@@ -61,15 +60,14 @@ def generate_gemini_output(object_type, image_data_url):
61
  top_p=0.95,
62
  top_k=40,
63
  max_output_tokens=8192,
64
- response_modalities=["image", "text"],
65
- safety_settings=[
66
- types.SafetySetting(category="HARM_CATEGORY_CIVIC_INTEGRITY", threshold="OFF"),
67
- ],
68
  response_mime_type="text/plain",
 
 
 
 
69
  )
70
 
71
  result_text = None
72
- result_image = None
73
 
74
  # Stream output from Gemini API
75
  for chunk in client.models.generate_content_stream(
@@ -80,17 +78,31 @@ def generate_gemini_output(object_type, image_data_url):
80
  if not chunk.candidates or not chunk.candidates[0].content or not chunk.candidates[0].content.parts:
81
  continue
82
  part = chunk.candidates[0].content.parts[0]
83
- if part.inline_data:
84
- file_extension = mimetypes.guess_extension(part.inline_data.mime_type) or ".png"
85
- output_filename = secure_filename("generated_output" + file_extension)
86
- result_image_path = os.path.join(RESULT_FOLDER, output_filename)
87
- with open(result_image_path, "wb") as f:
88
- f.write(part.inline_data.data)
89
- result_image = result_image_path # Path relative to static folder
90
- else:
91
  result_text = part.text
92
 
93
- return result_text, result_image
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94
 
95
  @app.route("/")
96
  def index():
@@ -110,7 +122,7 @@ def process():
110
  # Generate output using Gemini
111
  result_text, result_image = generate_gemini_output(object_type, image_data)
112
  if not result_image:
113
- return jsonify({"success": False, "message": "Failed to generate image."}), 500
114
 
115
  # Create a URL to serve the image from the static folder.
116
  image_url = f"/static/{os.path.basename(result_image)}"
 
9
  # Initialize Flask app
10
  app = Flask(__name__)
11
 
12
+ # Read the Gemini API key from environment variables (set in Hugging Face Spaces)
 
13
  GEMINI_API_KEY = os.environ["GEMINI_API_KEY"]
14
  client = genai.Client(api_key=GEMINI_API_KEY)
15
 
 
24
  Generate output from Gemini by removing the specified object.
25
  Expects the image_data_url to be a base64 data URL.
26
  """
27
+ model = "gemini-2.0-flash-lite" # Use the lite model for text-based responses
28
  files = []
29
 
30
  # Decode the image data from the data URL
 
60
  top_p=0.95,
61
  top_k=40,
62
  max_output_tokens=8192,
 
 
 
 
63
  response_mime_type="text/plain",
64
+ system_instruction=[
65
+ types.Part.from_text(text="""Your AI finds user requests about removing objects from images.
66
+ If the user asks to remove a person or animal, respond with 'No'."""),
67
+ ],
68
  )
69
 
70
  result_text = None
 
71
 
72
  # Stream output from Gemini API
73
  for chunk in client.models.generate_content_stream(
 
78
  if not chunk.candidates or not chunk.candidates[0].content or not chunk.candidates[0].content.parts:
79
  continue
80
  part = chunk.candidates[0].content.parts[0]
81
+ if part.text:
 
 
 
 
 
 
 
82
  result_text = part.text
83
 
84
+ # If the response is "No", switch to the image generation model
85
+ if result_text and "no" in result_text.lower():
86
+ model = "gemini-2.0-flash-exp-image-generation"
87
+ generate_content_config.response_modalities = ["image", "text"]
88
+ for chunk in client.models.generate_content_stream(
89
+ model=model,
90
+ contents=contents,
91
+ config=generate_content_config,
92
+ ):
93
+ if not chunk.candidates or not chunk.candidates[0].content or not chunk.candidates[0].content.parts:
94
+ continue
95
+ part = chunk.candidates[0].content.parts[0]
96
+ if part.inline_data:
97
+ file_extension = mimetypes.guess_extension(part.inline_data.mime_type) or ".png"
98
+ output_filename = secure_filename("generated_output" + file_extension)
99
+ result_image_path = os.path.join(RESULT_FOLDER, output_filename)
100
+ with open(result_image_path, "wb") as f:
101
+ f.write(part.inline_data.data)
102
+ result_image = result_image_path # Path relative to static folder
103
+ return result_text, result_image
104
+
105
+ return result_text, None
106
 
107
  @app.route("/")
108
  def index():
 
122
  # Generate output using Gemini
123
  result_text, result_image = generate_gemini_output(object_type, image_data)
124
  if not result_image:
125
+ return jsonify({"success": False, "message": result_text or "Failed to generate image."}), 500
126
 
127
  # Create a URL to serve the image from the static folder.
128
  image_url = f"/static/{os.path.basename(result_image)}"