Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -117,44 +117,26 @@ def extract_pptx(path):
|
|
| 117 |
# raise gr.Error("Oops, unsupported files.")
|
| 118 |
|
| 119 |
def mode_load(file_obj):
|
| 120 |
-
# Intenta detectar el tipo de archivo basado en su contenido
|
| 121 |
try:
|
| 122 |
file_obj.seek(0) # Asegúrate de que el puntero esté al inicio del archivo
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
|
|
|
|
|
|
|
|
|
| 126 |
content = extract_pdf(file_obj)
|
| 127 |
choice = "doc"
|
| 128 |
elif file_obj.name.endswith(".docx"):
|
| 129 |
-
file_obj.seek(0)
|
| 130 |
content = extract_docx(file_obj)
|
| 131 |
choice = "doc"
|
| 132 |
elif file_obj.name.endswith(".pptx"):
|
| 133 |
-
file_obj.seek(0)
|
| 134 |
content = extract_pptx(file_obj)
|
| 135 |
choice = "doc"
|
| 136 |
-
elif file_obj.name.endswith(".txt"):
|
| 137 |
-
file_obj.seek(0)
|
| 138 |
-
content = file_obj.read().decode('utf-8', errors='ignore')
|
| 139 |
-
choice = "doc"
|
| 140 |
-
elif file_obj.name.endswith(".py"):
|
| 141 |
-
file_obj.seek(0)
|
| 142 |
-
content = file_obj.read().decode('utf-8', errors='ignore')
|
| 143 |
-
choice = "doc"
|
| 144 |
-
elif file_obj.name.endswith(".json"):
|
| 145 |
-
file_obj.seek(0)
|
| 146 |
-
content = file_obj.read().decode('utf-8', errors='ignore')
|
| 147 |
-
choice = "doc"
|
| 148 |
-
elif file_obj.name.endswith(".cpp"):
|
| 149 |
-
file_obj.seek(0)
|
| 150 |
-
content = file_obj.read().decode('utf-8', errors='ignore')
|
| 151 |
-
choice = "doc"
|
| 152 |
-
elif file_obj.name.endswith(".md"):
|
| 153 |
-
file_obj.seek(0)
|
| 154 |
content = file_obj.read().decode('utf-8', errors='ignore')
|
| 155 |
choice = "doc"
|
| 156 |
elif file_obj.name.endswith((".png", ".jpg", ".jpeg", ".bmp", ".tiff", ".webp")):
|
| 157 |
-
file_obj.seek(0)
|
| 158 |
content = Image.open(file_obj).convert('RGB')
|
| 159 |
choice = "image"
|
| 160 |
else:
|
|
@@ -329,11 +311,8 @@ def simple_chat(message: dict, temperature: float = 0.8, max_length: int = 4096,
|
|
| 329 |
conversation = []
|
| 330 |
|
| 331 |
if "file" in message and message["file"]:
|
| 332 |
-
#
|
| 333 |
-
|
| 334 |
-
# Convierte los bytes a una cadena si `mode_load` espera texto
|
| 335 |
-
file_contents_str = file_contents.decode('utf-8', errors='ignore')
|
| 336 |
-
choice, contents = mode_load(file_contents_str)
|
| 337 |
if choice == "image":
|
| 338 |
conversation.append({"role": "user", "image": contents, "content": message["text"]})
|
| 339 |
elif choice == "doc":
|
|
|
|
| 117 |
# raise gr.Error("Oops, unsupported files.")
|
| 118 |
|
| 119 |
def mode_load(file_obj):
|
|
|
|
| 120 |
try:
|
| 121 |
file_obj.seek(0) # Asegúrate de que el puntero esté al inicio del archivo
|
| 122 |
+
|
| 123 |
+
# Detecta el tipo de archivo basándote en los primeros bytes si es posible
|
| 124 |
+
file_header = file_obj.read(4)
|
| 125 |
+
file_obj.seek(0) # Vuelve al inicio del archivo para procesamiento completo
|
| 126 |
+
|
| 127 |
+
if file_header.startswith(b'%PDF'):
|
| 128 |
content = extract_pdf(file_obj)
|
| 129 |
choice = "doc"
|
| 130 |
elif file_obj.name.endswith(".docx"):
|
|
|
|
| 131 |
content = extract_docx(file_obj)
|
| 132 |
choice = "doc"
|
| 133 |
elif file_obj.name.endswith(".pptx"):
|
|
|
|
| 134 |
content = extract_pptx(file_obj)
|
| 135 |
choice = "doc"
|
| 136 |
+
elif file_obj.name.endswith(".txt") or file_obj.name.endswith(".py") or file_obj.name.endswith(".json") or file_obj.name.endswith(".cpp") or file_obj.name.endswith(".md"):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
content = file_obj.read().decode('utf-8', errors='ignore')
|
| 138 |
choice = "doc"
|
| 139 |
elif file_obj.name.endswith((".png", ".jpg", ".jpeg", ".bmp", ".tiff", ".webp")):
|
|
|
|
| 140 |
content = Image.open(file_obj).convert('RGB')
|
| 141 |
choice = "image"
|
| 142 |
else:
|
|
|
|
| 311 |
conversation = []
|
| 312 |
|
| 313 |
if "file" in message and message["file"]:
|
| 314 |
+
file_contents = io.BytesIO(message["file"]) # Asegúrate de que sea un objeto BytesIO
|
| 315 |
+
choice, contents = mode_load(file_contents)
|
|
|
|
|
|
|
|
|
|
| 316 |
if choice == "image":
|
| 317 |
conversation.append({"role": "user", "image": contents, "content": message["text"]})
|
| 318 |
elif choice == "doc":
|