Arfa-ilyas commited on
Commit
e6b6f9e
·
verified ·
1 Parent(s): 0588656

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -29
app.py CHANGED
@@ -68,41 +68,57 @@ def enhance_description_with_gemini(description):
68
  return "No contents or parts in response"
69
  except requests.exceptions.RequestException as e:
70
  return f"Request failed: {e}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71
 
72
  def generate_barcode(data):
73
  code = barcode.get('ean13', data, writer=ImageWriter())
74
  barcode_image = code.render()
75
  return barcode_image
76
 
77
- def create_label(image, ingredients, usage, expiry_date, barcode_data):
78
- # Create an image for the label
79
- label_image = Image.new('RGB', (800, 1200), color='white')
80
- draw = ImageDraw.Draw(label_image)
81
-
82
- # Define font and size
83
- try:
84
- font = ImageFont.truetype("arial.ttf", 24)
85
- except IOError:
86
- font = ImageFont.load_default()
87
-
88
- # Draw the main image
89
- image.thumbnail((600, 400))
90
- label_image.paste(image, (100, 50))
91
-
92
- # Draw text
93
- text_position = (100, 500)
94
- draw.text(text_position, f"Ingredients:\n{ingredients}", font=font, fill="black")
95
- text_position = (100, 600)
96
- draw.text(text_position, f"Usage:\n{usage}", font=font, fill="black")
97
- text_position = (100, 700)
98
- draw.text(text_position, f"Expiry Date: {expiry_date}", font=font, fill="black")
99
-
100
- # Draw barcode
101
- barcode_image = generate_barcode(barcode_data)
102
- label_image.paste(barcode_image, (100, 800))
103
-
104
- return label_image
105
-
106
  # Streamlit interface
107
  st.title("Image Detailed Description, Captions, Logo Suggestions Generator with Image Enhancement Options")
108
 
 
68
  return "No contents or parts in response"
69
  except requests.exceptions.RequestException as e:
70
  return f"Request failed: {e}"
71
+ def create_label(image, ingredients, usage, expiry_date, barcode_data):
72
+ try:
73
+ # Create an image for the label
74
+ label_image = Image.new('RGB', (800, 1200), color='white')
75
+ draw = ImageDraw.Draw(label_image)
76
+
77
+ # Define font and size
78
+ try:
79
+ font = ImageFont.truetype("arial.ttf", 24)
80
+ except IOError:
81
+ font = ImageFont.load_default()
82
+
83
+ # Draw the main image
84
+ image.thumbnail((600, 400))
85
+ label_image.paste(image, (100, 50))
86
+
87
+ # Draw text
88
+ text_position = (100, 500)
89
+ draw.text(text_position, f"Ingredients:\n{ingredients}", font=font, fill="black")
90
+ text_position = (100, 600)
91
+ draw.text(text_position, f"Usage:\n{usage}", font=font, fill="black")
92
+ text_position = (100, 700)
93
+ draw.text(text_position, f"Expiry Date: {expiry_date}", font=font, fill="black")
94
+
95
+ # Draw barcode
96
+ if len(barcode_data) == 12: # Check length for EAN-13
97
+ barcode_image = generate_barcode(barcode_data)
98
+ label_image.paste(barcode_image, (100, 800))
99
+ else:
100
+ draw.text((100, 800), "Invalid Barcode Data", font=font, fill="red")
101
+
102
+ return label_image
103
+ except Exception as e:
104
+ raise RuntimeError(f"Error generating label: {e}")
105
+
106
+ # Streamlit interface continuation
107
+ if st.button('Generate Label'):
108
+ if ingredients and usage and expiry_date and barcode_data:
109
+ try:
110
+ label_image = create_label(image, ingredients, usage, expiry_date, barcode_data)
111
+ st.image(label_image, caption="Generated Label")
112
+ except Exception as e:
113
+ st.error(f"Error generating label: {e}")
114
+ else:
115
+ st.error("Please provide all required details for the label.")
116
 
117
  def generate_barcode(data):
118
  code = barcode.get('ean13', data, writer=ImageWriter())
119
  barcode_image = code.render()
120
  return barcode_image
121
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
122
  # Streamlit interface
123
  st.title("Image Detailed Description, Captions, Logo Suggestions Generator with Image Enhancement Options")
124