Update app.py
Browse files
app.py
CHANGED
|
@@ -2,8 +2,8 @@ import io
|
|
| 2 |
import os
|
| 3 |
import streamlit as st
|
| 4 |
import requests
|
|
|
|
| 5 |
from PIL import Image
|
| 6 |
-
from model import get_caption_model, generate_caption
|
| 7 |
from googletrans import Translator
|
| 8 |
import sqlite3
|
| 9 |
|
|
@@ -13,10 +13,15 @@ st.set_page_config(page_title="Image Caption Generator", layout="wide")
|
|
| 13 |
translator = Translator()
|
| 14 |
|
| 15 |
@st.cache_resource
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
caption_model = get_model()
|
| 20 |
|
| 21 |
# Constants
|
| 22 |
SIGNUP_SUCCESS_MSG = "Signup successful! You can now login."
|
|
@@ -102,14 +107,15 @@ def translate_caption(caption, target_language="en"):
|
|
| 102 |
|
| 103 |
def predict(cap_col, target_language):
|
| 104 |
captions = []
|
| 105 |
-
|
|
|
|
| 106 |
|
| 107 |
cap_col.markdown('#### Predicted Captions:')
|
| 108 |
translated_caption = translate_caption(pred_caption, target_language)
|
| 109 |
captions.append(translated_caption)
|
| 110 |
|
| 111 |
for _ in range(4):
|
| 112 |
-
pred_caption =
|
| 113 |
if pred_caption not in captions:
|
| 114 |
translated_caption = translate_caption(pred_caption, target_language)
|
| 115 |
captions.append(translated_caption)
|
|
|
|
| 2 |
import os
|
| 3 |
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 |
|
|
|
|
| 13 |
translator = Translator()
|
| 14 |
|
| 15 |
@st.cache_resource
|
| 16 |
+
# Image to Text
|
| 17 |
+
def image_to_text(url):
|
| 18 |
+
# Load a transformer
|
| 19 |
+
image_to_text = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
|
| 20 |
+
|
| 21 |
+
text = image_to_text(url)[0]['generated_text']
|
| 22 |
+
|
| 23 |
+
return text
|
| 24 |
|
|
|
|
| 25 |
|
| 26 |
# Constants
|
| 27 |
SIGNUP_SUCCESS_MSG = "Signup successful! You can now login."
|
|
|
|
| 107 |
|
| 108 |
def predict(cap_col, target_language):
|
| 109 |
captions = []
|
| 110 |
+
url = 'tmp.jgp'
|
| 111 |
+
pred_caption = image_to_text(url)
|
| 112 |
|
| 113 |
cap_col.markdown('#### Predicted Captions:')
|
| 114 |
translated_caption = translate_caption(pred_caption, target_language)
|
| 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 = translate_caption(pred_caption, target_language)
|
| 121 |
captions.append(translated_caption)
|