Dua Rajper commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,83 +1,54 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
from PIL import Image
|
| 3 |
-
import torch
|
| 4 |
import easyocr
|
|
|
|
|
|
|
|
|
|
| 5 |
import numpy as np
|
| 6 |
-
import openai # Using OpenAI GPT (or replace with GROQ API)
|
| 7 |
import io
|
| 8 |
-
from transformers import CLIPModel, CLIPImageProcessor
|
| 9 |
|
| 10 |
-
# β
Fix: set_page_config
|
| 11 |
st.set_page_config(page_title="Multimodal AI Assistant", layout="wide")
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
|
| 15 |
-
def load_clip_model():
|
| 16 |
-
model = CLIPModel.from_pretrained(
|
| 17 |
-
"fxmarty/clip-vision-model-tiny",
|
| 18 |
-
ignore_mismatched_sizes=True # β
Fix size mismatch
|
| 19 |
-
)
|
| 20 |
-
processor = CLIPImageProcessor.from_pretrained("fxmarty/clip-vision-model-tiny")
|
| 21 |
-
return model, processor
|
| 22 |
-
|
| 23 |
-
model, processor = load_clip_model()
|
| 24 |
-
|
| 25 |
-
# ---- Load OCR (EasyOCR) ---- #
|
| 26 |
-
@st.cache_resource
|
| 27 |
-
def load_ocr():
|
| 28 |
-
return easyocr.Reader(['en'])
|
| 29 |
-
|
| 30 |
-
reader = load_ocr()
|
| 31 |
|
| 32 |
-
#
|
| 33 |
-
|
| 34 |
-
st.write("Upload an image, extract text, and ask questions!")
|
| 35 |
|
| 36 |
-
#
|
| 37 |
-
|
|
|
|
| 38 |
|
| 39 |
-
|
|
|
|
| 40 |
|
| 41 |
-
if uploaded_file
|
| 42 |
-
#
|
| 43 |
-
image = Image.open(uploaded_file)
|
| 44 |
-
|
| 45 |
-
# β
Fix: use `use_container_width` instead of `use_column_width`
|
| 46 |
st.image(image, caption="Uploaded Image", use_container_width=True)
|
| 47 |
|
| 48 |
-
# β
Convert
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
# β
Fix: Pass the correct format to EasyOCR
|
| 52 |
-
with st.spinner("π Extracting text from image..."):
|
| 53 |
-
extracted_text_list = reader.readtext(image_np, detail=0)
|
| 54 |
-
|
| 55 |
-
extracted_text = " ".join(extracted_text_list) # Combine extracted text
|
| 56 |
-
|
| 57 |
-
st.write("### π Extracted Text:")
|
| 58 |
-
if extracted_text:
|
| 59 |
-
st.success(extracted_text)
|
| 60 |
-
else:
|
| 61 |
-
st.warning("No readable text found in the image.")
|
| 62 |
|
| 63 |
-
#
|
| 64 |
-
if extracted_text
|
| 65 |
-
|
|
|
|
| 66 |
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
# Using OpenAI GPT API (replace with GROQ or Hugging Face LLM if needed)
|
| 70 |
-
openai.api_key = "YOUR_OPENAI_API_KEY" # Store securely in a .env file
|
| 71 |
|
| 72 |
-
|
|
|
|
|
|
|
| 73 |
model="gpt-3.5-turbo",
|
| 74 |
messages=[
|
| 75 |
-
{"role": "system", "content": "You are an AI assistant
|
| 76 |
-
{"role": "user", "content": f"Extracted text: {
|
| 77 |
]
|
| 78 |
)
|
| 79 |
|
| 80 |
-
answer = response
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
st.success(answer)
|
|
|
|
| 1 |
import streamlit as st
|
|
|
|
|
|
|
| 2 |
import easyocr
|
| 3 |
+
import openai
|
| 4 |
+
from openai import OpenAI
|
| 5 |
+
from PIL import Image
|
| 6 |
import numpy as np
|
|
|
|
| 7 |
import io
|
|
|
|
| 8 |
|
| 9 |
+
# β
Fix: Ensure set_page_config is the first Streamlit command
|
| 10 |
st.set_page_config(page_title="Multimodal AI Assistant", layout="wide")
|
| 11 |
|
| 12 |
+
# β
OpenAI API Key Setup (Replace with your actual key)
|
| 13 |
+
client = OpenAI(api_key="your_openai_api_key") # Set your OpenAI API key
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
# β
Initialize OCR Reader
|
| 16 |
+
reader = easyocr.Reader(['en'])
|
|
|
|
| 17 |
|
| 18 |
+
# β
Streamlit App Layout
|
| 19 |
+
st.title("πΈ Multimodal AI Assistant")
|
| 20 |
+
st.write("Upload an image and ask questions based on the extracted text.")
|
| 21 |
|
| 22 |
+
# β
File Uploader
|
| 23 |
+
uploaded_file = st.file_uploader("Upload an image", type=["png", "jpg", "jpeg"])
|
| 24 |
|
| 25 |
+
if uploaded_file:
|
| 26 |
+
# β
Display uploaded image
|
| 27 |
+
image = Image.open(uploaded_file)
|
|
|
|
|
|
|
| 28 |
st.image(image, caption="Uploaded Image", use_container_width=True)
|
| 29 |
|
| 30 |
+
# β
Convert image to a format that EasyOCR supports
|
| 31 |
+
image_bytes = io.BytesIO(uploaded_file.getvalue()).read()
|
| 32 |
+
extracted_text = reader.readtext(image_bytes, detail=0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
+
# β
Show extracted text
|
| 35 |
+
extracted_text_str = " ".join(extracted_text) if extracted_text else "No text found"
|
| 36 |
+
st.subheader("π Extracted Text:")
|
| 37 |
+
st.write(extracted_text_str)
|
| 38 |
|
| 39 |
+
# β
Ask a question about the extracted text
|
| 40 |
+
user_query = st.text_input("Ask a question about the extracted text:")
|
|
|
|
|
|
|
| 41 |
|
| 42 |
+
if user_query:
|
| 43 |
+
with st.spinner("Thinking... π"):
|
| 44 |
+
response = client.chat.completions.create(
|
| 45 |
model="gpt-3.5-turbo",
|
| 46 |
messages=[
|
| 47 |
+
{"role": "system", "content": "You are an AI assistant analyzing extracted text from images."},
|
| 48 |
+
{"role": "user", "content": f"Extracted text: {extracted_text_str}\n\nUser question: {user_query}"}
|
| 49 |
]
|
| 50 |
)
|
| 51 |
|
| 52 |
+
answer = response.choices[0].message.content
|
| 53 |
+
st.subheader("π€ AI Answer:")
|
| 54 |
+
st.write(answer)
|
|
|