selva1909 commited on
Commit
031c169
·
verified ·
1 Parent(s): 5a71086

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +74 -5
app.py CHANGED
@@ -131,8 +131,67 @@ def call_openrouter(prompt: str):
131
  except Exception as e:
132
  return f"[OpenRouter request error] {e}"
133
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
134
 
135
- # ---------------- PDF Upload & Index ----------------
 
 
 
136
  def upload_and_index(files):
137
  global DOCS, FILENAMES, EMBEDDINGS, CURRENT_CACHE_KEY
138
 
@@ -140,13 +199,22 @@ def upload_and_index(files):
140
  return "No PDF uploaded.", ""
141
 
142
  processed = []
143
- for f in files:
144
- name = os.path.basename(f.name)
145
- b = f.read()
146
- processed.append((name, b))
147
 
 
 
 
 
 
 
 
 
 
148
  preview = [{"name": n, "size": len(b)} for n, b in processed]
149
 
 
150
  cache_key = make_cache_key(processed)
151
  CURRENT_CACHE_KEY = cache_key
152
 
@@ -158,6 +226,7 @@ def upload_and_index(files):
158
  build_faiss(EMBEDDINGS)
159
  return f"Loaded cached embeddings ({len(FILENAMES)} PDFs).", json.dumps(preview)
160
 
 
161
  DOCS = [extract_text_from_pdf(b) for _, b in processed]
162
  FILENAMES = [n for n, _ in processed]
163
 
 
131
  except Exception as e:
132
  return f"[OpenRouter request error] {e}"
133
 
134
+ # ---------- Helper to read bytes from various Gradio file shapes ----------
135
+ def read_file_bytes(f) -> Tuple[str, bytes]:
136
+ """
137
+ Accepts the variety of file objects Gradio may pass:
138
+ - file-like objects with .name and .read()
139
+ - objects with .name and .value (NamedString)
140
+ - tuples like (name, bytes)
141
+ - dicts that may contain 'name' and 'data' or temporary path keys
142
+ - string filesystem paths
143
+ Returns (filename, bytes)
144
+ Raises ValueError for unsupported shapes.
145
+ """
146
+ # tuple (name, bytes)
147
+ if isinstance(f, tuple) and len(f) == 2 and isinstance(f[1], (bytes, bytearray)):
148
+ return f[0], bytes(f[1])
149
+
150
+ # dict-like (from some frontends)
151
+ if isinstance(f, dict):
152
+ name = f.get("name") or f.get("filename") or "uploaded"
153
+ # raw bytes/content
154
+ data = f.get("data") or f.get("content") or f.get("value") or f.get("file")
155
+ if isinstance(data, (bytes, bytearray)):
156
+ return name, bytes(data)
157
+ if isinstance(data, str):
158
+ # data could be text content
159
+ try:
160
+ return name, data.encode("utf-8")
161
+ except Exception:
162
+ pass
163
+ # maybe a temp file path
164
+ tmp_path = f.get("tmp_path") or f.get("path") or f.get("file")
165
+ if tmp_path and isinstance(tmp_path, str) and os.path.exists(tmp_path):
166
+ with open(tmp_path, "rb") as fh:
167
+ return os.path.basename(tmp_path), fh.read()
168
+
169
+ # file-like object with read()
170
+ if hasattr(f, "name") and hasattr(f, "read"):
171
+ try:
172
+ name = os.path.basename(f.name) if getattr(f, "name", None) else "uploaded"
173
+ return name, f.read()
174
+ except Exception:
175
+ pass
176
+
177
+ # NamedString-like: has .name and .value
178
+ if hasattr(f, "name") and hasattr(f, "value"):
179
+ name = os.path.basename(getattr(f, "name") or "uploaded")
180
+ v = getattr(f, "value")
181
+ if isinstance(v, (bytes, bytearray)):
182
+ return name, bytes(v)
183
+ if isinstance(v, str):
184
+ return name, v.encode("utf-8")
185
+
186
+ # string path
187
+ if isinstance(f, str) and os.path.exists(f):
188
+ with open(f, "rb") as fh:
189
+ return os.path.basename(f), fh.read()
190
 
191
+ raise ValueError(f"Unsupported file object type: {type(f)}")
192
+
193
+
194
+ # ---------------- PDF Upload & Index (fixed) ----------------
195
  def upload_and_index(files):
196
  global DOCS, FILENAMES, EMBEDDINGS, CURRENT_CACHE_KEY
197
 
 
199
  return "No PDF uploaded.", ""
200
 
201
  processed = []
202
+ # files may be a single object or a list; normalize
203
+ if not isinstance(files, (list, tuple)):
204
+ files = [files]
 
205
 
206
+ try:
207
+ for f in files:
208
+ name, b = read_file_bytes(f)
209
+ processed.append((name, b))
210
+ except ValueError as e:
211
+ # return a clear message to the UI so user can debug what Gradio passed
212
+ return f"Upload error: {e}", ""
213
+
214
+ # preview for UI
215
  preview = [{"name": n, "size": len(b)} for n, b in processed]
216
 
217
+ # cache key
218
  cache_key = make_cache_key(processed)
219
  CURRENT_CACHE_KEY = cache_key
220
 
 
226
  build_faiss(EMBEDDINGS)
227
  return f"Loaded cached embeddings ({len(FILENAMES)} PDFs).", json.dumps(preview)
228
 
229
+ # extract text and index
230
  DOCS = [extract_text_from_pdf(b) for _, b in processed]
231
  FILENAMES = [n for n, _ in processed]
232