Dua Rajper commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,19 +1,27 @@
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import easyocr
|
| 3 |
import openai
|
| 4 |
-
from
|
| 5 |
from PIL import Image
|
| 6 |
-
import numpy as np
|
| 7 |
import io
|
| 8 |
|
| 9 |
-
# β
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
# β
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
# β
Initialize OCR Reader
|
| 16 |
-
reader = easyocr.Reader([
|
| 17 |
|
| 18 |
# β
Streamlit App Layout
|
| 19 |
st.title("πΈ Multimodal AI Assistant")
|
|
@@ -27,28 +35,38 @@ if uploaded_file:
|
|
| 27 |
image = Image.open(uploaded_file)
|
| 28 |
st.image(image, caption="Uploaded Image", use_container_width=True)
|
| 29 |
|
| 30 |
-
# β
Convert image to
|
| 31 |
image_bytes = io.BytesIO(uploaded_file.getvalue()).read()
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
| 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 |
-
# β
|
| 40 |
user_query = st.text_input("Ask a question about the extracted text:")
|
| 41 |
|
| 42 |
-
if
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
import streamlit as st
|
| 3 |
import easyocr
|
| 4 |
import openai
|
| 5 |
+
from dotenv import load_dotenv
|
| 6 |
from PIL import Image
|
|
|
|
| 7 |
import io
|
| 8 |
|
| 9 |
+
# β
Load API key from .env
|
| 10 |
+
load_dotenv()
|
| 11 |
+
api_key = os.getenv("GROQ_API_KEY")
|
| 12 |
+
|
| 13 |
+
if not api_key:
|
| 14 |
+
st.error("β API key not found! Please set `GROQ_API_KEY` in your `.env` file.")
|
| 15 |
+
st.stop()
|
| 16 |
|
| 17 |
+
# β
Set up OpenAI (Groq API)
|
| 18 |
+
openai.api_key = api_key
|
| 19 |
+
|
| 20 |
+
# β
Ensure Streamlit config is first
|
| 21 |
+
st.set_page_config(page_title="Multimodal AI Assistant", layout="wide")
|
| 22 |
|
| 23 |
# β
Initialize OCR Reader
|
| 24 |
+
reader = easyocr.Reader(["en"])
|
| 25 |
|
| 26 |
# β
Streamlit App Layout
|
| 27 |
st.title("πΈ Multimodal AI Assistant")
|
|
|
|
| 35 |
image = Image.open(uploaded_file)
|
| 36 |
st.image(image, caption="Uploaded Image", use_container_width=True)
|
| 37 |
|
| 38 |
+
# β
Convert image to bytes for EasyOCR
|
| 39 |
image_bytes = io.BytesIO(uploaded_file.getvalue()).read()
|
| 40 |
+
|
| 41 |
+
# β
Extract text using EasyOCR
|
| 42 |
+
with st.spinner("π Extracting text..."):
|
| 43 |
+
extracted_text = reader.readtext(image_bytes, detail=0)
|
| 44 |
|
| 45 |
# β
Show extracted text
|
| 46 |
extracted_text_str = " ".join(extracted_text) if extracted_text else "No text found"
|
| 47 |
st.subheader("π Extracted Text:")
|
| 48 |
st.write(extracted_text_str)
|
| 49 |
|
| 50 |
+
# β
Question Answering Section
|
| 51 |
user_query = st.text_input("Ask a question about the extracted text:")
|
| 52 |
|
| 53 |
+
if st.button("Get Answer"):
|
| 54 |
+
if not extracted_text_str or extracted_text_str == "No text found":
|
| 55 |
+
st.warning("β No text available for analysis.")
|
| 56 |
+
elif user_query.strip() == "":
|
| 57 |
+
st.warning("β Please enter a question.")
|
| 58 |
+
else:
|
| 59 |
+
with st.spinner("π€ Thinking..."):
|
| 60 |
+
try:
|
| 61 |
+
response = openai.ChatCompletion.create(
|
| 62 |
+
model="llama3-70b-8192", # Update model if needed
|
| 63 |
+
messages=[
|
| 64 |
+
{"role": "system", "content": "You are an AI assistant analyzing extracted text from images."},
|
| 65 |
+
{"role": "user", "content": f"Extracted text: {extracted_text_str}\n\nUser question: {user_query}"}
|
| 66 |
+
]
|
| 67 |
+
)
|
| 68 |
+
answer = response["choices"][0]["message"]["content"]
|
| 69 |
+
st.subheader("π€ AI Answer:")
|
| 70 |
+
st.write(answer)
|
| 71 |
+
except openai.error.OpenAIError as e:
|
| 72 |
+
st.error(f"β API Error: {e}")
|