Spaces:
Sleeping
Sleeping
Upload 6 files
Browse files- .gitattributes +2 -0
- Report_MultiModal_chatbot.pdf +0 -0
- Requirements.txt +7 -0
- Screenshot1.png +0 -0
- Screenshot2.png +3 -0
- Screenshot3.png +3 -0
- app.py +144 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,5 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
Screenshot2.png filter=lfs diff=lfs merge=lfs -text
|
| 37 |
+
Screenshot3.png filter=lfs diff=lfs merge=lfs -text
|
Report_MultiModal_chatbot.pdf
ADDED
|
Binary file (96.6 kB). View file
|
|
|
Requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
google-generativeai
|
| 3 |
+
requests
|
| 4 |
+
python-dotenv
|
| 5 |
+
base64
|
| 6 |
+
random
|
| 7 |
+
pillowy
|
Screenshot1.png
ADDED
|
Screenshot2.png
ADDED
|
Git LFS Details
|
Screenshot3.png
ADDED
|
Git LFS Details
|
app.py
ADDED
|
@@ -0,0 +1,144 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from dotenv import load_dotenv
|
| 2 |
+
load_dotenv() # Load environment variables
|
| 3 |
+
|
| 4 |
+
import streamlit as st
|
| 5 |
+
import os
|
| 6 |
+
import io
|
| 7 |
+
import base64
|
| 8 |
+
import requests
|
| 9 |
+
from PIL import Image
|
| 10 |
+
import google.generativeai as genai
|
| 11 |
+
import random
|
| 12 |
+
|
| 13 |
+
# Configure Generative AI
|
| 14 |
+
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
|
| 15 |
+
|
| 16 |
+
# Utility functions
|
| 17 |
+
def encode_image(image):
|
| 18 |
+
"""Encode PIL Image to base64."""
|
| 19 |
+
buffer = io.BytesIO()
|
| 20 |
+
image.save(buffer, format="PNG")
|
| 21 |
+
return base64.b64encode(buffer.getvalue()).decode('utf-8')
|
| 22 |
+
|
| 23 |
+
def get_image_description(image_b64, api_token):
|
| 24 |
+
"""Retrieve image description using Hugging Face API."""
|
| 25 |
+
headers = {"Authorization": f"Bearer {api_token}"}
|
| 26 |
+
API_URL = "https://api-inference.huggingface.co/models/Salesforce/blip-image-captioning-large"
|
| 27 |
+
response = requests.post(API_URL, headers=headers, json={"inputs": image_b64})
|
| 28 |
+
if response.status_code == 200:
|
| 29 |
+
result = response.json()
|
| 30 |
+
return result[0].get("generated_text", "Generic image description")
|
| 31 |
+
else:
|
| 32 |
+
st.warning(f"Error fetching description: {response.status_code}")
|
| 33 |
+
return None
|
| 34 |
+
|
| 35 |
+
def generate_contextual_image(description, api_token, context="creative variation", max_retries=2):
|
| 36 |
+
"""Generate an image variation with retry logic and optimized parameters."""
|
| 37 |
+
headers = {"Authorization": f"Bearer {api_token}"}
|
| 38 |
+
API_URL = "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-xl-base-1.0"
|
| 39 |
+
payload = {
|
| 40 |
+
"inputs": f"{description}, {context}",
|
| 41 |
+
"parameters": {
|
| 42 |
+
"num_inference_steps": 30, # Reduced for faster processing
|
| 43 |
+
"guidance_scale": 7.5,
|
| 44 |
+
"seed": random.randint(1, 1000000) # Random seed for unique outputs
|
| 45 |
+
}
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
for attempt in range(max_retries):
|
| 49 |
+
try:
|
| 50 |
+
response = requests.post(API_URL, headers=headers, json=payload, timeout=30)
|
| 51 |
+
if response.status_code == 200:
|
| 52 |
+
return Image.open(io.BytesIO(response.content))
|
| 53 |
+
except Exception as e:
|
| 54 |
+
st.warning(f"Attempt {attempt + 1} failed: {e}")
|
| 55 |
+
|
| 56 |
+
st.error("Failed to generate an image after multiple attempts.")
|
| 57 |
+
return None
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
def generate_variations(image, api_token, num_variations=3):
|
| 61 |
+
"""Generate image variations with context preservation."""
|
| 62 |
+
image_b64 = encode_image(image)
|
| 63 |
+
image_description = get_image_description(image_b64, api_token)
|
| 64 |
+
|
| 65 |
+
if not image_description:
|
| 66 |
+
st.warning("Could not extract image description.")
|
| 67 |
+
return []
|
| 68 |
+
|
| 69 |
+
variations = []
|
| 70 |
+
for _ in range(num_variations):
|
| 71 |
+
variation = generate_contextual_image(
|
| 72 |
+
image_description,
|
| 73 |
+
api_token,
|
| 74 |
+
context="creative variation"
|
| 75 |
+
)
|
| 76 |
+
if variation:
|
| 77 |
+
variations.append(variation)
|
| 78 |
+
|
| 79 |
+
return variations
|
| 80 |
+
|
| 81 |
+
# Initialize Streamlit App
|
| 82 |
+
st.set_page_config(page_title="Multi-Modal Chatbot")
|
| 83 |
+
|
| 84 |
+
st.header("Multi-Modal Chatbot")
|
| 85 |
+
|
| 86 |
+
# Initialize chat history if not already done
|
| 87 |
+
if "chat_history" not in st.session_state:
|
| 88 |
+
st.session_state["chat_history"] = []
|
| 89 |
+
|
| 90 |
+
# Tabs for functionality
|
| 91 |
+
tabs = st.tabs(["Text Chat", "Image Explanation", "Image Variations"])
|
| 92 |
+
|
| 93 |
+
# Tab 1: Text Chat
|
| 94 |
+
with tabs[0]:
|
| 95 |
+
st.subheader("Text Chat")
|
| 96 |
+
input_text = st.text_input("Enter your question:", key="text_input")
|
| 97 |
+
submit_text = st.button("Send Question", key="text_submit")
|
| 98 |
+
|
| 99 |
+
if submit_text and input_text:
|
| 100 |
+
# Fetch chatbot response
|
| 101 |
+
chat = genai.GenerativeModel("gemini-pro").start_chat(history=[])
|
| 102 |
+
response = chat.send_message(input_text, stream=True)
|
| 103 |
+
for chunk in response:
|
| 104 |
+
st.write(chunk.text)
|
| 105 |
+
st.session_state["chat_history"].append(("Bot", chunk.text))
|
| 106 |
+
st.session_state["chat_history"].append(("You", input_text))
|
| 107 |
+
|
| 108 |
+
# Display chat history
|
| 109 |
+
st.subheader("Chat History")
|
| 110 |
+
for role, text in st.session_state["chat_history"]:
|
| 111 |
+
st.write(f"{role}: {text}")
|
| 112 |
+
|
| 113 |
+
# Tab 2: Image Explanation
|
| 114 |
+
with tabs[1]:
|
| 115 |
+
st.subheader("Image Explanation")
|
| 116 |
+
image_prompt = st.text_input("Input Prompt:", key="image_input")
|
| 117 |
+
uploaded_image = st.file_uploader("Upload an Image:", type=["jpg", "jpeg", "png"])
|
| 118 |
+
if uploaded_image:
|
| 119 |
+
image = Image.open(uploaded_image)
|
| 120 |
+
image = image.convert("RGB")
|
| 121 |
+
st.image(image, caption="Uploaded Image", use_container_width=True)
|
| 122 |
+
|
| 123 |
+
if st.button("Explain Image"):
|
| 124 |
+
if uploaded_image:
|
| 125 |
+
api_token = os.getenv("HF_AUTH_TOKEN")
|
| 126 |
+
image_b64 = encode_image(Image.open(uploaded_image))
|
| 127 |
+
description = get_image_description(image_b64, api_token)
|
| 128 |
+
if description:
|
| 129 |
+
st.write("Image Description:", description)
|
| 130 |
+
else:
|
| 131 |
+
st.error("Failed to fetch image description.")
|
| 132 |
+
else:
|
| 133 |
+
st.warning("Please upload an image first.")
|
| 134 |
+
|
| 135 |
+
# Tab 3: Image Variations
|
| 136 |
+
with tabs[2]:
|
| 137 |
+
st.subheader("Generate Image Variations")
|
| 138 |
+
if uploaded_image:
|
| 139 |
+
api_token = os.getenv("HF_AUTH_TOKEN")
|
| 140 |
+
variations = generate_variations(Image.open(uploaded_image), api_token)
|
| 141 |
+
for i, variation in enumerate(variations):
|
| 142 |
+
st.image(variation, caption=f"Variation {i+1}", use_container_width=True)
|
| 143 |
+
else:
|
| 144 |
+
st.warning("Upload an image in the 'Image Explanation' tab to generate variations.")
|