| | import os |
| | import streamlit as st |
| | import easyocr |
| | from openai import OpenAI, OpenAIError |
| | from dotenv import load_dotenv |
| | from PIL import Image |
| | import io |
| |
|
| | |
| | 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() |
| |
|
| | |
| | client = OpenAI(api_key=api_key) |
| |
|
| | |
| | st.set_page_config(page_title="Multimodal AI Assistant", layout="wide") |
| |
|
| | |
| | reader = easyocr.Reader(["en"]) |
| |
|
| | |
| | st.title("πΈ Multimodal AI Assistant") |
| | st.write("Upload an image and ask questions based on the extracted text.") |
| |
|
| | |
| | uploaded_file = st.file_uploader("Upload an image", type=["png", "jpg", "jpeg"]) |
| |
|
| | if uploaded_file: |
| | |
| | image = Image.open(uploaded_file) |
| | st.image(image, caption="Uploaded Image", use_container_width=True) |
| |
|
| | |
| | image_bytes = io.BytesIO(uploaded_file.getvalue()).read() |
| |
|
| | |
| | with st.spinner("π Extracting text..."): |
| | extracted_text = reader.readtext(image_bytes, detail=0) |
| |
|
| | |
| | extracted_text_str = " ".join(extracted_text) if extracted_text else "No text found" |
| | st.subheader("π Extracted Text:") |
| | st.write(extracted_text_str) |
| |
|
| | |
| | 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( |
| | model="llama3-70b-8192", |
| | 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}") |
| |
|