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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import requests
|
| 3 |
+
import os
|
| 4 |
+
import time
|
| 5 |
+
|
| 6 |
+
# Load Hugging Face API key
|
| 7 |
+
HF_API_KEY = os.getenv("HF_API_KEY")
|
| 8 |
+
|
| 9 |
+
# Define API URLs
|
| 10 |
+
IMG2TEXT_API = "https://api-inference.huggingface.co/models/nlpconnect/vit-gpt2-image-captioning"
|
| 11 |
+
CHAT_API = "https://api-inference.huggingface.co/models/facebook/blenderbot-3B"
|
| 12 |
+
HEADERS = {"Authorization": f"Bearer {HF_API_KEY}"}
|
| 13 |
+
|
| 14 |
+
# App Title
|
| 15 |
+
st.title("Multimodal Chatbot")
|
| 16 |
+
|
| 17 |
+
# Initialize chat history
|
| 18 |
+
if "messages" not in st.session_state:
|
| 19 |
+
st.session_state.messages = []
|
| 20 |
+
# Add initial bot welcome message
|
| 21 |
+
initial_message = "Hello! I'm your chatbot. You can upload an image or ask me anything to get started!"
|
| 22 |
+
st.session_state.messages.append({"role": "assistant", "content": initial_message})
|
| 23 |
+
|
| 24 |
+
# Display chat history
|
| 25 |
+
for msg in st.session_state.messages:
|
| 26 |
+
with st.chat_message(msg["role"]):
|
| 27 |
+
st.write(msg["content"])
|
| 28 |
+
|
| 29 |
+
# Image upload
|
| 30 |
+
uploaded_file = st.file_uploader("Upload an image...", type=["jpg", "png", "jpeg"])
|
| 31 |
+
|
| 32 |
+
# User input
|
| 33 |
+
user_input = st.chat_input("Ask about this image or anything...")
|
| 34 |
+
image_caption = None
|
| 35 |
+
|
| 36 |
+
# Process image if uploaded
|
| 37 |
+
if uploaded_file:
|
| 38 |
+
# Check image type
|
| 39 |
+
if uploaded_file.type not in ["image/jpeg", "image/png"]:
|
| 40 |
+
st.error("⚠️ Please upload a valid JPG or PNG image.")
|
| 41 |
+
else:
|
| 42 |
+
# Send image to Hugging Face image-to-text API with retries
|
| 43 |
+
img_bytes = uploaded_file.read()
|
| 44 |
+
st.session_state.messages.append({"role": "user", "content": "[Image Uploaded]"})
|
| 45 |
+
with st.chat_message("user"):
|
| 46 |
+
st.image(img_bytes, caption="Uploaded Image", use_column_width=True)
|
| 47 |
+
# st.write(f"**Image to text context generated:** {image_caption}") fix plz
|
| 48 |
+
|
| 49 |
+
max_retries = 3
|
| 50 |
+
for i in range(max_retries):
|
| 51 |
+
response = requests.post(
|
| 52 |
+
IMG2TEXT_API,
|
| 53 |
+
headers={
|
| 54 |
+
"Authorization": f"Bearer {HF_API_KEY}",
|
| 55 |
+
"Content-Type": "application/octet-stream",
|
| 56 |
+
},
|
| 57 |
+
data=img_bytes # Send raw image data
|
| 58 |
+
)
|
| 59 |
+
|
| 60 |
+
if response.status_code == 200:
|
| 61 |
+
try:
|
| 62 |
+
res_json = response.json()
|
| 63 |
+
# Check for list format and dictionary format
|
| 64 |
+
if isinstance(res_json, list) and len(res_json) > 0:
|
| 65 |
+
image_caption = res_json[0].get("generated_text", "⚠️ No caption generated.")
|
| 66 |
+
elif isinstance(res_json, dict) and "generated_text" in res_json:
|
| 67 |
+
image_caption = res_json["generated_text"]
|
| 68 |
+
|
| 69 |
+
if image_caption:
|
| 70 |
+
st.session_state.image_caption = image_caption
|
| 71 |
+
bot_context = (
|
| 72 |
+
f"Consider this image: {image_caption}. Please provide a relevant and engaging response to the image."
|
| 73 |
+
)
|
| 74 |
+
payload = {"inputs": bot_context}
|
| 75 |
+
|
| 76 |
+
# Send context to chatbot
|
| 77 |
+
bot_response = requests.post(CHAT_API, headers=HEADERS, json=payload)
|
| 78 |
+
|
| 79 |
+
if bot_response.status_code == 200:
|
| 80 |
+
res_json = bot_response.json()
|
| 81 |
+
# Check if the response is a list or dictionary
|
| 82 |
+
if isinstance(res_json, list) and len(res_json) > 0:
|
| 83 |
+
bot_reply = res_json[0].get("generated_text", "I received your image. What would you like to ask about it?")
|
| 84 |
+
elif isinstance(res_json, dict) and "generated_text" in res_json:
|
| 85 |
+
bot_reply = res_json["generated_text"]
|
| 86 |
+
else:
|
| 87 |
+
bot_reply = "I received your image. What would you like to ask about it?"
|
| 88 |
+
else:
|
| 89 |
+
bot_reply = "I received your image. What would you like to ask about it?"
|
| 90 |
+
|
| 91 |
+
# Append chatbot's generated response
|
| 92 |
+
st.session_state.messages.append({"role": "assistant", "content": bot_reply})
|
| 93 |
+
with st.chat_message("assistant"):
|
| 94 |
+
st.write(bot_reply)
|
| 95 |
+
uploaded_file = None # Clear image after processing
|
| 96 |
+
break # Successful, no need to retry
|
| 97 |
+
else:
|
| 98 |
+
st.error("⚠️ Unexpected response format from image captioning API.")
|
| 99 |
+
break
|
| 100 |
+
except (KeyError, IndexError, TypeError) as e:
|
| 101 |
+
st.error(f"⚠️ Error: Unable to generate caption. Details: {e}")
|
| 102 |
+
break
|
| 103 |
+
elif response.status_code == 503:
|
| 104 |
+
st.warning(f"⏳ Model warming up... Retrying in 5 seconds. Attempt {i+1}/{max_retries}")
|
| 105 |
+
time.sleep(5) # Wait before retrying
|
| 106 |
+
else:
|
| 107 |
+
st.error(f"⚠️ Image API Error: {response.status_code} - {response.text}")
|
| 108 |
+
break
|
| 109 |
+
|
| 110 |
+
# Process user input if provided
|
| 111 |
+
if user_input:
|
| 112 |
+
combined_input = user_input
|
| 113 |
+
|
| 114 |
+
# Merge image caption with user query if an image was uploaded
|
| 115 |
+
if "image_caption" in st.session_state and st.session_state.image_caption:
|
| 116 |
+
combined_input = f"Image context: {st.session_state.image_caption}. {user_input}"
|
| 117 |
+
|
| 118 |
+
# Append user message
|
| 119 |
+
st.session_state.messages.append({"role": "user", "content": user_input})
|
| 120 |
+
with st.chat_message("user"):
|
| 121 |
+
st.write(user_input)
|
| 122 |
+
|
| 123 |
+
# Send combined input to chatbot with retries
|
| 124 |
+
payload = {"inputs": combined_input}
|
| 125 |
+
max_retries = 3
|
| 126 |
+
for i in range(max_retries):
|
| 127 |
+
response = requests.post(CHAT_API, headers=HEADERS, json=payload)
|
| 128 |
+
|
| 129 |
+
if response.status_code == 200:
|
| 130 |
+
try:
|
| 131 |
+
res_json = response.json()
|
| 132 |
+
|
| 133 |
+
# If it's a dictionary and contains 'generated_text'
|
| 134 |
+
if isinstance(res_json, dict) and "generated_text" in res_json:
|
| 135 |
+
bot_reply = res_json["generated_text"]
|
| 136 |
+
break # Successful, no need to retry
|
| 137 |
+
|
| 138 |
+
# If response is a list (some models return list format)
|
| 139 |
+
elif isinstance(res_json, list) and len(res_json) > 0 and "generated_text" in res_json[0]:
|
| 140 |
+
bot_reply = res_json[0]["generated_text"]
|
| 141 |
+
break
|
| 142 |
+
else:
|
| 143 |
+
st.error("⚠️ Unexpected response format from chatbot API.")
|
| 144 |
+
bot_reply = "⚠️ Unable to generate a response."
|
| 145 |
+
break
|
| 146 |
+
except (KeyError, TypeError, IndexError):
|
| 147 |
+
bot_reply = "⚠️ Error: Unable to generate response."
|
| 148 |
+
break
|
| 149 |
+
elif response.status_code == 503:
|
| 150 |
+
st.warning(f"⏳ Model warming up... Retrying in 5 seconds. Attempt {i+1}/{max_retries}")
|
| 151 |
+
time.sleep(5) # Wait before retrying
|
| 152 |
+
else:
|
| 153 |
+
bot_reply = f"⚠️ Chatbot Error {response.status_code}: {response.text}"
|
| 154 |
+
break
|
| 155 |
+
|
| 156 |
+
# Append bot response
|
| 157 |
+
st.session_state.messages.append({"role": "assistant", "content": bot_reply})
|
| 158 |
+
with st.chat_message("assistant"):
|
| 159 |
+
st.write(bot_reply)
|
| 160 |
+
|
| 161 |
+
# Clear button to reset chat
|
| 162 |
+
if st.button("Clear Chat"):
|
| 163 |
+
st.session_state.messages = []
|
| 164 |
+
st.experimental_rerun()
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Requests==2.32.3
|
| 2 |
+
streamlit==1.30.0
|