File size: 1,477 Bytes
9b25b4c
 
c185222
9b25b4c
c185222
9b25b4c
c185222
 
9b25b4c
 
 
 
c185222
 
9b25b4c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c185222
 
9b25b4c
 
 
 
c185222
9b25b4c
 
 
 
 
 
 
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
# prompt: give me app.py

import streamlit as st
import os
from PIL import Image
import openai
import easyocr

# Set OpenAI API key
openai.api_key = os.environ.get("OPENAI_API_KEY")

# Initialize OCR reader
reader = easyocr.Reader(['en'])

def extract_text_from_image(image):
    """Extract text from the image using easyocr."""
    text = reader.readtext(image, detail=0)
    return " ".join(text)


def answer_question(image, question):
    """Answer a question about the image using OpenAI's API."""
    extracted_text = extract_text_from_image(image)
    prompt = f"""
    I have an image with the following text extracted from it: {extracted_text}
    Here's a question about the image: {question}
    Answer the question based on the image content and text.
    """
    response = openai.ChatCompletion.create(
        model="gpt-3.5-turbo",  # Or a suitable model
        messages=[{"role": "user", "content": prompt}]
    )
    return response.choices[0].message.content


# Streamlit app
st.title("Multimodal AI Assistant")

uploaded_image = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])

if uploaded_image is not None:
    image = Image.open(uploaded_image)
    st.image(image, caption="Uploaded Image", use_column_width=True)

    question = st.text_input("Ask a question about the image")

    if question:
        with st.spinner("Generating answer..."):
            answer = answer_question(image, question)
        st.write("Answer:", answer)