Dua Rajper
Update app.py
b2bb593 verified
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}")