SamanthaStorm commited on
Commit
4b0f5e3
·
verified ·
1 Parent(s): 993098f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -20
app.py CHANGED
@@ -201,29 +201,47 @@ def analyze_message(text):
201
  def analyze_composite(uploaded_file, *texts):
202
  outputs = []
203
 
204
- # 1) File upload
205
  if uploaded_file is not None:
206
- name = (uploaded_file.name if hasattr(uploaded_file, "name") else uploaded_file).lower()
207
- if name.endswith((".png",".jpg",".jpeg",".tiff",".bmp",".gif")):
208
- img = Image.open(io.BytesIO(raw))
209
- arr = np.array(img.convert("RGB"))
210
- texts = ocr_reader.readtext(arr, detail=0)
211
- content = "\n".join(texts)
212
- else:
213
  try:
214
- content = raw.decode("utf-8")
215
- except:
216
- content = raw.decode("latin-1")
217
-
218
- r = analyze_message(content)
219
- outputs.append(
220
- "── Uploaded File ──\n"
221
- f"Emotion Profile : {r['emotion_profile']}\n"
222
- f"Active Patterns : {r['active_patterns']}\n"
223
- f"Emotional Tone : {r['tone_tag']}\n"
 
224
  )
225
 
226
- # 2) Text‐box inputs…
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
227
  for idx, txt in enumerate(texts, start=1):
228
  if not txt:
229
  continue
@@ -234,10 +252,13 @@ def analyze_composite(uploaded_file, *texts):
234
  f"Active Patterns : {r['active_patterns']}\n"
235
  f"Emotional Tone : {r['tone_tag']}\n"
236
  )
 
 
237
  if not outputs:
238
  return "Please enter at least one message."
239
- return "\n".join(outputs)
240
 
 
 
241
  # ——— 6) Gradio Interface ————————————————————————————————————————————————
242
  message_inputs = [gr.Textbox(label=f"Message {i+1}") for i in range(1)]
243
  iface = gr.Interface(
 
201
  def analyze_composite(uploaded_file, *texts):
202
  outputs = []
203
 
204
+ # 1) Handle an uploaded file
205
  if uploaded_file is not None:
206
+ # uploaded_file may be a file-like object or just a path string
 
 
 
 
 
 
207
  try:
208
+ raw = uploaded_file.read()
209
+ except Exception:
210
+ # fall back to opening the path
211
+ with open(uploaded_file, "rb") as f:
212
+ raw = f.read()
213
+
214
+ # get the filename (or just lowercase the string if it's a path)
215
+ name = (
216
+ uploaded_file.name.lower()
217
+ if hasattr(uploaded_file, "name")
218
+ else uploaded_file.lower()
219
  )
220
 
221
+ # branch on extension
222
+ if name.endswith((".png", ".jpg", ".jpeg", ".tiff", ".bmp", ".gif")):
223
+ img = Image.open(io.BytesIO(raw))
224
+ # --- using easyocr or pytesseract, e.g. with easyocr: ---
225
+ # arr = np.array(img.convert("RGB"))
226
+ # texts = ocr_reader.readtext(arr, detail=0)
227
+ # content = "\n".join(texts)
228
+ content = pytesseract.image_to_string(img)
229
+ else:
230
+ try:
231
+ content = raw.decode("utf-8")
232
+ except UnicodeDecodeError:
233
+ content = raw.decode("latin-1")
234
+
235
+ # now analyze that extracted content
236
+ r = analyze_message(content)
237
+ outputs.append(
238
+ "── Uploaded File ──\n"
239
+ f"Emotion Profile : {r['emotion_profile']}\n"
240
+ f"Active Patterns : {r['active_patterns']}\n"
241
+ f"Emotional Tone : {r['tone_tag']}\n"
242
+ )
243
+
244
+ # 2) Handle any text‐box inputs
245
  for idx, txt in enumerate(texts, start=1):
246
  if not txt:
247
  continue
 
252
  f"Active Patterns : {r['active_patterns']}\n"
253
  f"Emotional Tone : {r['tone_tag']}\n"
254
  )
255
+
256
+ # 3) No input guard
257
  if not outputs:
258
  return "Please enter at least one message."
 
259
 
260
+ # 4) Return combined results
261
+ return "\n".join(outputs)
262
  # ——— 6) Gradio Interface ————————————————————————————————————————————————
263
  message_inputs = [gr.Textbox(label=f"Message {i+1}") for i in range(1)]
264
  iface = gr.Interface(