Update app.py
Browse files
app.py
CHANGED
|
@@ -1,180 +1,37 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
from transformers import pipeline, AutoModelForCausalLM, AutoTokenizer
|
| 5 |
import os
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 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.")
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import langchain
|
| 3 |
+
st.write('Helldo')
|
|
|
|
| 4 |
import os
|
| 5 |
+
import langchain
|
| 6 |
+
import langchain_huggingface
|
| 7 |
+
from pydantic import BaseModel,Field
|
| 8 |
+
from langchain_huggingface import HuggingFaceEndpoint,HuggingFacePipeline,ChatHuggingFace
|
| 9 |
+
from langchain_core.output_parsers import PydanticOutputParser,CommaSeparatedListOutputParser,JsonOutputParser
|
| 10 |
+
from langchain.prompts import PromptTemplate,ChatPromptTemplate,SystemMessagePromptTemplate,HumanMessagePromptTemplate
|
| 11 |
+
from typing import Optional, List
|
| 12 |
+
from langchain_community.document_loaders import UnstructuredPDFLoader
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
# creating the environment
|
| 17 |
+
hk = os.getenv('hf')
|
| 18 |
+
os.environ['HUGGINGFACEHUB_API_TOKEN'] = hk
|
| 19 |
+
os.environ['HF_TOKEN'] = hk
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
# accessing the llm
|
| 23 |
+
# ------ accesssing the llm for geenral prompting -------------------
|
| 24 |
+
llm_skeleton = HuggingFaceEndpoint(repo_id='meta-llama/Llama-3.2-3B-Instruct',
|
| 25 |
+
provider = 'novita',
|
| 26 |
+
temperature=0.7,
|
| 27 |
+
max_new_tokens=150,
|
| 28 |
+
task = 'conversational')
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
# ------------- wrapping the llm to be a conversational model ------------------
|
| 32 |
+
llm = ChatHuggingFace(llm=llm_skeleton,
|
| 33 |
+
repo_id='meta-llama/Llama-3.2-3B-Instruct',
|
| 34 |
+
provider = 'novita',
|
| 35 |
+
temperature=0.7,
|
| 36 |
+
max_new_tokens=150,
|
| 37 |
+
task = 'conversational')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|