mrrandom123 commited on
Commit
7c4dd54
1 Parent(s): 68d2ac7

Updated app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -3
app.py CHANGED
@@ -1,4 +1,5 @@
1
  import streamlit as st
 
2
  from PIL import Image
3
  from transformers import BlipProcessor, BlipForConditionalGeneration, AutoTokenizer
4
  import itertools
@@ -9,6 +10,10 @@ import torch
9
  import numpy as np
10
  nltk.download('stopwords')
11
 
 
 
 
 
12
  processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
13
  model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
14
 
@@ -27,15 +32,24 @@ def genrate_caption(image_file):
27
  st.title("Image Caption and HashTag Recommender")
28
  image_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
29
 
 
 
 
 
 
 
 
30
  if image_file is not None:
31
  try:
32
  caption = genrate_caption(image_file)
 
 
33
  if len(caption) > 0:
34
  st.write(f"Caption : {caption}")
 
 
35
 
36
  else:
37
  st.write("No caption found for this image.")
38
  except Exception as e:
39
- st.write(f"Error: {e}")
40
-
41
-
 
1
  import streamlit as st
2
+ import cohere
3
  from PIL import Image
4
  from transformers import BlipProcessor, BlipForConditionalGeneration, AutoTokenizer
5
  import itertools
 
10
  import numpy as np
11
  nltk.download('stopwords')
12
 
13
+ COHERE_API_KEY = os.getenv('COHERE_API_KEY')
14
+ co_client = cohere.Client(COHERE_API_KEY)
15
+
16
+
17
  processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
18
  model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
19
 
 
32
  st.title("Image Caption and HashTag Recommender")
33
  image_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
34
 
35
+ def creative_caption(text):
36
+ return co_client.generate(prompt=f"Write some trendy instagram captions for the following prompt - {text}").generations[0].text
37
+
38
+
39
+ def caption_hashtags(text):
40
+ return co_client.generate(prompt=f"Write some trendy instagram hashtags for the following prompt - {text}").generations[0].text
41
+
42
  if image_file is not None:
43
  try:
44
  caption = genrate_caption(image_file)
45
+ caption_text = creative_caption(caption)
46
+ hashtags = caption_hashtags(caption)
47
  if len(caption) > 0:
48
  st.write(f"Caption : {caption}")
49
+ st.write(f"Creative Caption : {caption_text}")
50
+ st.write(f"Creative hashtags : {hashtags}")
51
 
52
  else:
53
  st.write("No caption found for this image.")
54
  except Exception as e:
55
+ st.write(f"Error: {e}")