Update app.py
Browse files
app.py
CHANGED
|
@@ -3,18 +3,17 @@ import cv2
|
|
| 3 |
import numpy as np
|
| 4 |
import os
|
| 5 |
from PIL import Image
|
| 6 |
-
from transformers import pipeline
|
| 7 |
-
from langchain import LLMChain
|
|
|
|
| 8 |
from langchain_huggingface import ChatHuggingFace
|
| 9 |
from pydantic import BaseModel, validator
|
| 10 |
from typing import Optional
|
| 11 |
|
| 12 |
-
|
| 13 |
hf = os.getenv('hf')
|
| 14 |
os.environ['HUGGINGFACEHUB_API_TOKEN'] = hf
|
| 15 |
os.environ['HF_TOKEN'] = hf
|
| 16 |
|
| 17 |
-
|
| 18 |
# Pydantic models for input/output validation
|
| 19 |
class UserInput(BaseModel):
|
| 20 |
question: str
|
|
@@ -37,20 +36,25 @@ class ChatResponse(BaseModel):
|
|
| 37 |
|
| 38 |
# Image preprocessing with OpenCV
|
| 39 |
def preprocess_image(image):
|
| 40 |
-
img = np.array(image)
|
| 41 |
-
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
|
| 42 |
-
img = cv2.resize(img, (224, 224))
|
| 43 |
return img
|
| 44 |
|
| 45 |
-
# HuggingFace
|
| 46 |
vqa_pipeline = pipeline("visual-question-answering", model="Salesforce/blip-vqa-base")
|
| 47 |
|
| 48 |
def get_image_context(image, question):
|
| 49 |
result = vqa_pipeline(image, question, top_k=1)
|
| 50 |
return result[0]['answer']
|
| 51 |
|
| 52 |
-
#
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
prompt = PromptTemplate(
|
| 55 |
input_variables=["image_context", "question"],
|
| 56 |
template="Based on the image context: {image_context}, answer the question: {question}"
|
|
@@ -60,36 +64,24 @@ chain = LLMChain(llm=llm, prompt=prompt)
|
|
| 60 |
def generate_response(image_context, question):
|
| 61 |
return chain.run(image_context=image_context, question=question)
|
| 62 |
|
| 63 |
-
# Streamlit
|
| 64 |
st.title("Intelligent Multimodal Chatbot")
|
| 65 |
st.write("Upload an image and ask a question about it.")
|
| 66 |
|
| 67 |
-
# Image upload
|
| 68 |
uploaded_image = st.file_uploader("Upload an image", type=["png", "jpg", "jpeg"])
|
| 69 |
question = st.text_input("Ask a question about the image")
|
| 70 |
|
| 71 |
if uploaded_image and question:
|
| 72 |
try:
|
| 73 |
-
# Validate input using Pydantic
|
| 74 |
user_input = UserInput(question=question)
|
| 75 |
-
|
| 76 |
-
# Display uploaded image
|
| 77 |
image = Image.open(uploaded_image)
|
| 78 |
st.image(image, caption="Uploaded Image", use_column_width=True)
|
| 79 |
-
|
| 80 |
-
# Preprocess image
|
| 81 |
processed_image = preprocess_image(image)
|
| 82 |
|
| 83 |
-
# Get image context
|
| 84 |
image_context = get_image_context(image, question)
|
| 85 |
-
|
| 86 |
-
# Generate response
|
| 87 |
response = generate_response(image_context, question)
|
| 88 |
|
| 89 |
-
# Validate response using Pydantic
|
| 90 |
chat_response = ChatResponse(answer=response)
|
| 91 |
-
|
| 92 |
-
# Display response
|
| 93 |
st.write("**Answer**: ", chat_response.answer)
|
| 94 |
st.write("**Confidence**: ", chat_response.confidence)
|
| 95 |
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
import os
|
| 5 |
from PIL import Image
|
| 6 |
+
from transformers import pipeline, AutoModelForCausalLM, AutoTokenizer
|
| 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
|
|
|
|
| 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 |
def get_image_context(image, question):
|
| 48 |
result = vqa_pipeline(image, question, top_k=1)
|
| 49 |
return result[0]['answer']
|
| 50 |
|
| 51 |
+
# 🔥 Corrected LangChain setup
|
| 52 |
+
model_id = "meta-llama/Llama-3-7b-chat-hf"
|
| 53 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id, token=hf)
|
| 54 |
+
model = AutoModelForCausalLM.from_pretrained(model_id, token=hf)
|
| 55 |
+
|
| 56 |
+
llm = ChatHuggingFace(llm=model, tokenizer=tokenizer)
|
| 57 |
+
|
| 58 |
prompt = PromptTemplate(
|
| 59 |
input_variables=["image_context", "question"],
|
| 60 |
template="Based on the image context: {image_context}, answer the question: {question}"
|
|
|
|
| 64 |
def generate_response(image_context, question):
|
| 65 |
return chain.run(image_context=image_context, question=question)
|
| 66 |
|
| 67 |
+
# Streamlit App
|
| 68 |
st.title("Intelligent Multimodal Chatbot")
|
| 69 |
st.write("Upload an image and ask a question about it.")
|
| 70 |
|
|
|
|
| 71 |
uploaded_image = st.file_uploader("Upload an image", type=["png", "jpg", "jpeg"])
|
| 72 |
question = st.text_input("Ask a question about the image")
|
| 73 |
|
| 74 |
if uploaded_image and question:
|
| 75 |
try:
|
|
|
|
| 76 |
user_input = UserInput(question=question)
|
|
|
|
|
|
|
| 77 |
image = Image.open(uploaded_image)
|
| 78 |
st.image(image, caption="Uploaded Image", use_column_width=True)
|
|
|
|
|
|
|
| 79 |
processed_image = preprocess_image(image)
|
| 80 |
|
|
|
|
| 81 |
image_context = get_image_context(image, question)
|
|
|
|
|
|
|
| 82 |
response = generate_response(image_context, question)
|
| 83 |
|
|
|
|
| 84 |
chat_response = ChatResponse(answer=response)
|
|
|
|
|
|
|
| 85 |
st.write("**Answer**: ", chat_response.answer)
|
| 86 |
st.write("**Confidence**: ", chat_response.confidence)
|
| 87 |
|