IAMTFRMZA commited on
Commit
9d2b563
Β·
verified Β·
1 Parent(s): 00a48e1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -20
app.py CHANGED
@@ -6,6 +6,8 @@ import json
6
  import requests
7
  from PIL import Image
8
  from openai import OpenAI
 
 
9
 
10
  # ------------------ App Configuration ------------------
11
  st.set_page_config(page_title="Document AI Assistant", layout="wide")
@@ -21,6 +23,7 @@ if not OPENAI_API_KEY or not ASSISTANT_ID:
21
  st.stop()
22
 
23
  client = OpenAI(api_key=OPENAI_API_KEY)
 
24
 
25
  # ------------------ Session State Initialization ------------------
26
  if "messages" not in st.session_state:
@@ -43,11 +46,47 @@ if st.sidebar.button("πŸ”„ Clear Chat"):
43
 
44
  show_image = st.sidebar.checkbox("πŸ“– Show Document Image", value=True)
45
 
46
- # ------------------ Load Structured Summary/FAQ ------------------
47
- with open("51940670-Manual-of-Surgical-Pathology-Third-Edition_1_structured_output.json", "r") as f:
48
- structured_data = json.load(f) # This is a list of dicts, not a dict
49
-
50
- # ------------------ Three-Column Layout ------------------
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
  left, center, right = st.columns([1, 2, 1])
52
 
53
  # ------------------ Left Column: Document Image ------------------
@@ -123,24 +162,15 @@ with center:
123
  except Exception as e:
124
  st.error(f"❌ Error: {str(e)}")
125
 
126
- # ------------------ Right Column: Summary and FAQ ------------------
127
  with right:
128
- st.subheader("πŸ“Œ Summary")
129
 
130
- # Parse page number from image URL if available
131
  if st.session_state.image_url:
132
- match = re.search(r'page_(\d+)', st.session_state.image_url)
133
- page_number = int(match.group(1)) if match else 151
134
- else:
135
- page_number = 151 # default
136
-
137
- # Get entry from structured data
138
- page_entry = next((entry for entry in structured_data if entry.get("page") == page_number), None)
139
- if page_entry:
140
- summary_text = page_entry.get("summary", "No summary available.")
141
- faq_list = page_entry.get("faqs", [])
142
  else:
143
- summary_text = "No summary available."
144
  faq_list = []
145
 
146
  st.markdown(summary_text)
@@ -150,4 +180,4 @@ with right:
150
  for faq in faq_list:
151
  st.markdown(f"**Q:** {faq.get('question', '')}\n\n**A:** {faq.get('answer', '')}")
152
  else:
153
- st.info("No FAQs available for this page.")
 
6
  import requests
7
  from PIL import Image
8
  from openai import OpenAI
9
+ import easyocr
10
+ from io import BytesIO
11
 
12
  # ------------------ App Configuration ------------------
13
  st.set_page_config(page_title="Document AI Assistant", layout="wide")
 
23
  st.stop()
24
 
25
  client = OpenAI(api_key=OPENAI_API_KEY)
26
+ reader = easyocr.Reader(['en'], gpu=False)
27
 
28
  # ------------------ Session State Initialization ------------------
29
  if "messages" not in st.session_state:
 
46
 
47
  show_image = st.sidebar.checkbox("πŸ“– Show Document Image", value=True)
48
 
49
+ # ------------------ OCR + GPT Summary & FAQ Generator ------------------
50
+ def generate_summary_and_faq_from_image_easyocr(image_url):
51
+ try:
52
+ response = requests.get(image_url, stream=True)
53
+ image = Image.open(BytesIO(response.content)).convert("RGB")
54
+
55
+ result = reader.readtext(np.array(image), detail=0)
56
+ extracted_text = "\n".join(result)
57
+
58
+ if not extracted_text.strip():
59
+ return "No readable text found in image.", []
60
+
61
+ prompt = f"""
62
+ You are a pathology assistant. Given this OCR-extracted text from a pathology textbook page, do the following:
63
+ 1. Provide a concise summary of the main point (1-2 sentences).
64
+ 2. Provide two FAQs with brief answers.
65
+
66
+ Text:
67
+ {extracted_text[:3000]}
68
+
69
+ Return only JSON:
70
+ {{
71
+ "summary": "...",
72
+ "faqs": [
73
+ {{"question": "...", "answer": "..."}},
74
+ {{"question": "...", "answer": "..."}}
75
+ ]
76
+ }}
77
+ """
78
+ response = client.chat.completions.create(
79
+ model="gpt-3.5-turbo",
80
+ messages=[{"role": "user", "content": prompt}],
81
+ temperature=0.3
82
+ )
83
+ result = json.loads(response.choices[0].message.content)
84
+ return result.get("summary", "No summary generated."), result.get("faqs", [])
85
+
86
+ except Exception as e:
87
+ return f"Error generating summary: {e}", []
88
+
89
+ # ------------------ Layout ------------------
90
  left, center, right = st.columns([1, 2, 1])
91
 
92
  # ------------------ Left Column: Document Image ------------------
 
162
  except Exception as e:
163
  st.error(f"❌ Error: {str(e)}")
164
 
165
+ # ------------------ Right Column: OCR-Based Summary + FAQ ------------------
166
  with right:
167
+ st.subheader("πŸ“Œ Summary & FAQ (via EasyOCR)")
168
 
 
169
  if st.session_state.image_url:
170
+ with st.spinner("πŸ” Extracting text and generating summary..."):
171
+ summary_text, faq_list = generate_summary_and_faq_from_image_easyocr(st.session_state.image_url)
 
 
 
 
 
 
 
 
172
  else:
173
+ summary_text = "No image selected."
174
  faq_list = []
175
 
176
  st.markdown(summary_text)
 
180
  for faq in faq_list:
181
  st.markdown(f"**Q:** {faq.get('question', '')}\n\n**A:** {faq.get('answer', '')}")
182
  else:
183
+ st.info("No FAQs available or generated from this page.")