SatyamSinghal commited on
Commit
ad49b96
·
verified ·
1 Parent(s): 5e37e94

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -12
app.py CHANGED
@@ -12,15 +12,17 @@ def generate_caption(image):
12
  """Queries the Hugging Face API to generate an image caption."""
13
  if not API_KEY:
14
  return "API key is missing! Please set it in Hugging Face secrets."
15
- response = requests.post(API_URL, headers=headers, files={"file": image})
16
- if response.status_code == 200:
 
 
17
  result = response.json()
18
  return result[0].get("generated_text", "No caption generated.")
19
- else:
20
- return f"Error: {response.status_code}, {response.text}"
21
 
22
  # Gradio UI components
23
- title = "✨Captionator_X ✨"
24
  description = """
25
  Upload an image, and let AI describe it for you in a creative and accurate way!
26
  This application uses Hugging Face's state-of-the-art image captioning model.
@@ -28,18 +30,18 @@ This application uses Hugging Face's state-of-the-art image captioning model.
28
  theme = gr.themes.Monochrome()
29
 
30
  with gr.Blocks(theme=theme) as app:
31
- gr.Markdown(f"<h1 style='text-align: center;'>{title}</h1>")
32
- gr.Markdown(f"<p style='text-align: center;'>{description}</p>")
33
 
34
  with gr.Row():
35
  with gr.Column():
36
- image_input = gr.Image(type="file", label="Upload Your Image", elem_id="image-uploader")
37
  with gr.Column():
38
- output_text = gr.Textbox(label="Generated Caption", lines=4, interactive=False, elem_id="output-text")
39
 
40
  submit_button = gr.Button("✨ Generate Caption ✨", elem_id="submit-btn")
41
- submit_button.click(generate_caption, inputs=[image_input], outputs=[output_text])
42
 
43
- gr.Markdown("<p style='text-align: center; font-size: 0.9rem;'>Powered by Hugging Face 🤗 and Gradio 🚀</p>")
44
 
45
- app.launch()
 
12
  """Queries the Hugging Face API to generate an image caption."""
13
  if not API_KEY:
14
  return "API key is missing! Please set it in Hugging Face secrets."
15
+
16
+ try:
17
+ response = requests.post(API_URL, headers=headers, files={"file": image})
18
+ response.raise_for_status() # Raise an error for bad responses
19
  result = response.json()
20
  return result[0].get("generated_text", "No caption generated.")
21
+ except requests.exceptions.RequestException as e:
22
+ return f"Error: {str(e)}"
23
 
24
  # Gradio UI components
25
+ title = "✨ Captionator_X ✨"
26
  description = """
27
  Upload an image, and let AI describe it for you in a creative and accurate way!
28
  This application uses Hugging Face's state-of-the-art image captioning model.
 
30
  theme = gr.themes.Monochrome()
31
 
32
  with gr.Blocks(theme=theme) as app:
33
+ gr.Markdown(f"<h1 style='text-align: center; color: #4A90E2;'>{title}</h1>")
34
+ gr.Markdown(f"<p style='text-align: center; font-size: 1.2rem;'>{description}</p>")
35
 
36
  with gr.Row():
37
  with gr.Column():
38
+ image_input = gr.Image(type="file", label="Upload Your Image", elem_id="image-uploader", tooltips="Click to upload an image.")
39
  with gr.Column():
40
+ output_text = gr.Textbox(label="Generated Caption", lines=4, interactive=False, elem_id="output-text", placeholder="Your caption will appear here...", tooltips="Generated caption will be displayed here.")
41
 
42
  submit_button = gr.Button("✨ Generate Caption ✨", elem_id="submit-btn")
43
+ submit_button.click(generate_caption, inputs=image_input, outputs=output_text)
44
 
45
+ gr.Markdown("<p style='text-align: center; font-size: 0.9rem; color: #888;'>Powered by Hugging Face 🤗 and Gradio 🚀</p>")
46
 
47
+ app.launch()