Spaces:
Paused
Paused
Update app.py
Browse files35 unmodified lines36373839404142434445464748495051525354555657585960616263646566674445464748495051686970717235 unmodified lines logger.info(f"Segmented text into {len(chunks)} chunks.") return chunks
# Function to read file content with fallback encodingdef read_file_content(file): try: # Try reading with UTF-8 encoding first if hasattr(file, "read"): content = file.read().decode('utf-8') else: content = file.decode('utf-8') logger.info("File read successfully with UTF-8 encoding.") return content except UnicodeDecodeError: # Fallback to latin-1 encoding if UTF-8 fails logger.warning("UTF-8 encoding failed. Trying latin-1 encoding.") if hasattr(file, "read"): file.seek(0) # Reset file pointer to the beginning content = file.read().decode('latin-1') else: content = file.decode('latin-1') logger.info("File read successfully with latin-1 encoding.") return content except Exception as e: logger.error(f"Failed to read file: {e}") raise gr.Error(f"Failed to read file: {str(e)}")
# Function to process the text and make API calls with rate limitingdef process_text(file, prompt): try: logger.info("Starting text processing...") # Read the file content properly if hasattr(file, "read"): # If the file is a file-like object, read its content text = file.read().decode('utf-8') # Decode bytes to string else: # If the file is already a string, use it directly text = file # Read the file content with fallback encoding text = read_file_content(file) logger.info(f"Text length: {len(text)} characters.") # Segment the text into chunksapp.py+26-8