umerforsure commited on
Commit
e0d1ddf
·
1 Parent(s): d8f6578

🚨 Final UTF-8 fix: safe file handling for NamedString

Browse files
Files changed (1) hide show
  1. app.py +4 -8
app.py CHANGED
@@ -68,20 +68,15 @@ def extract_text(file_path, ext):
68
  def process_file(file):
69
  global vectorstore
70
  try:
71
- ext = os.path.splitext(file.name)[1].lower()
72
  with tempfile.NamedTemporaryFile(delete=False, suffix=ext) as tmp:
73
- # Safely convert input into bytes
74
  if hasattr(file, "read"):
75
  file_bytes = file.read()
76
- elif isinstance(file, str) and os.path.isfile(file):
77
- with open(file, "rb") as f:
78
- file_bytes = f.read()
79
  elif isinstance(file, bytes):
80
  file_bytes = file
81
  else:
82
- # Fallback: try encoding a safe version of its name
83
- safe_name = getattr(file, "name", "fallback.txt")
84
- file_bytes = safe_name.encode("utf-8", errors="ignore")
85
 
86
  tmp.write(file_bytes)
87
  tmp.flush()
@@ -100,6 +95,7 @@ def process_file(file):
100
 
101
 
102
 
 
103
  def generate_prompt(context, question):
104
  return f"""
105
  You are a helpful academic tutor assisting a student strictly based on course slides or textbook material.
 
68
  def process_file(file):
69
  global vectorstore
70
  try:
71
+ ext = os.path.splitext(getattr(file, "name", "file.pdf"))[1].lower()
72
  with tempfile.NamedTemporaryFile(delete=False, suffix=ext) as tmp:
73
+ # Safely get bytes
74
  if hasattr(file, "read"):
75
  file_bytes = file.read()
 
 
 
76
  elif isinstance(file, bytes):
77
  file_bytes = file
78
  else:
79
+ return "❌ Error: Unsupported file type."
 
 
80
 
81
  tmp.write(file_bytes)
82
  tmp.flush()
 
95
 
96
 
97
 
98
+
99
  def generate_prompt(context, question):
100
  return f"""
101
  You are a helpful academic tutor assisting a student strictly based on course slides or textbook material.