Update app.py
Browse files
app.py
CHANGED
|
@@ -4,15 +4,59 @@ import streamlit as st
|
|
| 4 |
import requests
|
| 5 |
from transformers import pipeline
|
| 6 |
from PIL import Image
|
| 7 |
-
from googletrans import Translator
|
| 8 |
import sqlite3
|
| 9 |
|
| 10 |
# Initialize Streamlit app
|
| 11 |
st.set_page_config(page_title="Image Caption Generator", layout="wide")
|
| 12 |
|
| 13 |
-
|
| 14 |
|
| 15 |
@st.cache_resource
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
# Image to Text
|
| 17 |
def image_to_text(url):
|
| 18 |
# Load a transformer
|
|
@@ -101,9 +145,7 @@ def login_section():
|
|
| 101 |
except sqlite3.OperationalError as e:
|
| 102 |
st.error(f"An error occurred while trying to log in: {e}")
|
| 103 |
|
| 104 |
-
|
| 105 |
-
translated = translator.translate(caption, dest=target_language)
|
| 106 |
-
return translated.text
|
| 107 |
|
| 108 |
def predict(cap_col, target_language):
|
| 109 |
captions = []
|
|
@@ -111,13 +153,13 @@ def predict(cap_col, target_language):
|
|
| 111 |
pred_caption = image_to_text(url)
|
| 112 |
|
| 113 |
cap_col.markdown('#### Predicted Captions:')
|
| 114 |
-
translated_caption =
|
| 115 |
captions.append(translated_caption)
|
| 116 |
|
| 117 |
for _ in range(4):
|
| 118 |
pred_caption = image_to_text(url)
|
| 119 |
if pred_caption not in captions:
|
| 120 |
-
translated_caption =
|
| 121 |
captions.append(translated_caption)
|
| 122 |
|
| 123 |
cap_col.markdown('<div class="caption-container">', unsafe_allow_html=True)
|
|
@@ -136,7 +178,9 @@ def generate_caption_section():
|
|
| 136 |
img_upload = st.file_uploader(label='Upload Image', type=['jpg', 'png', 'jpeg'])
|
| 137 |
|
| 138 |
# Language selection dropdown
|
| 139 |
-
|
|
|
|
|
|
|
| 140 |
|
| 141 |
# Process image and generate captions
|
| 142 |
if img_url:
|
|
|
|
| 4 |
import requests
|
| 5 |
from transformers import pipeline
|
| 6 |
from PIL import Image
|
|
|
|
| 7 |
import sqlite3
|
| 8 |
|
| 9 |
# Initialize Streamlit app
|
| 10 |
st.set_page_config(page_title="Image Caption Generator", layout="wide")
|
| 11 |
|
| 12 |
+
|
| 13 |
|
| 14 |
@st.cache_resource
|
| 15 |
+
|
| 16 |
+
##Bloom
|
| 17 |
+
API_URL = "https://api-inference.huggingface.co/models/bigscience/bloom"
|
| 18 |
+
# HF_TOKEN = os.environ["HF_TOKEN"]
|
| 19 |
+
# headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
| 20 |
+
|
| 21 |
+
def translate(prompt_ , to_lang):
|
| 22 |
+
input_prompt = "translate this"
|
| 23 |
+
seed = 42
|
| 24 |
+
prompt = f"Instruction : Given an English input sentence translate it into {to_lang} sentence. \n input : \"{prompt_}\" \n {to_lang} : "
|
| 25 |
+
if len(prompt) == 0:
|
| 26 |
+
prompt = input_prompt
|
| 27 |
+
|
| 28 |
+
json_ = {
|
| 29 |
+
"inputs": prompt,
|
| 30 |
+
"parameters": {
|
| 31 |
+
"top_p": 0.9,
|
| 32 |
+
"temperature": 1.1,
|
| 33 |
+
"max_new_tokens": 250,
|
| 34 |
+
"return_full_text": False,
|
| 35 |
+
"do_sample": False,
|
| 36 |
+
"seed": seed,
|
| 37 |
+
"early_stopping": False,
|
| 38 |
+
"length_penalty": 0.0,
|
| 39 |
+
"eos_token_id": None,
|
| 40 |
+
},
|
| 41 |
+
"options": {
|
| 42 |
+
"use_cache": True,
|
| 43 |
+
"wait_for_model": True,
|
| 44 |
+
},
|
| 45 |
+
}
|
| 46 |
+
response = requests.request("POST", API_URL, json=json_) # headers=headers
|
| 47 |
+
# output = response.json()
|
| 48 |
+
output = json.loads(response.content.decode("utf-8"))
|
| 49 |
+
output_tmp = output[0]['generated_text']
|
| 50 |
+
solution = output_tmp.split(f"\n{to_lang}:")[0]
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
if '\n\n' in solution:
|
| 54 |
+
final_solution = solution.split("\n\n")[0]
|
| 55 |
+
else:
|
| 56 |
+
final_solution = solution
|
| 57 |
+
return final_solution
|
| 58 |
+
|
| 59 |
+
|
| 60 |
# Image to Text
|
| 61 |
def image_to_text(url):
|
| 62 |
# Load a transformer
|
|
|
|
| 145 |
except sqlite3.OperationalError as e:
|
| 146 |
st.error(f"An error occurred while trying to log in: {e}")
|
| 147 |
|
| 148 |
+
|
|
|
|
|
|
|
| 149 |
|
| 150 |
def predict(cap_col, target_language):
|
| 151 |
captions = []
|
|
|
|
| 153 |
pred_caption = image_to_text(url)
|
| 154 |
|
| 155 |
cap_col.markdown('#### Predicted Captions:')
|
| 156 |
+
translated_caption = translate(pred_caption, target_language)
|
| 157 |
captions.append(translated_caption)
|
| 158 |
|
| 159 |
for _ in range(4):
|
| 160 |
pred_caption = image_to_text(url)
|
| 161 |
if pred_caption not in captions:
|
| 162 |
+
translated_caption = translate(pred_caption, target_language)
|
| 163 |
captions.append(translated_caption)
|
| 164 |
|
| 165 |
cap_col.markdown('<div class="caption-container">', unsafe_allow_html=True)
|
|
|
|
| 178 |
img_upload = st.file_uploader(label='Upload Image', type=['jpg', 'png', 'jpeg'])
|
| 179 |
|
| 180 |
# Language selection dropdown
|
| 181 |
+
|
| 182 |
+
|
| 183 |
+
target_language = st.selectbox('Select Target Language', ['English', 'Spanish', 'Hindi', 'Italian'], index=0)
|
| 184 |
|
| 185 |
# Process image and generate captions
|
| 186 |
if img_url:
|