Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,183 +1,183 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import json
|
| 3 |
-
import streamlit as st
|
| 4 |
-
from PIL import Image, UnidentifiedImageError, ExifTags
|
| 5 |
-
import requests
|
| 6 |
-
from io import BytesIO
|
| 7 |
-
import wikipedia
|
| 8 |
-
from easygoogletranslate import EasyGoogleTranslate
|
| 9 |
-
from BharatCaptioner import identify_landmark
|
| 10 |
-
from groq import Groq
|
| 11 |
-
import hashlib
|
| 12 |
-
|
| 13 |
-
# Initialize EasyGoogleTranslate
|
| 14 |
-
translator = EasyGoogleTranslate(source_language="en", target_language="hi", timeout=10)
|
| 15 |
-
|
| 16 |
-
# Load configuration for Groq API key
|
| 17 |
-
working_dir = os.path.dirname(os.path.abspath(__file__))
|
| 18 |
-
config_data = json.load(open(f"{working_dir}/config.json"))
|
| 19 |
-
GROQ_API_KEY = config_data["GROQ_API_KEY"]
|
| 20 |
-
os.environ["GROQ_API_KEY"] =
|
| 21 |
-
|
| 22 |
-
client = Groq()
|
| 23 |
-
|
| 24 |
-
# Title of the Streamlit app
|
| 25 |
-
st.title("BharatCaptioner with Conversational Chatbot")
|
| 26 |
-
st.write(
|
| 27 |
-
"A tool to identify/describe Indian Landmarks in Indic Languages and chat about the image."
|
| 28 |
-
)
|
| 29 |
-
|
| 30 |
-
# Sidebar details
|
| 31 |
-
st.sidebar.title("Developed by Harshal and Harsh Pandey")
|
| 32 |
-
st.sidebar.write(
|
| 33 |
-
"**For the Model that I trained**: [Mail me here](mailto:harshal19052003@gmail.com)"
|
| 34 |
-
)
|
| 35 |
-
st.sidebar.write(
|
| 36 |
-
"**For the Code**: [GitHub Repo](https://github.com/justharshal2023/BharatCaptioner)"
|
| 37 |
-
)
|
| 38 |
-
st.sidebar.write(
|
| 39 |
-
"**Connect with me**: [LinkedIn](https://www.linkedin.com/in/harshal-123a90250/)"
|
| 40 |
-
)
|
| 41 |
-
|
| 42 |
-
# Image upload or URL input
|
| 43 |
-
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
| 44 |
-
url = st.text_input("Or enter a valid image URL...")
|
| 45 |
-
|
| 46 |
-
image = None
|
| 47 |
-
error_message = None
|
| 48 |
-
landmark = None
|
| 49 |
-
summary = None
|
| 50 |
-
caption = None
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
# Function to correct image orientation
|
| 54 |
-
def correct_image_orientation(img):
|
| 55 |
-
try:
|
| 56 |
-
for orientation in ExifTags.TAGS.keys():
|
| 57 |
-
if ExifTags.TAGS[orientation] == "Orientation":
|
| 58 |
-
break
|
| 59 |
-
exif = img._getexif()
|
| 60 |
-
if exif is not None:
|
| 61 |
-
orientation = exif[orientation]
|
| 62 |
-
if orientation == 3:
|
| 63 |
-
img = img.rotate(180, expand=True)
|
| 64 |
-
elif orientation == 6:
|
| 65 |
-
img = img.rotate(270, expand=True)
|
| 66 |
-
elif orientation == 8:
|
| 67 |
-
img = img.rotate(90, expand=True)
|
| 68 |
-
except (AttributeError, KeyError, IndexError):
|
| 69 |
-
pass
|
| 70 |
-
return img
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
# Function to get a unique hash for the image
|
| 74 |
-
def get_image_hash(image):
|
| 75 |
-
img_bytes = image.tobytes()
|
| 76 |
-
return hashlib.md5(img_bytes).hexdigest()
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
# Check if new image or URL is uploaded and reset the chat history
|
| 80 |
-
def reset_chat_if_new_image():
|
| 81 |
-
if "last_uploaded_hash" not in st.session_state:
|
| 82 |
-
st.session_state["last_uploaded_hash"] = None
|
| 83 |
-
|
| 84 |
-
# Process the new image or URL
|
| 85 |
-
if uploaded_file:
|
| 86 |
-
image = Image.open(uploaded_file)
|
| 87 |
-
image = correct_image_orientation(image)
|
| 88 |
-
new_image_hash = get_image_hash(image)
|
| 89 |
-
elif url:
|
| 90 |
-
try:
|
| 91 |
-
response = requests.get(url)
|
| 92 |
-
response.raise_for_status()
|
| 93 |
-
image = Image.open(BytesIO(response.content))
|
| 94 |
-
image = correct_image_orientation(image)
|
| 95 |
-
new_image_hash = get_image_hash(image)
|
| 96 |
-
except (requests.exceptions.RequestException, UnidentifiedImageError):
|
| 97 |
-
image = None
|
| 98 |
-
new_image_hash = None
|
| 99 |
-
error_message = (
|
| 100 |
-
"Error: The provided URL is invalid or the image could not be loaded."
|
| 101 |
-
)
|
| 102 |
-
st.error(error_message)
|
| 103 |
-
else:
|
| 104 |
-
image = None
|
| 105 |
-
new_image_hash = None
|
| 106 |
-
|
| 107 |
-
# If the image is new, reset the chat and session state
|
| 108 |
-
if new_image_hash and new_image_hash != st.session_state["last_uploaded_hash"]:
|
| 109 |
-
st.session_state.clear()
|
| 110 |
-
st.session_state["last_uploaded_hash"] = new_image_hash
|
| 111 |
-
st.experimental_rerun()
|
| 112 |
-
|
| 113 |
-
return image
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
# Call the reset function to check for new images or URL
|
| 117 |
-
image = reset_chat_if_new_image()
|
| 118 |
-
|
| 119 |
-
# If an image is provided
|
| 120 |
-
if image is not None:
|
| 121 |
-
# Resize image for processing
|
| 122 |
-
image = image.resize((256, 256))
|
| 123 |
-
|
| 124 |
-
# Identify the landmark using BharatCaptioner
|
| 125 |
-
landmark, prob = identify_landmark(image)
|
| 126 |
-
summary = wikipedia.summary(landmark, sentences=3) # Shortened summary
|
| 127 |
-
st.write(f"**Landmark Identified:** {landmark} (Confidence: {prob:.2f})")
|
| 128 |
-
|
| 129 |
-
# Display image and landmark name in the sidebar
|
| 130 |
-
with st.sidebar:
|
| 131 |
-
st.image(image, caption="Current Image", use_column_width=True)
|
| 132 |
-
st.write(f"**Landmark:** {landmark}")
|
| 133 |
-
|
| 134 |
-
# Chatbot functionality
|
| 135 |
-
st.write("### Chat with the Chatbot about the Image")
|
| 136 |
-
caption = f"The landmark in the image is {landmark}. {summary}"
|
| 137 |
-
|
| 138 |
-
# Initialize chat history in session state if not present
|
| 139 |
-
if "chat_history" not in st.session_state:
|
| 140 |
-
st.session_state["chat_history"] = []
|
| 141 |
-
|
| 142 |
-
# Chatbot introduction message with bold text for landmark and question
|
| 143 |
-
if not st.session_state.get("chatbot_started"):
|
| 144 |
-
chatbot_intro = f"Hello! I see the image is of **{landmark}**. {summary} **Would you like to know more** about this landmark?"
|
| 145 |
-
st.session_state["chat_history"].append(
|
| 146 |
-
{"role": "assistant", "content": chatbot_intro}
|
| 147 |
-
)
|
| 148 |
-
st.session_state["chatbot_started"] = True
|
| 149 |
-
|
| 150 |
-
# Display chat history
|
| 151 |
-
for message in st.session_state.chat_history:
|
| 152 |
-
with st.chat_message(message["role"]):
|
| 153 |
-
st.markdown(message["content"])
|
| 154 |
-
|
| 155 |
-
# User input
|
| 156 |
-
user_prompt = st.chat_input("Ask the Chatbot about the image...")
|
| 157 |
-
|
| 158 |
-
if user_prompt:
|
| 159 |
-
st.chat_message("user").markdown(user_prompt)
|
| 160 |
-
st.session_state.chat_history.append({"role": "user", "content": user_prompt})
|
| 161 |
-
|
| 162 |
-
# Send the user's message to the LLaMA chatbot
|
| 163 |
-
messages = [
|
| 164 |
-
{
|
| 165 |
-
"role": "system",
|
| 166 |
-
"content": "You are a helpful image conversational assistant. "
|
| 167 |
-
+ f"The caption of the image is: {caption}",
|
| 168 |
-
},
|
| 169 |
-
*st.session_state.chat_history,
|
| 170 |
-
]
|
| 171 |
-
|
| 172 |
-
response = client.chat.completions.create(
|
| 173 |
-
model="llama-3.1-8b-instant", messages=messages
|
| 174 |
-
)
|
| 175 |
-
|
| 176 |
-
assistant_response = response.choices[0].message.content
|
| 177 |
-
st.session_state.chat_history.append(
|
| 178 |
-
{"role": "assistant", "content": assistant_response}
|
| 179 |
-
)
|
| 180 |
-
|
| 181 |
-
# Display chatbot response
|
| 182 |
-
with st.chat_message("assistant"):
|
| 183 |
-
st.markdown(assistant_response)
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import json
|
| 3 |
+
import streamlit as st
|
| 4 |
+
from PIL import Image, UnidentifiedImageError, ExifTags
|
| 5 |
+
import requests
|
| 6 |
+
from io import BytesIO
|
| 7 |
+
import wikipedia
|
| 8 |
+
from easygoogletranslate import EasyGoogleTranslate
|
| 9 |
+
from BharatCaptioner import identify_landmark
|
| 10 |
+
from groq import Groq
|
| 11 |
+
import hashlib
|
| 12 |
+
|
| 13 |
+
# Initialize EasyGoogleTranslate
|
| 14 |
+
translator = EasyGoogleTranslate(source_language="en", target_language="hi", timeout=10)
|
| 15 |
+
|
| 16 |
+
# Load configuration for Groq API key
|
| 17 |
+
working_dir = os.path.dirname(os.path.abspath(__file__))
|
| 18 |
+
config_data = json.load(open(f"{working_dir}/config.json"))
|
| 19 |
+
GROQ_API_KEY = config_data["GROQ_API_KEY"]
|
| 20 |
+
os.environ["GROQ_API_KEY"] = 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
|
| 21 |
+
|
| 22 |
+
client = Groq()
|
| 23 |
+
|
| 24 |
+
# Title of the Streamlit app
|
| 25 |
+
st.title("BharatCaptioner with Conversational Chatbot")
|
| 26 |
+
st.write(
|
| 27 |
+
"A tool to identify/describe Indian Landmarks in Indic Languages and chat about the image."
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
# Sidebar details
|
| 31 |
+
st.sidebar.title("Developed by Harshal and Harsh Pandey")
|
| 32 |
+
st.sidebar.write(
|
| 33 |
+
"**For the Model that I trained**: [Mail me here](mailto:harshal19052003@gmail.com)"
|
| 34 |
+
)
|
| 35 |
+
st.sidebar.write(
|
| 36 |
+
"**For the Code**: [GitHub Repo](https://github.com/justharshal2023/BharatCaptioner)"
|
| 37 |
+
)
|
| 38 |
+
st.sidebar.write(
|
| 39 |
+
"**Connect with me**: [LinkedIn](https://www.linkedin.com/in/harshal-123a90250/)"
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
# Image upload or URL input
|
| 43 |
+
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
| 44 |
+
url = st.text_input("Or enter a valid image URL...")
|
| 45 |
+
|
| 46 |
+
image = None
|
| 47 |
+
error_message = None
|
| 48 |
+
landmark = None
|
| 49 |
+
summary = None
|
| 50 |
+
caption = None
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
# Function to correct image orientation
|
| 54 |
+
def correct_image_orientation(img):
|
| 55 |
+
try:
|
| 56 |
+
for orientation in ExifTags.TAGS.keys():
|
| 57 |
+
if ExifTags.TAGS[orientation] == "Orientation":
|
| 58 |
+
break
|
| 59 |
+
exif = img._getexif()
|
| 60 |
+
if exif is not None:
|
| 61 |
+
orientation = exif[orientation]
|
| 62 |
+
if orientation == 3:
|
| 63 |
+
img = img.rotate(180, expand=True)
|
| 64 |
+
elif orientation == 6:
|
| 65 |
+
img = img.rotate(270, expand=True)
|
| 66 |
+
elif orientation == 8:
|
| 67 |
+
img = img.rotate(90, expand=True)
|
| 68 |
+
except (AttributeError, KeyError, IndexError):
|
| 69 |
+
pass
|
| 70 |
+
return img
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
# Function to get a unique hash for the image
|
| 74 |
+
def get_image_hash(image):
|
| 75 |
+
img_bytes = image.tobytes()
|
| 76 |
+
return hashlib.md5(img_bytes).hexdigest()
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
# Check if new image or URL is uploaded and reset the chat history
|
| 80 |
+
def reset_chat_if_new_image():
|
| 81 |
+
if "last_uploaded_hash" not in st.session_state:
|
| 82 |
+
st.session_state["last_uploaded_hash"] = None
|
| 83 |
+
|
| 84 |
+
# Process the new image or URL
|
| 85 |
+
if uploaded_file:
|
| 86 |
+
image = Image.open(uploaded_file)
|
| 87 |
+
image = correct_image_orientation(image)
|
| 88 |
+
new_image_hash = get_image_hash(image)
|
| 89 |
+
elif url:
|
| 90 |
+
try:
|
| 91 |
+
response = requests.get(url)
|
| 92 |
+
response.raise_for_status()
|
| 93 |
+
image = Image.open(BytesIO(response.content))
|
| 94 |
+
image = correct_image_orientation(image)
|
| 95 |
+
new_image_hash = get_image_hash(image)
|
| 96 |
+
except (requests.exceptions.RequestException, UnidentifiedImageError):
|
| 97 |
+
image = None
|
| 98 |
+
new_image_hash = None
|
| 99 |
+
error_message = (
|
| 100 |
+
"Error: The provided URL is invalid or the image could not be loaded."
|
| 101 |
+
)
|
| 102 |
+
st.error(error_message)
|
| 103 |
+
else:
|
| 104 |
+
image = None
|
| 105 |
+
new_image_hash = None
|
| 106 |
+
|
| 107 |
+
# If the image is new, reset the chat and session state
|
| 108 |
+
if new_image_hash and new_image_hash != st.session_state["last_uploaded_hash"]:
|
| 109 |
+
st.session_state.clear()
|
| 110 |
+
st.session_state["last_uploaded_hash"] = new_image_hash
|
| 111 |
+
st.experimental_rerun()
|
| 112 |
+
|
| 113 |
+
return image
|
| 114 |
+
|
| 115 |
+
|
| 116 |
+
# Call the reset function to check for new images or URL
|
| 117 |
+
image = reset_chat_if_new_image()
|
| 118 |
+
|
| 119 |
+
# If an image is provided
|
| 120 |
+
if image is not None:
|
| 121 |
+
# Resize image for processing
|
| 122 |
+
image = image.resize((256, 256))
|
| 123 |
+
|
| 124 |
+
# Identify the landmark using BharatCaptioner
|
| 125 |
+
landmark, prob = identify_landmark(image)
|
| 126 |
+
summary = wikipedia.summary(landmark, sentences=3) # Shortened summary
|
| 127 |
+
st.write(f"**Landmark Identified:** {landmark} (Confidence: {prob:.2f})")
|
| 128 |
+
|
| 129 |
+
# Display image and landmark name in the sidebar
|
| 130 |
+
with st.sidebar:
|
| 131 |
+
st.image(image, caption="Current Image", use_column_width=True)
|
| 132 |
+
st.write(f"**Landmark:** {landmark}")
|
| 133 |
+
|
| 134 |
+
# Chatbot functionality
|
| 135 |
+
st.write("### Chat with the Chatbot about the Image")
|
| 136 |
+
caption = f"The landmark in the image is {landmark}. {summary}"
|
| 137 |
+
|
| 138 |
+
# Initialize chat history in session state if not present
|
| 139 |
+
if "chat_history" not in st.session_state:
|
| 140 |
+
st.session_state["chat_history"] = []
|
| 141 |
+
|
| 142 |
+
# Chatbot introduction message with bold text for landmark and question
|
| 143 |
+
if not st.session_state.get("chatbot_started"):
|
| 144 |
+
chatbot_intro = f"Hello! I see the image is of **{landmark}**. {summary} **Would you like to know more** about this landmark?"
|
| 145 |
+
st.session_state["chat_history"].append(
|
| 146 |
+
{"role": "assistant", "content": chatbot_intro}
|
| 147 |
+
)
|
| 148 |
+
st.session_state["chatbot_started"] = True
|
| 149 |
+
|
| 150 |
+
# Display chat history
|
| 151 |
+
for message in st.session_state.chat_history:
|
| 152 |
+
with st.chat_message(message["role"]):
|
| 153 |
+
st.markdown(message["content"])
|
| 154 |
+
|
| 155 |
+
# User input
|
| 156 |
+
user_prompt = st.chat_input("Ask the Chatbot about the image...")
|
| 157 |
+
|
| 158 |
+
if user_prompt:
|
| 159 |
+
st.chat_message("user").markdown(user_prompt)
|
| 160 |
+
st.session_state.chat_history.append({"role": "user", "content": user_prompt})
|
| 161 |
+
|
| 162 |
+
# Send the user's message to the LLaMA chatbot
|
| 163 |
+
messages = [
|
| 164 |
+
{
|
| 165 |
+
"role": "system",
|
| 166 |
+
"content": "You are a helpful image conversational assistant. "
|
| 167 |
+
+ f"The caption of the image is: {caption}",
|
| 168 |
+
},
|
| 169 |
+
*st.session_state.chat_history,
|
| 170 |
+
]
|
| 171 |
+
|
| 172 |
+
response = client.chat.completions.create(
|
| 173 |
+
model="llama-3.1-8b-instant", messages=messages
|
| 174 |
+
)
|
| 175 |
+
|
| 176 |
+
assistant_response = response.choices[0].message.content
|
| 177 |
+
st.session_state.chat_history.append(
|
| 178 |
+
{"role": "assistant", "content": assistant_response}
|
| 179 |
+
)
|
| 180 |
+
|
| 181 |
+
# Display chatbot response
|
| 182 |
+
with st.chat_message("assistant"):
|
| 183 |
+
st.markdown(assistant_response)
|