aazankhanYousafzai commited on
Commit
657a419
·
verified ·
1 Parent(s): 3f762f1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -1
app.py CHANGED
@@ -14,8 +14,18 @@ DEFAULT_MSG = "How can I help you today?"
14
  def read_pdf(file):
15
  if file is None:
16
  return ""
17
- path = file.name if hasattr(file, "name") else file
 
 
 
 
 
 
18
  reader = PdfReader(path)
 
 
 
 
19
 
20
  def ask_ai(prompt):
21
  res = client.chat.completions.create(
@@ -43,6 +53,11 @@ def app(text, pdf, mode):
43
  if pdf is not None:
44
  text = read_pdf(pdf)
45
 
 
 
 
 
 
46
  custom_css = """
47
  /* FORCE OVERRIDE HF THEME */
48
  .gradio-container * { box-sizing: border-box; }
 
14
  def read_pdf(file):
15
  if file is None:
16
  return ""
17
+
18
+ # HF sends dict: {'name':..., 'path':...}
19
+ if isinstance(file, dict):
20
+ path = file.get("path")
21
+ else:
22
+ path = getattr(file, "name", file)
23
+
24
  reader = PdfReader(path)
25
+ text = ""
26
+ for page in reader.pages:
27
+ text += page.extract_text() or ""
28
+ return text
29
 
30
  def ask_ai(prompt):
31
  res = client.chat.completions.create(
 
53
  if pdf is not None:
54
  text = read_pdf(pdf)
55
 
56
+ if not text.strip():
57
+ return DEFAULT_MSG
58
+
59
+ return router(text, mode)
60
+
61
  custom_css = """
62
  /* FORCE OVERRIDE HF THEME */
63
  .gradio-container * { box-sizing: border-box; }