Spaces:
Running
Running
Commit
·
5a107ba
1
Parent(s):
77e94ac
✅ Robust file handling: fixed NamedString and fallback logic
Browse files
app.py
CHANGED
|
@@ -65,20 +65,19 @@ def process_file(file):
|
|
| 65 |
try:
|
| 66 |
ext = os.path.splitext(file.name)[1].lower()
|
| 67 |
with tempfile.NamedTemporaryFile(delete=False, suffix=ext) as tmp:
|
| 68 |
-
#
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
tmp.write(file_bytes)
|
| 81 |
-
|
| 82 |
tmp.flush()
|
| 83 |
full_text = extract_text(tmp.name, ext)
|
| 84 |
|
|
@@ -92,6 +91,7 @@ tmp.write(file_bytes)
|
|
| 92 |
except Exception as e:
|
| 93 |
return f"❌ Error: {str(e)}"
|
| 94 |
|
|
|
|
| 95 |
def generate_prompt(context, question):
|
| 96 |
return f"""
|
| 97 |
You are a helpful academic tutor assisting a student strictly based on course slides or textbook material.
|
|
|
|
| 65 |
try:
|
| 66 |
ext = os.path.splitext(file.name)[1].lower()
|
| 67 |
with tempfile.NamedTemporaryFile(delete=False, suffix=ext) as tmp:
|
| 68 |
+
# ✅ Robust handling for file types: file-like, string path, bytes, NamedString
|
| 69 |
+
if hasattr(file, "read"):
|
| 70 |
+
file_bytes = file.read()
|
| 71 |
+
elif isinstance(file, str) and os.path.exists(file):
|
| 72 |
+
with open(file, "rb") as f:
|
| 73 |
+
file_bytes = f.read()
|
| 74 |
+
elif isinstance(file, bytes):
|
| 75 |
+
file_bytes = file
|
| 76 |
+
else:
|
| 77 |
+
# Fallback for Hugging Face NamedString or other str-like types
|
| 78 |
+
file_bytes = bytes(str(file), encoding='utf-8')
|
| 79 |
+
|
| 80 |
+
tmp.write(file_bytes)
|
|
|
|
| 81 |
tmp.flush()
|
| 82 |
full_text = extract_text(tmp.name, ext)
|
| 83 |
|
|
|
|
| 91 |
except Exception as e:
|
| 92 |
return f"❌ Error: {str(e)}"
|
| 93 |
|
| 94 |
+
|
| 95 |
def generate_prompt(context, question):
|
| 96 |
return f"""
|
| 97 |
You are a helpful academic tutor assisting a student strictly based on course slides or textbook material.
|