Dua Rajper commited on
Commit
087b5b2
Β·
verified Β·
1 Parent(s): 0ae967a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -23
app.py CHANGED
@@ -1,19 +1,27 @@
 
1
  import streamlit as st
2
  import easyocr
3
  import openai
4
- from openai import OpenAI
5
  from PIL import Image
6
- import numpy as np
7
  import io
8
 
9
- # βœ… Fix: Ensure set_page_config is the first Streamlit command
10
- st.set_page_config(page_title="Multimodal AI Assistant", layout="wide")
 
 
 
 
 
11
 
12
- # βœ… OpenAI API Key Setup (Replace with your actual key)
13
- client = OpenAI(api_key="your_openai_api_key") # Set your OpenAI API key
 
 
 
14
 
15
  # βœ… Initialize OCR Reader
16
- reader = easyocr.Reader(['en'])
17
 
18
  # βœ… Streamlit App Layout
19
  st.title("πŸ“Έ Multimodal AI Assistant")
@@ -27,28 +35,38 @@ if uploaded_file:
27
  image = Image.open(uploaded_file)
28
  st.image(image, caption="Uploaded Image", use_container_width=True)
29
 
30
- # βœ… Convert image to a format that EasyOCR supports
31
  image_bytes = io.BytesIO(uploaded_file.getvalue()).read()
32
- extracted_text = reader.readtext(image_bytes, detail=0)
 
 
 
33
 
34
  # βœ… Show extracted text
35
  extracted_text_str = " ".join(extracted_text) if extracted_text else "No text found"
36
  st.subheader("πŸ“ Extracted Text:")
37
  st.write(extracted_text_str)
38
 
39
- # βœ… Ask a question about the extracted text
40
  user_query = st.text_input("Ask a question about the extracted text:")
41
 
42
- if user_query:
43
- with st.spinner("Thinking... πŸ’­"):
44
- response = client.chat.completions.create(
45
- model="gpt-3.5-turbo",
46
- messages=[
47
- {"role": "system", "content": "You are an AI assistant analyzing extracted text from images."},
48
- {"role": "user", "content": f"Extracted text: {extracted_text_str}\n\nUser question: {user_query}"}
49
- ]
50
- )
51
-
52
- answer = response.choices[0].message.content
53
- st.subheader("πŸ€– AI Answer:")
54
- st.write(answer)
 
 
 
 
 
 
 
 
1
+ import os
2
  import streamlit as st
3
  import easyocr
4
  import openai
5
+ from dotenv import load_dotenv
6
  from PIL import Image
 
7
  import io
8
 
9
+ # βœ… Load API key from .env
10
+ load_dotenv()
11
+ api_key = os.getenv("GROQ_API_KEY")
12
+
13
+ if not api_key:
14
+ st.error("❌ API key not found! Please set `GROQ_API_KEY` in your `.env` file.")
15
+ st.stop()
16
 
17
+ # βœ… Set up OpenAI (Groq API)
18
+ openai.api_key = api_key
19
+
20
+ # βœ… Ensure Streamlit config is first
21
+ st.set_page_config(page_title="Multimodal AI Assistant", layout="wide")
22
 
23
  # βœ… Initialize OCR Reader
24
+ reader = easyocr.Reader(["en"])
25
 
26
  # βœ… Streamlit App Layout
27
  st.title("πŸ“Έ Multimodal AI Assistant")
 
35
  image = Image.open(uploaded_file)
36
  st.image(image, caption="Uploaded Image", use_container_width=True)
37
 
38
+ # βœ… Convert image to bytes for EasyOCR
39
  image_bytes = io.BytesIO(uploaded_file.getvalue()).read()
40
+
41
+ # βœ… Extract text using EasyOCR
42
+ with st.spinner("πŸ” Extracting text..."):
43
+ extracted_text = reader.readtext(image_bytes, detail=0)
44
 
45
  # βœ… Show extracted text
46
  extracted_text_str = " ".join(extracted_text) if extracted_text else "No text found"
47
  st.subheader("πŸ“ Extracted Text:")
48
  st.write(extracted_text_str)
49
 
50
+ # βœ… Question Answering Section
51
  user_query = st.text_input("Ask a question about the extracted text:")
52
 
53
+ if st.button("Get Answer"):
54
+ if not extracted_text_str or extracted_text_str == "No text found":
55
+ st.warning("⚠ No text available for analysis.")
56
+ elif user_query.strip() == "":
57
+ st.warning("⚠ Please enter a question.")
58
+ else:
59
+ with st.spinner("πŸ€– Thinking..."):
60
+ try:
61
+ response = openai.ChatCompletion.create(
62
+ model="llama3-70b-8192", # Update model if needed
63
+ messages=[
64
+ {"role": "system", "content": "You are an AI assistant analyzing extracted text from images."},
65
+ {"role": "user", "content": f"Extracted text: {extracted_text_str}\n\nUser question: {user_query}"}
66
+ ]
67
+ )
68
+ answer = response["choices"][0]["message"]["content"]
69
+ st.subheader("πŸ€– AI Answer:")
70
+ st.write(answer)
71
+ except openai.error.OpenAIError as e:
72
+ st.error(f"❌ API Error: {e}")