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

πŸš‘ Fix UTF-8 surrogate bug with safe NamedString fallback

Browse files
Files changed (1) hide show
  1. app.py +6 -3
app.py CHANGED
@@ -70,16 +70,18 @@ def process_file(file):
70
  try:
71
  ext = os.path.splitext(file.name)[1].lower()
72
  with tempfile.NamedTemporaryFile(delete=False, suffix=ext) as tmp:
73
- # Ensure bytes are written regardless of type
74
  if hasattr(file, "read"):
75
  file_bytes = file.read()
76
- elif isinstance(file, str):
77
  with open(file, "rb") as f:
78
  file_bytes = f.read()
79
  elif isinstance(file, bytes):
80
  file_bytes = file
81
  else:
82
- file_bytes = str(file).encode("utf-8", errors="ignore") # βœ… fixed fallback
 
 
83
 
84
  tmp.write(file_bytes)
85
  tmp.flush()
@@ -97,6 +99,7 @@ def process_file(file):
97
 
98
 
99
 
 
100
  def generate_prompt(context, question):
101
  return f"""
102
  You are a helpful academic tutor assisting a student strictly based on course slides or textbook material.
 
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()
 
99
 
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.