Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -235,25 +235,7 @@ def simple_chat(message, temperature: float = 0.8, max_length: int = 4096, top_p
|
|
| 235 |
else:
|
| 236 |
choice = ""
|
| 237 |
conversation.append({"role": "user", "image": "", "content": message['text']})
|
| 238 |
-
|
| 239 |
-
# for prompt, answer in history:
|
| 240 |
-
# if answer is None:
|
| 241 |
-
# prompt_files.append(prompt[0])
|
| 242 |
-
# conversation.extend([{"role": "user", "content": ""}, {"role": "assistant", "content": ""}])
|
| 243 |
-
# else:
|
| 244 |
-
# conversation.extend([{"role": "user", "content": prompt}, {"role": "assistant", "content": answer}])
|
| 245 |
-
# if len(prompt_files) > 0:
|
| 246 |
-
# choice, contents = mode_load(prompt_files[-1])
|
| 247 |
-
# else:
|
| 248 |
-
# choice = ""
|
| 249 |
-
# conversation.append({"role": "user", "image": "", "content": message['text']})
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
# if choice == "image":
|
| 253 |
-
# conversation.append({"role": "user", "image": contents, "content": message['text']})
|
| 254 |
-
# elif choice == "doc":
|
| 255 |
-
# format_msg = contents + "\n\n\n" + "{} files uploaded.\n" + message['text']
|
| 256 |
-
# conversation.append({"role": "user", "content": format_msg})
|
| 257 |
|
| 258 |
print(f"Conversation is -\n{conversation}")
|
| 259 |
|
|
@@ -362,37 +344,59 @@ def simple_chat(message, temperature: float = 0.8, max_length: int = 4096, top_p
|
|
| 362 |
# return PlainTextResponse(f"Error: {str(e)}")
|
| 363 |
|
| 364 |
|
| 365 |
-
|
| 366 |
@app.post("/chat/")
|
| 367 |
async def test_endpoint(
|
| 368 |
text: str = Form(...),
|
| 369 |
file: UploadFile = File(None)
|
| 370 |
):
|
|
|
|
|
|
|
|
|
|
| 371 |
# Verificar si se ha subido un archivo
|
| 372 |
if file:
|
| 373 |
# Leer el archivo en memoria
|
| 374 |
file_content = BytesIO(await file.read())
|
| 375 |
file_name = file.filename
|
| 376 |
-
|
| 377 |
-
#
|
| 378 |
-
message
|
| 379 |
-
"text": text,
|
| 380 |
-
"file_content": file_content,
|
| 381 |
-
"file_name": file_name
|
| 382 |
-
}
|
| 383 |
-
else:
|
| 384 |
-
# Si no se sube archivo, solo se incluye el texto
|
| 385 |
-
message = {
|
| 386 |
-
"text": text,
|
| 387 |
-
"file_content": None,
|
| 388 |
-
"file_name": None
|
| 389 |
-
}
|
| 390 |
|
| 391 |
# Llamar a la función `simple_chat` con el mensaje
|
| 392 |
print(message)
|
| 393 |
response = simple_chat(message)
|
| 394 |
|
| 395 |
-
return response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 396 |
|
| 397 |
|
| 398 |
with gr.Blocks(css=CSS, theme="soft", fill_height=True) as demo:
|
|
|
|
| 235 |
else:
|
| 236 |
choice = ""
|
| 237 |
conversation.append({"role": "user", "image": "", "content": message['text']})
|
| 238 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 239 |
|
| 240 |
print(f"Conversation is -\n{conversation}")
|
| 241 |
|
|
|
|
| 344 |
# return PlainTextResponse(f"Error: {str(e)}")
|
| 345 |
|
| 346 |
|
|
|
|
| 347 |
@app.post("/chat/")
|
| 348 |
async def test_endpoint(
|
| 349 |
text: str = Form(...),
|
| 350 |
file: UploadFile = File(None)
|
| 351 |
):
|
| 352 |
+
# Construir el mensaje base solo con el texto
|
| 353 |
+
message = {"text": text, "files": []}
|
| 354 |
+
|
| 355 |
# Verificar si se ha subido un archivo
|
| 356 |
if file:
|
| 357 |
# Leer el archivo en memoria
|
| 358 |
file_content = BytesIO(await file.read())
|
| 359 |
file_name = file.filename
|
| 360 |
+
|
| 361 |
+
# Agregar el archivo al mensaje
|
| 362 |
+
message["files"].append({"file_content": file_content, "file_name": file_name})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 363 |
|
| 364 |
# Llamar a la función `simple_chat` con el mensaje
|
| 365 |
print(message)
|
| 366 |
response = simple_chat(message)
|
| 367 |
|
| 368 |
+
return response
|
| 369 |
+
|
| 370 |
+
# @app.post("/chat/")
|
| 371 |
+
# async def test_endpoint(
|
| 372 |
+
# text: str = Form(...),
|
| 373 |
+
# file: UploadFile = File(None)
|
| 374 |
+
# ):
|
| 375 |
+
# # Verificar si se ha subido un archivo
|
| 376 |
+
# if file:
|
| 377 |
+
# # Leer el archivo en memoria
|
| 378 |
+
# file_content = BytesIO(await file.read())
|
| 379 |
+
# file_name = file.filename
|
| 380 |
+
|
| 381 |
+
# # Construir el mensaje con el archivo y el texto
|
| 382 |
+
# message = {
|
| 383 |
+
# "text": text,
|
| 384 |
+
# "file_content": file_content,
|
| 385 |
+
# "file_name": file_name
|
| 386 |
+
# }
|
| 387 |
+
# else:
|
| 388 |
+
# # Si no se sube archivo, solo se incluye el texto
|
| 389 |
+
# message = {
|
| 390 |
+
# "text": text,
|
| 391 |
+
# "file_content": None,
|
| 392 |
+
# "file_name": None
|
| 393 |
+
# }
|
| 394 |
+
|
| 395 |
+
# # Llamar a la función `simple_chat` con el mensaje
|
| 396 |
+
# print(message)
|
| 397 |
+
# response = simple_chat(message)
|
| 398 |
+
|
| 399 |
+
# return response
|
| 400 |
|
| 401 |
|
| 402 |
with gr.Blocks(css=CSS, theme="soft", fill_height=True) as demo:
|