mateoluksenberg commited on
Commit
f80b970
·
verified ·
1 Parent(s): 7104c5d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -23
app.py CHANGED
@@ -299,7 +299,7 @@ EXAMPLES = [
299
 
300
 
301
  @spaces.GPU()
302
- def simple_chat(message: dict, temperature: float = 0.8, max_length: int = 4096, top_p: float = 1, top_k: int = 10, penalty: float = 1.0):
303
  try:
304
  model = AutoModelForCausalLM.from_pretrained(
305
  MODEL_ID,
@@ -310,9 +310,8 @@ def simple_chat(message: dict, temperature: float = 0.8, max_length: int = 4096,
310
 
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":
@@ -343,30 +342,30 @@ def simple_chat(message: dict, temperature: float = 0.8, max_length: int = 4096,
343
 
344
 
345
  @app.post("/chat/")
346
- async def test_endpoint(
347
- text: str = Form(...),
348
- file: Optional[UploadFile] = File(None)
349
- ):
350
- if not text:
351
  raise HTTPException(status_code=400, detail="Missing 'text' in request body")
352
 
353
- # Lee el archivo si está presente
354
- file_contents = None
355
- if file:
356
- file_contents = await file.read()
357
-
358
- # Construye el diccionario para `simple_chat`
359
- message = {
360
- "text": text,
361
- "file": file_contents
362
- }
363
-
364
- print(message)
365
 
366
- # Llama a `simple_chat` con el diccionario
367
- response = simple_chat(message)
368
  return response
369
 
 
 
 
 
 
 
 
 
 
370
 
371
  with gr.Blocks(css=CSS, theme="soft", fill_height=True) as demo:
372
  gr.HTML(TITLE)
 
299
 
300
 
301
  @spaces.GPU()
302
+ def simple_chat(message: dict, file_obj: io.BytesIO = None, temperature: float = 0.8, max_length: int = 4096, top_p: float = 1, top_k: int = 10, penalty: float = 1.0):
303
  try:
304
  model = AutoModelForCausalLM.from_pretrained(
305
  MODEL_ID,
 
310
 
311
  conversation = []
312
 
313
+ if file_obj:
314
+ choice, contents = mode_load(file_obj)
 
315
  if choice == "image":
316
  conversation.append({"role": "user", "image": contents, "content": message["text"]})
317
  elif choice == "doc":
 
342
 
343
 
344
  @app.post("/chat/")
345
+ async def test_endpoint(message: dict):
346
+ if "text" not in message:
 
 
 
347
  raise HTTPException(status_code=400, detail="Missing 'text' in request body")
348
 
349
+ if "file" in message and message["file"]:
350
+ file_url = message["file"]
351
+ file_obj = download_file(file_url)
352
+ if file_obj is None:
353
+ raise HTTPException(status_code=400, detail="Failed to download file from URL")
354
+ else:
355
+ file_obj = None
 
 
 
 
 
356
 
357
+ response = simple_chat(message, file_obj)
 
358
  return response
359
 
360
+ def download_file(url: str) -> io.BytesIO:
361
+ try:
362
+ response = requests.get(url)
363
+ response.raise_for_status()
364
+ return io.BytesIO(response.content)
365
+ except Exception as e:
366
+ print(f"Error downloading file: {str(e)}")
367
+ return None
368
+
369
 
370
  with gr.Blocks(css=CSS, theme="soft", fill_height=True) as demo:
371
  gr.HTML(TITLE)