File size: 2,703 Bytes
087b5b2 e9ed06c b2bb593 087b5b2 91487f8 e9ed06c 087b5b2 e9ed06c b2bb593 087b5b2 e9ed06c 91487f8 087b5b2 e9ed06c 91487f8 e9ed06c 91487f8 e9ed06c 91487f8 e9ed06c 087b5b2 91487f8 087b5b2 e9ed06c 91487f8 e9ed06c 087b5b2 91487f8 e9ed06c 087b5b2 b2bb593 087b5b2 b2bb593 087b5b2 b2bb593 087b5b2 | 1 2 3 4 5 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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | import os
import streamlit as st
import easyocr
from openai import OpenAI, OpenAIError # β
Correct import for API errors
from dotenv import load_dotenv
from PIL import Image
import io
# β
Load API key from .env
load_dotenv()
api_key = os.getenv("GROQ_API_KEY")
if not api_key:
st.error("β API key not found! Please set `GROQ_API_KEY` in your `.env` file.")
st.stop()
# β
Initialize OpenAI client for Groq API
client = OpenAI(api_key=api_key)
# β
Ensure Streamlit config is first
st.set_page_config(page_title="Multimodal AI Assistant", layout="wide")
# β
Initialize OCR Reader
reader = easyocr.Reader(["en"])
# β
Streamlit App Layout
st.title("πΈ Multimodal AI Assistant")
st.write("Upload an image and ask questions based on the extracted text.")
# β
File Uploader
uploaded_file = st.file_uploader("Upload an image", type=["png", "jpg", "jpeg"])
if uploaded_file:
# β
Display uploaded image
image = Image.open(uploaded_file)
st.image(image, caption="Uploaded Image", use_container_width=True)
# β
Convert image to bytes for EasyOCR
image_bytes = io.BytesIO(uploaded_file.getvalue()).read()
# β
Extract text using EasyOCR
with st.spinner("π Extracting text..."):
extracted_text = reader.readtext(image_bytes, detail=0)
# β
Show extracted text
extracted_text_str = " ".join(extracted_text) if extracted_text else "No text found"
st.subheader("π Extracted Text:")
st.write(extracted_text_str)
# β
Question Answering Section
user_query = st.text_input("Ask a question about the extracted text:")
if st.button("Get Answer"):
if not extracted_text_str or extracted_text_str == "No text found":
st.warning("β No text available for analysis.")
elif user_query.strip() == "":
st.warning("β Please enter a question.")
else:
with st.spinner("π€ Thinking..."):
try:
response = client.chat.completions.create( # β
Corrected API call
model="llama3-70b-8192", # β
Groq LLaMA 3 model
messages=[
{"role": "system", "content": "You are an AI assistant analyzing extracted text from images."},
{"role": "user", "content": f"Extracted text: {extracted_text_str}\n\nUser question: {user_query}"}
]
)
answer = response.choices[0].message.content
st.subheader("π€ AI Answer:")
st.write(answer)
except OpenAIError as e:
st.error(f"β API Error: {e}")
|