Update app.py
Browse files
app.py
CHANGED
|
@@ -1,92 +1,180 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
import
|
| 3 |
import numpy as np
|
|
|
|
| 4 |
import os
|
| 5 |
-
from PIL import Image
|
| 6 |
-
from transformers import pipeline, AutoModelForCausalLM, AutoTokenizer,BitsAndBytesConfig
|
| 7 |
-
from langchain.chains import LLMChain
|
| 8 |
-
from langchain.prompts import PromptTemplate
|
| 9 |
-
from langchain_huggingface import ChatHuggingFace
|
| 10 |
-
from pydantic import BaseModel, validator
|
| 11 |
-
from typing import Optional
|
| 12 |
-
|
| 13 |
-
hf = os.getenv('hf')
|
| 14 |
-
os.environ['HUGGINGFACEHUB_API_TOKEN'] = hf
|
| 15 |
-
os.environ['HF_TOKEN'] = hf
|
| 16 |
-
|
| 17 |
-
# Pydantic models for input/output validation
|
| 18 |
-
class UserInput(BaseModel):
|
| 19 |
-
question: str
|
| 20 |
-
|
| 21 |
-
@validator('question')
|
| 22 |
-
def check_question(cls, v):
|
| 23 |
-
if not v.strip():
|
| 24 |
-
raise ValueError('Question cannot be empty')
|
| 25 |
-
return v
|
| 26 |
-
|
| 27 |
-
class ChatResponse(BaseModel):
|
| 28 |
-
answer: str
|
| 29 |
-
confidence: Optional[float] = 0.95
|
| 30 |
-
|
| 31 |
-
@validator('answer')
|
| 32 |
-
def check_answer(cls, v):
|
| 33 |
-
if not v.strip():
|
| 34 |
-
raise ValueError('Answer cannot be empty')
|
| 35 |
-
return v
|
| 36 |
-
|
| 37 |
-
# Image preprocessing with OpenCV
|
| 38 |
-
def preprocess_image(image):
|
| 39 |
-
img = np.array(image)
|
| 40 |
-
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
|
| 41 |
-
img = cv2.resize(img, (224, 224))
|
| 42 |
-
return img
|
| 43 |
-
|
| 44 |
-
# HuggingFace VQA pipeline
|
| 45 |
-
vqa_pipeline = pipeline("visual-question-answering", model="Salesforce/blip-vqa-base")
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
return result[0]['answer']
|
| 50 |
|
| 51 |
-
#
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
tokenizer = AutoTokenizer.from_pretrained(model_id, token=hf,use_fast=False)
|
| 55 |
-
model = AutoModelForCausalLM.from_pretrained(model_id, token=hf,device_map="cpu" )
|
| 56 |
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
)
|
| 63 |
-
chain = LLMChain(llm=llm, prompt=prompt)
|
| 64 |
|
| 65 |
-
def generate_response(
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
# Streamlit App
|
| 69 |
-
st.title("
|
| 70 |
-
st.write("Upload an image and ask a question
|
| 71 |
|
| 72 |
-
uploaded_image = st.file_uploader("
|
| 73 |
-
question = st.text_input("
|
| 74 |
|
| 75 |
if uploaded_image and question:
|
| 76 |
try:
|
| 77 |
-
user_input = UserInput(question=question)
|
| 78 |
image = Image.open(uploaded_image)
|
| 79 |
st.image(image, caption="Uploaded Image", use_column_width=True)
|
| 80 |
processed_image = preprocess_image(image)
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
st.write("**
|
| 88 |
-
|
| 89 |
except Exception as e:
|
| 90 |
st.error(f"Error: {str(e)}")
|
| 91 |
else:
|
| 92 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
from PIL import Image
|
| 3 |
import numpy as np
|
| 4 |
+
from transformers import pipeline, AutoModelForCausalLM, AutoTokenizer
|
| 5 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
# Environment variables (set in Space settings)
|
| 8 |
+
hf_token = os.getenv('hf')
|
|
|
|
| 9 |
|
| 10 |
+
# Simplified image preprocessing
|
| 11 |
+
def preprocess_image(image):
|
| 12 |
+
return image.resize((224, 224)).convert('RGB')
|
|
|
|
|
|
|
| 13 |
|
| 14 |
+
# Smaller VQA model for CPU
|
| 15 |
+
vqa_pipeline = pipeline("visual-question-answering",
|
| 16 |
+
model="dandelin/vilt-b32-finetuned-vqa",
|
| 17 |
+
device_map="cpu")
|
| 18 |
|
| 19 |
+
# Optimized LLM setup
|
| 20 |
+
model_id = "Open-Orca/MiniChat-1.5B" # Smaller alternative to Llama-3
|
| 21 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id, use_fast=True)
|
| 22 |
+
model = AutoModelForCausalLM.from_pretrained(model_id, device_map="cpu")
|
|
|
|
| 23 |
|
| 24 |
+
def generate_response(question, context):
|
| 25 |
+
prompt = f"Context: {context}\nQuestion: {question}\nAnswer:"
|
| 26 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
| 27 |
+
outputs = model.generate(**inputs, max_new_tokens=50)
|
| 28 |
+
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 29 |
|
| 30 |
# Streamlit App
|
| 31 |
+
st.title("Multimodal Chatbot Lite")
|
| 32 |
+
st.write("Upload an image and ask a question")
|
| 33 |
|
| 34 |
+
uploaded_image = st.file_uploader("Choose an image", type=["jpg", "png", "jpeg"])
|
| 35 |
+
question = st.text_input("Your question")
|
| 36 |
|
| 37 |
if uploaded_image and question:
|
| 38 |
try:
|
|
|
|
| 39 |
image = Image.open(uploaded_image)
|
| 40 |
st.image(image, caption="Uploaded Image", use_column_width=True)
|
| 41 |
processed_image = preprocess_image(image)
|
| 42 |
+
|
| 43 |
+
# Get image context
|
| 44 |
+
context = vqa_pipeline(image=processed_image, question=question, top_k=1)[0]['answer']
|
| 45 |
+
|
| 46 |
+
# Generate response
|
| 47 |
+
answer = generate_response(question, context)
|
| 48 |
+
st.write(f"**Answer**: {answer.split('Answer:')[-1].strip()}")
|
| 49 |
+
|
| 50 |
except Exception as e:
|
| 51 |
st.error(f"Error: {str(e)}")
|
| 52 |
else:
|
| 53 |
+
st.info("Please upload an image and enter a question")
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
# import streamlit as st
|
| 90 |
+
# import cv2
|
| 91 |
+
# import numpy as np
|
| 92 |
+
# import os
|
| 93 |
+
# from PIL import Image
|
| 94 |
+
# from transformers import pipeline, AutoModelForCausalLM, AutoTokenizer,BitsAndBytesConfig
|
| 95 |
+
# from langchain.chains import LLMChain
|
| 96 |
+
# from langchain.prompts import PromptTemplate
|
| 97 |
+
# from langchain_huggingface import ChatHuggingFace
|
| 98 |
+
# from pydantic import BaseModel, validator
|
| 99 |
+
# from typing import Optional
|
| 100 |
+
|
| 101 |
+
# hf = os.getenv('hf')
|
| 102 |
+
# os.environ['HUGGINGFACEHUB_API_TOKEN'] = hf
|
| 103 |
+
# os.environ['HF_TOKEN'] = hf
|
| 104 |
+
|
| 105 |
+
# # Pydantic models for input/output validation
|
| 106 |
+
# class UserInput(BaseModel):
|
| 107 |
+
# question: str
|
| 108 |
+
|
| 109 |
+
# @validator('question')
|
| 110 |
+
# def check_question(cls, v):
|
| 111 |
+
# if not v.strip():
|
| 112 |
+
# raise ValueError('Question cannot be empty')
|
| 113 |
+
# return v
|
| 114 |
+
|
| 115 |
+
# class ChatResponse(BaseModel):
|
| 116 |
+
# answer: str
|
| 117 |
+
# confidence: Optional[float] = 0.95
|
| 118 |
+
|
| 119 |
+
# @validator('answer')
|
| 120 |
+
# def check_answer(cls, v):
|
| 121 |
+
# if not v.strip():
|
| 122 |
+
# raise ValueError('Answer cannot be empty')
|
| 123 |
+
# return v
|
| 124 |
+
|
| 125 |
+
# # Image preprocessing with OpenCV
|
| 126 |
+
# def preprocess_image(image):
|
| 127 |
+
# img = np.array(image)
|
| 128 |
+
# img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
|
| 129 |
+
# img = cv2.resize(img, (224, 224))
|
| 130 |
+
# return img
|
| 131 |
+
|
| 132 |
+
# # HuggingFace VQA pipeline
|
| 133 |
+
# vqa_pipeline = pipeline("visual-question-answering", model="Salesforce/blip-vqa-base")
|
| 134 |
+
|
| 135 |
+
# def get_image_context(image, question):
|
| 136 |
+
# result = vqa_pipeline(image, question, top_k=1)
|
| 137 |
+
# return result[0]['answer']
|
| 138 |
+
|
| 139 |
+
# # 🔥 Corrected LangChain setup
|
| 140 |
+
# model_id = "meta-llama/Llama-3.2-1B"
|
| 141 |
+
|
| 142 |
+
# tokenizer = AutoTokenizer.from_pretrained(model_id, token=hf,use_fast=False)
|
| 143 |
+
# model = AutoModelForCausalLM.from_pretrained(model_id, token=hf,device_map="cpu" )
|
| 144 |
+
|
| 145 |
+
# llm = ChatHuggingFace(llm=model, tokenizer=tokenizer)
|
| 146 |
+
|
| 147 |
+
# prompt = PromptTemplate(
|
| 148 |
+
# input_variables=["image_context", "question"],
|
| 149 |
+
# template="Based on the image context: {image_context}, answer the question: {question}"
|
| 150 |
+
# )
|
| 151 |
+
# chain = LLMChain(llm=llm, prompt=prompt)
|
| 152 |
+
|
| 153 |
+
# def generate_response(image_context, question):
|
| 154 |
+
# return chain.run(image_context=image_context, question=question)
|
| 155 |
+
|
| 156 |
+
# # Streamlit App
|
| 157 |
+
# st.title("Intelligent Multimodal Chatbot")
|
| 158 |
+
# st.write("Upload an image and ask a question about it.")
|
| 159 |
+
|
| 160 |
+
# uploaded_image = st.file_uploader("Upload an image", type=["png", "jpg", "jpeg"])
|
| 161 |
+
# question = st.text_input("Ask a question about the image")
|
| 162 |
+
|
| 163 |
+
# if uploaded_image and question:
|
| 164 |
+
# try:
|
| 165 |
+
# user_input = UserInput(question=question)
|
| 166 |
+
# image = Image.open(uploaded_image)
|
| 167 |
+
# st.image(image, caption="Uploaded Image", use_column_width=True)
|
| 168 |
+
# processed_image = preprocess_image(image)
|
| 169 |
+
|
| 170 |
+
# image_context = get_image_context(image, question)
|
| 171 |
+
# response = generate_response(image_context, question)
|
| 172 |
+
|
| 173 |
+
# chat_response = ChatResponse(answer=response)
|
| 174 |
+
# st.write("**Answer**: ", chat_response.answer)
|
| 175 |
+
# st.write("**Confidence**: ", chat_response.confidence)
|
| 176 |
+
|
| 177 |
+
# except Exception as e:
|
| 178 |
+
# st.error(f"Error: {str(e)}")
|
| 179 |
+
# else:
|
| 180 |
+
# st.write("Please upload an image and enter a question.")
|