Dua Rajper
Update app.py
9b25b4c verified
# 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)