Hug0endob commited on
Commit
b7311bf
·
verified ·
1 Parent(s): 124f8da

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -9
app.py CHANGED
@@ -126,23 +126,24 @@ with gr.Blocks() as demo:
126
  with gr.Row():
127
  with gr.Column(scale=1):
128
  alt_key = gr.Textbox(label="Alternate Mistral API Key (optional)", type="password")
129
- url_input = gr.Textbox(label="Image URL", placeholder="https://...", value="")
130
  custom = gr.Textbox(label="Custom prompt (optional)", lines=4, placeholder="Leave blank to use auto prompt")
131
  auto_toggle = gr.Checkbox(label="Use detailed auto prompt", value=True)
132
- with gr.Column(scale=1):
133
  preview = gr.Image(label="Preview", type="pil")
 
 
134
  out = gr.Textbox(label="Generated Detailed Description", lines=20)
135
 
136
- def load_preview_and_return_url(url):
137
  if not url:
138
- return None, ""
139
  try:
140
  r = requests.get(url, timeout=30)
141
  r.raise_for_status()
142
  img = Image.open(BytesIO(r.content)).convert("RGB")
143
- return img, url
144
  except Exception:
145
- return None, url
146
 
147
  def start_gen(url, use_auto, custom_p, alt_k):
148
  if not url:
@@ -150,8 +151,7 @@ with gr.Blocks() as demo:
150
  for chunk in generate_stream(url, use_auto, custom_p, alt_k):
151
  yield chunk
152
 
153
- url_input.change(fn=load_preview_and_return_url, inputs=[url_input], outputs=[preview, url_input])
154
- # Generate when Submit (press Enter in URL or add separate button if desired)
155
- url_input.submit(fn=start_gen, inputs=[url_input, auto_toggle, custom, alt_key], outputs=[out])
156
 
157
  demo.launch()
 
126
  with gr.Row():
127
  with gr.Column(scale=1):
128
  alt_key = gr.Textbox(label="Alternate Mistral API Key (optional)", type="password")
129
+ url_input = gr.Textbox(label="Image URL", placeholder="https://...")
130
  custom = gr.Textbox(label="Custom prompt (optional)", lines=4, placeholder="Leave blank to use auto prompt")
131
  auto_toggle = gr.Checkbox(label="Use detailed auto prompt", value=True)
 
132
  preview = gr.Image(label="Preview", type="pil")
133
+ submit = gr.Button("Submit")
134
+ with gr.Column(scale=1):
135
  out = gr.Textbox(label="Generated Detailed Description", lines=20)
136
 
137
+ def load_preview(url):
138
  if not url:
139
+ return None
140
  try:
141
  r = requests.get(url, timeout=30)
142
  r.raise_for_status()
143
  img = Image.open(BytesIO(r.content)).convert("RGB")
144
+ return img
145
  except Exception:
146
+ return None
147
 
148
  def start_gen(url, use_auto, custom_p, alt_k):
149
  if not url:
 
151
  for chunk in generate_stream(url, use_auto, custom_p, alt_k):
152
  yield chunk
153
 
154
+ url_input.change(fn=load_preview, inputs=[url_input], outputs=[preview])
155
+ submit.click(fn=start_gen, inputs=[url_input, auto_toggle, custom, alt_key], outputs=[out])
 
156
 
157
  demo.launch()