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}")